From aa004a05b8050050f6ea59883590b54b0ba71a39 Mon Sep 17 00:00:00 2001 From: AjaySi Date: Thu, 4 Apr 2024 18:19:49 +0530 Subject: [PATCH] WIP - Code refactoring --- lib/ai_writers/keywords_to_blog.py | 57 +++++++++++++----------- lib/blog_postprocessing/humanize_blog.py | 34 ++++++++++++++ main_config | 4 +- 3 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 lib/blog_postprocessing/humanize_blog.py diff --git a/lib/ai_writers/keywords_to_blog.py b/lib/ai_writers/keywords_to_blog.py index 4b25d56d..bc3ca45f 100644 --- a/lib/ai_writers/keywords_to_blog.py +++ b/lib/ai_writers/keywords_to_blog.py @@ -22,7 +22,7 @@ from ..ai_web_researcher.you_web_reseacher import get_rag_results, search_ydc_in from ..blog_metadata.get_blog_metadata import blog_metadata from ..blog_postprocessing.save_blog_to_file import save_blog_to_file from ..blog_postprocessing.blog_proof_reader import blog_proof_editor - +from ..blog_postprocessing.humanize_blog import blog_humanize def write_blog_from_keywords(search_keywords, url=None): @@ -48,32 +48,32 @@ def write_blog_from_keywords(search_keywords, url=None): # logger.info/check the final blog content. logger.info(f"######### Blog content Google SERP research: ###########\n\n{blog_markdown_str}\n\n") -# # Do Tavily AI research to augument the above blog. -# try: -# tavily_search_result, t_titles = do_tavily_ai_search(search_keywords) -# example_blog_titles.append(t_titles) -# blog_markdown_str = blog_with_research(blog_markdown_str, tavily_search_result) -# logger.info(f"######### Blog content after Tavily AI research: ######### \n\n{blog_markdown_str}\n\n") -# except Exception as err: -# logger.error(f"Failed to do Tavily AI research: {err}") -# -# try: -# # Do Metaphor/Exa AI search. -# metaphor_search_result, m_titles = do_metaphor_ai_research(search_keywords) -# example_blog_titles.append(m_titles) -# blog_markdown_str = blog_with_research(blog_markdown_str, metaphor_search_result) -# logger.info(f"######## Blog content after EXA AI research: ########## \n\n{blog_markdown_str}\n\n") -# except Exception as err: -# logger.error(f"Failed to do Metaphor AI search: {err}") -# -# # Do Google trends analysis and combine with latest blog. -# try: -# pytrends_search_result = do_google_pytrends_analysis(search_keywords) -# logger.info(f"Google Trends keywords to use in the blog: {pytrends_search_result}\n") -# blog_markdown_str = blog_with_keywords(blog_markdown_str, pytrends_search_result) -# except Exception as err: -# logger.error(f"Failed to do Google Trends Analysis:{err}") -# logger.info(f"########### Blog Content After Google Trends Analysis:######### \n {blog_markdown_str}\n\n") + # Do Tavily AI research to augument the above blog. + try: + tavily_search_result, t_titles = do_tavily_ai_search(search_keywords) + example_blog_titles.append(t_titles) + blog_markdown_str = blog_with_research(blog_markdown_str, tavily_search_result) + logger.info(f"######### Blog content after Tavily AI research: ######### \n\n{blog_markdown_str}\n\n") + except Exception as err: + logger.error(f"Failed to do Tavily AI research: {err}") + + try: + # Do Metaphor/Exa AI search. + metaphor_search_result, m_titles = do_metaphor_ai_research(search_keywords) + example_blog_titles.append(m_titles) + blog_markdown_str = blog_with_research(blog_markdown_str, metaphor_search_result) + logger.info(f"######## Blog content after EXA AI research: ########## \n\n{blog_markdown_str}\n\n") + except Exception as err: + logger.error(f"Failed to do Metaphor AI search: {err}") + + # Do Google trends analysis and combine with latest blog. + try: + pytrends_search_result = do_google_pytrends_analysis(search_keywords) + logger.info(f"Google Trends keywords to use in the blog: {pytrends_search_result}\n") + blog_markdown_str = blog_with_keywords(blog_markdown_str, pytrends_search_result) + except Exception as err: + logger.error(f"Failed to do Google Trends Analysis:{err}") + logger.info(f"########### Blog Content After Google Trends Analysis:######### \n {blog_markdown_str}\n\n") # Combine YOU.com RAG search with the latest blog content. #you_rag_result = get_rag_results(search_keywords) @@ -81,6 +81,9 @@ def write_blog_from_keywords(search_keywords, url=None): #blog_markdown_str = blog_with_research(blog_markdown_str, you_search_result) #logger.info(f"Final blog content: {blog_markdown_str}") + # Pass the content to remove obivious words used by AI. + blog_markdown_str = blog_humanize(blog_markdown_str) + # Pass the final content for proofreading. blog_markdown_str = blog_proof_editor(blog_markdown_str) blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(blog_markdown_str, diff --git a/lib/blog_postprocessing/humanize_blog.py b/lib/blog_postprocessing/humanize_blog.py new file mode 100644 index 00000000..ad6dff2d --- /dev/null +++ b/lib/blog_postprocessing/humanize_blog.py @@ -0,0 +1,34 @@ +import os +import sys + +from pathlib import Path +from dotenv import load_dotenv +load_dotenv(Path('../../.env')) + +from ..gpt_providers.gemini_pro_text import gemini_text_response +from ..gpt_providers.openai_text_gen import openai_chatgpt + + +def blog_humanize(blog_content): + """ Helper for blog proof reading. """ + gpt_provider = os.environ["GPT_PROVIDER"] + + prompt = f"""As an expert content writer and editor, I will provide you with blog content. + + Your task is to replace all occurances of words given below: + ['It’s important to note', 'Delve into', 'Tapestry', 'Bustling', 'In summary', 'In conclusion', 'Unleash', 'Unveiling', 'ever-evolving', '', 'Remember that', 'Take a dive into', 'Navigating', 'Navigating the landscape', 'Navigating the complexities of', 'Landscape', 'The landscape of', 'Testament', 'a testament to', 'In the world of', 'Realm', 'Embark', 'virtuoso', 'Let's explore', 'symphony', 'Harnessing', 'Revolutionizing', 'Empower', 'game changing', 'ever-changing', 'Embrace', 'Embracing', 'game-changing', 'ever-evolving'] + + \n\nBlog Content: '{blog_content}' + """ + if 'openai' in gpt_provider.lower(): + try: + response = openai_chatgpt(prompt) + return response + except Exception as err: + SystemError(f"Openai Error Blog Proof Reading: {err}") + elif 'google' in gpt_provider.lower(): + try: + response = gemini_text_response(prompt) + return response + except Exception as err: + SystemError(f"Gemini Error Blog Proof Reading: {err}") diff --git a/main_config b/main_config index 28083fd8..1823db3b 100644 --- a/main_config +++ b/main_config @@ -20,8 +20,8 @@ blog_demographic = "All" # informational, commercial, company, news, finance, competitor, programming, scholar etc blog_type = "Informational" -# German, Chinese, Arabic, Nepali, Hindi, Hindustani etc -blog_language = "Spanish" +# Spanish, German, Chinese, Arabic, Nepali, Hindi, Hindustani etc +blog_language = "English" # Specify the output format of the blog as: HTML, markdown, plaintext. Defaults to markdown. blog_output_format = "markdown"