Streamlit UI, Porting CLI - WIP
This commit is contained in:
@@ -13,6 +13,7 @@ import yaml
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
from configparser import ConfigParser
|
||||
import streamlit as st
|
||||
|
||||
from google.api_core import retry
|
||||
import google.generativeai as genai
|
||||
@@ -57,190 +58,195 @@ def long_form_generator(content_keywords):
|
||||
Write long form content using prompt chaining and iterative generation.
|
||||
Parameters:
|
||||
"""
|
||||
# Read the main_config to define tone, character, personality of the content to be generated.
|
||||
try:
|
||||
logger.info(f"Starting to write content on {content_keywords}.")
|
||||
# Define persona and writing guidelines
|
||||
content_tone, target_audience, content_type, content_language, output_format = read_return_config_section('blog_characteristics')
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to Read config params from main_config: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
filepath = os.path.join(os.environ["PROMPTS_DIR"], "long_form_ai_writer.prompts")
|
||||
# Check if file exists
|
||||
if not os.path.exists(filepath):
|
||||
raise FileNotFoundError(f"File {filepath} does not exist")
|
||||
with open(filepath, 'r') as file:
|
||||
prompts = yaml.safe_load(file)
|
||||
except Exception as err:
|
||||
logger.error(f"Exit: Failed to read prompts from {filepath}: {err}")
|
||||
exit(1)
|
||||
|
||||
writing_guidelines = prompts.get('writing_guidelines').format(
|
||||
content_language=content_language,
|
||||
content_tone=content_tone,
|
||||
content_type=content_type,
|
||||
output_format=output_format,
|
||||
content_keywords=content_keywords,
|
||||
target_audience=target_audience
|
||||
)
|
||||
|
||||
content_title = prompts.get('content_title').format(
|
||||
content_language=content_language,
|
||||
content_keywords=content_keywords,
|
||||
target_audience=target_audience
|
||||
)
|
||||
with st.status("Start Writing Long Form Article, Hold my Beer..", expanded=True) as status:
|
||||
# Read the main_config to define tone, character, personality of the content to be generated.
|
||||
try:
|
||||
logger.info(f"Starting to write content on {content_keywords}.")
|
||||
# Define persona and writing guidelines
|
||||
content_tone, target_audience, content_type, content_language, output_format = read_return_config_section('blog_characteristics')
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to Read config params from main_config: {err}")
|
||||
return
|
||||
|
||||
content_outline = prompts.get('content_outline').format(
|
||||
content_language=content_language,
|
||||
content_title='{content_title}',
|
||||
content_type=content_type,
|
||||
target_audience=target_audience
|
||||
)
|
||||
|
||||
starting_prompt = prompts.get('starting_prompt').format(
|
||||
content_language=content_language,
|
||||
content_title='{content_title}',
|
||||
content_outline='{content_outline}',
|
||||
writing_guidelines=writing_guidelines
|
||||
)
|
||||
|
||||
continuation_prompt = prompts.get('continuation_prompt').format(
|
||||
content_language=content_language,
|
||||
content_title='{content_title}',
|
||||
content_outline='{content_outline}',
|
||||
content_text='{content_text}',
|
||||
web_research_result='{web_research_result}',
|
||||
writing_guidelines=writing_guidelines
|
||||
)
|
||||
|
||||
# Configure generative AI
|
||||
load_dotenv(Path('../.env'))
|
||||
generation_config = {
|
||||
"temperature": 0.8,
|
||||
"top_p": 0.95,
|
||||
"max_output_tokens": 8192,
|
||||
}
|
||||
|
||||
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
||||
# Initialize the generative model
|
||||
model = genai.GenerativeModel('gemini-pro', generation_config=generation_config)
|
||||
model_pro = genai.GenerativeModel('gemini-1.5-flash-latest', generation_config=generation_config)
|
||||
|
||||
# Do SERP web research for given keywords to generate title and outline.
|
||||
web_research_result, g_titles = do_google_serp_search(content_keywords)
|
||||
|
||||
# Generate prompts
|
||||
try:
|
||||
content_title = generate_with_retry(model_pro, content_title.format(web_research_result=web_research_result)).text
|
||||
logger.info(f"The title of the content is: {content_title}")
|
||||
except Exception as err:
|
||||
logger.error(f"Content title Generation Error: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
content_outline = generate_with_retry(model_pro, content_outline.format(
|
||||
content_title=content_title,
|
||||
web_research_result=web_research_result)).text
|
||||
logger.info(f"The content Outline is: {content_outline}\n\n")
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to generate content outline: {err}")
|
||||
|
||||
try:
|
||||
logger.info("Do web research with Tavily to provide context for content creation.")
|
||||
# Do Metaphor/Exa AI search.
|
||||
table_data = []
|
||||
web_research_result, m_titles, t_titles = do_tavily_ai_search(content_keywords, max_results=5)
|
||||
for item in web_research_result.get("results"):
|
||||
title = item.get("title", "")
|
||||
snippet = item.get("content", "")
|
||||
table_data.append([title, snippet])
|
||||
web_research_result = table_data
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to do Tavily AI search: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
starting_draft = generate_with_retry(model_pro, starting_prompt.format(
|
||||
content_title=content_title,
|
||||
content_outline=content_outline,
|
||||
web_research_result=web_research_result,
|
||||
writing_guidelines=writing_guidelines)).text
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to Generate Starting draft: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
logger.info(f"Starting to write on the outline introduction.")
|
||||
draft = starting_draft
|
||||
continuation = generate_with_retry(model, continuation_prompt.format(
|
||||
content_title=content_title,
|
||||
content_outline=content_outline,
|
||||
content_text=draft,
|
||||
web_research_result=web_research_result,
|
||||
writing_guidelines=writing_guidelines)).text
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to write the initial draft: {err}")
|
||||
|
||||
# Add the continuation to the initial draft, keep building the story until we see 'IAMDONE'
|
||||
try:
|
||||
draft += '\n\n' + continuation
|
||||
except Exception as err:
|
||||
logger.error(f"Failed as: {err} and {continuation}")
|
||||
|
||||
logger.info(f"Writing in progress... Current draft length: {len(draft)} characters")
|
||||
search_terms = f"""
|
||||
I will provide you with blog outline, your task is to read the outline & return 3 google search keywords.
|
||||
Your response will be used to do web research for writing on the given outline.
|
||||
Do not explain your response, provide 3 google search sentences encompassing the given content outline.
|
||||
|
||||
Content Outline:\n\n
|
||||
{content_outline}
|
||||
"""
|
||||
search_words = generate_with_retry(model_pro, search_terms).text
|
||||
|
||||
while 'IAMDONE' not in continuation:
|
||||
try:
|
||||
#web_research_result, m_titles = do_metaphor_ai_research(content_keywords)
|
||||
str_list = re.split(r',\s*', search_words)
|
||||
# Strip quotes from each element
|
||||
str_list = [s.strip('\'"') for s in str_list]
|
||||
for search_term in str_list:
|
||||
web_research_result, m_titles, t_titles = do_tavily_ai_search(search_term, max_results=5)
|
||||
|
||||
filepath = os.path.join(os.environ["PROMPTS_DIR"], "long_form_ai_writer.prompts")
|
||||
# Check if file exists
|
||||
if not os.path.exists(filepath):
|
||||
raise FileNotFoundError(f"File {filepath} does not exist")
|
||||
with open(filepath, 'r') as file:
|
||||
prompts = yaml.safe_load(file)
|
||||
except Exception as err:
|
||||
logger.error(f"Exit: Failed to read prompts from {filepath}: {err}")
|
||||
exit(1)
|
||||
|
||||
writing_guidelines = prompts.get('writing_guidelines').format(
|
||||
content_language=content_language,
|
||||
content_tone=content_tone,
|
||||
content_type=content_type,
|
||||
output_format=output_format,
|
||||
content_keywords=content_keywords,
|
||||
target_audience=target_audience
|
||||
)
|
||||
|
||||
content_title = prompts.get('content_title').format(
|
||||
content_language=content_language,
|
||||
content_keywords=content_keywords,
|
||||
target_audience=target_audience
|
||||
)
|
||||
|
||||
content_outline = prompts.get('content_outline').format(
|
||||
content_language=content_language,
|
||||
content_title='{content_title}',
|
||||
content_type=content_type,
|
||||
target_audience=target_audience
|
||||
)
|
||||
|
||||
starting_prompt = prompts.get('starting_prompt').format(
|
||||
content_language=content_language,
|
||||
content_title='{content_title}',
|
||||
content_outline='{content_outline}',
|
||||
writing_guidelines=writing_guidelines
|
||||
)
|
||||
|
||||
continuation_prompt = prompts.get('continuation_prompt').format(
|
||||
content_language=content_language,
|
||||
content_title='{content_title}',
|
||||
content_outline='{content_outline}',
|
||||
content_text='{content_text}',
|
||||
web_research_result='{web_research_result}',
|
||||
writing_guidelines=writing_guidelines
|
||||
)
|
||||
|
||||
# Configure generative AI
|
||||
load_dotenv(Path('../.env'))
|
||||
generation_config = {
|
||||
"temperature": 0.6,
|
||||
"top_p": 1,
|
||||
"max_output_tokens": 4096,
|
||||
}
|
||||
|
||||
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
||||
# Initialize the generative model
|
||||
model = genai.GenerativeModel('gemini-pro', generation_config=generation_config)
|
||||
model_pro = genai.GenerativeModel('gemini-1.5-flash-latest', generation_config=generation_config)
|
||||
|
||||
# Do SERP web research for given keywords to generate title and outline.
|
||||
web_research_result, g_titles = do_google_serp_search(content_keywords)
|
||||
|
||||
# Generate prompts
|
||||
try:
|
||||
content_title = generate_with_retry(model_pro, content_title.format(web_research_result=web_research_result)).text
|
||||
logger.info(f"The title of the content is: {content_title}")
|
||||
status.update(label=f"The title of the content is: {content_title}")
|
||||
except Exception as err:
|
||||
logger.error(f"Content title Generation Error: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
content_outline = generate_with_retry(model_pro, content_outline.format(
|
||||
content_title=content_title,
|
||||
web_research_result=web_research_result)).text
|
||||
logger.info(f"The content Outline is: {content_outline}\n\n")
|
||||
status.update(label="Generated the content outline.")
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to generate content outline: {err}")
|
||||
|
||||
try:
|
||||
logger.info("Do web research with Tavily to provide context for content creation.")
|
||||
# Do Metaphor/Exa AI search.
|
||||
table_data = []
|
||||
web_research_result, m_titles, t_titles = do_tavily_ai_search(content_keywords, max_results=5)
|
||||
for item in web_research_result.get("results"):
|
||||
title = item.get("title", "")
|
||||
snippet = item.get("content", "")
|
||||
table_data.append([title, snippet])
|
||||
web_research_result = table_data
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to do Tavily AI search: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
starting_draft = generate_with_retry(model_pro, starting_prompt.format(
|
||||
content_title=content_title,
|
||||
content_outline=content_outline,
|
||||
web_research_result=web_research_result,
|
||||
writing_guidelines=writing_guidelines)).text
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to Generate Starting draft: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
logger.info(f"Starting to write on the outline introduction.")
|
||||
draft = starting_draft
|
||||
continuation = generate_with_retry(model, continuation_prompt.format(
|
||||
content_title=content_title,
|
||||
content_title=content_title,
|
||||
content_outline=content_outline,
|
||||
content_text=draft,
|
||||
web_research_result=web_research_result,
|
||||
writing_guidelines=writing_guidelines)).text
|
||||
|
||||
draft += '\n\n' + continuation
|
||||
logger.info(f"Writing in progress... Current draft length: {len(draft)} characters")
|
||||
|
||||
# At this point, the context is little stale. We should more web research on
|
||||
# related queries as per the content outline, to augment the LLM context.
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to continually write the Essay: {err}")
|
||||
return
|
||||
logger.error(f"Failed to write the initial draft: {err}")
|
||||
|
||||
# Remove 'IAMDONE' and print the final story
|
||||
final = draft.replace('IAMDONE', '').strip()
|
||||
|
||||
blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(final,
|
||||
content_keywords, m_titles)
|
||||
|
||||
generated_image_filepath = None
|
||||
# TBD: Save the blog content as a .md file. Markdown or HTML ?
|
||||
save_blog_to_file(final, blog_title, blog_meta_desc, blog_tags, blog_categories, generated_image_filepath)
|
||||
|
||||
blog_frontmatter = f"""
|
||||
---
|
||||
title: {blog_title}
|
||||
categories: [{blog_categories}]
|
||||
tags: [{blog_tags}]
|
||||
Meta description: {blog_meta_desc.replace(":", "-")}
|
||||
---"""
|
||||
logger.info(f"\n{blog_frontmatter}{final}\n\n")
|
||||
logger.info(f"\n\n ################ Finished writing Blog for : {content_keywords} #################### \n")
|
||||
# Add the continuation to the initial draft, keep building the story until we see 'IAMDONE'
|
||||
try:
|
||||
draft += '\n\n' + continuation
|
||||
except Exception as err:
|
||||
logger.error(f"Failed as: {err} and {continuation}")
|
||||
|
||||
logger.info(f"Writing in progress... Current draft length: {len(draft)} characters")
|
||||
search_terms = f"""
|
||||
I will provide you with blog outline, your task is to read the outline & return 3 google search keywords.
|
||||
Your response will be used to do web research for writing on the given outline.
|
||||
Do not explain your response, provide 3 google search sentences encompassing the given content outline.
|
||||
Provide the search term results as comma separated values.\n\n
|
||||
Content Outline:\n
|
||||
'{content_outline}'
|
||||
"""
|
||||
search_words = generate_with_retry(model_pro, search_terms).text
|
||||
status.update(label=f"Search terms from written draft: {search_words}")
|
||||
|
||||
while 'IAMDONE' not in continuation:
|
||||
try:
|
||||
#web_research_result, m_titles = do_metaphor_ai_research(content_keywords)
|
||||
str_list = re.split(r',\s*', search_words)
|
||||
# Strip quotes from each element
|
||||
str_list = [s.strip('\'"') for s in str_list]
|
||||
for search_term in str_list:
|
||||
web_research_result, m_titles, t_titles = do_tavily_ai_search(search_term, max_results=5)
|
||||
|
||||
continuation = generate_with_retry(model, continuation_prompt.format(
|
||||
content_title=content_title,
|
||||
content_outline=content_outline,
|
||||
content_text=draft,
|
||||
web_research_result=web_research_result,
|
||||
writing_guidelines=writing_guidelines)).text
|
||||
|
||||
draft += '\n\n' + continuation
|
||||
logger.info(f"Writing in progress... Current draft length: {len(draft)} characters")
|
||||
status.update(label=f"Writing in progress... Current draft length: {len(draft)} characters")
|
||||
# At this point, the context is little stale. We should more web research on
|
||||
# related queries as per the content outline, to augment the LLM context.
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to continually write the Essay: {err}")
|
||||
return
|
||||
|
||||
# Remove 'IAMDONE' and print the final story
|
||||
final = draft.replace('IAMDONE', '').strip()
|
||||
|
||||
blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(final,
|
||||
content_keywords, m_titles)
|
||||
|
||||
generated_image_filepath = None
|
||||
# TBD: Save the blog content as a .md file. Markdown or HTML ?
|
||||
save_blog_to_file(final, blog_title, blog_meta_desc, blog_tags, blog_categories, generated_image_filepath)
|
||||
|
||||
blog_frontmatter = f"""
|
||||
---
|
||||
title: {blog_title}
|
||||
categories: [{blog_categories}]
|
||||
tags: [{blog_tags}]
|
||||
Meta description: {blog_meta_desc.replace(":", "-")}
|
||||
---"""
|
||||
logger.info(f"\n{blog_frontmatter}{final}\n\n")
|
||||
st.write(f"\n{blog_frontmatter}{final}\n\n")
|
||||
logger.info(f"\n\n ################ Finished writing Blog for : {content_keywords} #################### \n")
|
||||
|
||||
Reference in New Issue
Block a user