WIP - Streamlit UI, Porting CLI
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import configparser
|
||||
from pathlib import Path
|
||||
|
||||
from loguru import logger
|
||||
logger.remove()
|
||||
@@ -13,26 +13,27 @@ logger.add(sys.stdout,
|
||||
from ..gpt_providers.text_generation.main_text_generation import llm_text_gen
|
||||
|
||||
|
||||
# FIXME: Provide num_blogs, num_faqs as inputs.
|
||||
def write_blog_google_serp(search_keyword, search_results):
|
||||
"""Combine the given online research and gpt blog content"""
|
||||
"""Combine the given online research and GPT blog content"""
|
||||
try:
|
||||
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'main_config'))
|
||||
config = configparser.ConfigParser()
|
||||
config.read(config_path, encoding='utf-8')
|
||||
config_path = Path(os.environ["ALWRITY_CONFIG"])
|
||||
with open(config_path, 'r', encoding='utf-8') as file:
|
||||
config = json.load(file)
|
||||
except Exception as err:
|
||||
print(f"Error: Failed to read values from config: {err}")
|
||||
logger.error(f"Error: Failed to read values from config: {err}")
|
||||
exit(1)
|
||||
|
||||
blog_characteristics = config['Blog Content Characteristics']
|
||||
|
||||
prompt = f"""
|
||||
As expert Creative Content writer,
|
||||
I want you to write {config.get('blog_characteristics', 'blog_type')} blog post,
|
||||
I want you to write {blog_characteristics['Blog Type']} blog post,
|
||||
that explores {search_keyword} and also include 5 FAQs.
|
||||
|
||||
Below are the guidelines to follow:
|
||||
1). You must respond in {config.get('blog_characteristics', 'blog_language')} language.
|
||||
2). Tone and Brand Alignment: Adjust your tone, voice, personality for {config.get('blog_characteristics', 'blog_tone')} audience.
|
||||
3). Make sure your response content length is of {config.get('blog_characteristics', 'blog_length')} words.
|
||||
1). You must respond in {blog_characteristics['Blog Language']} language.
|
||||
2). Tone and Brand Alignment: Adjust your tone, voice, personality for {blog_characteristics['Blog Tone']} audience.
|
||||
3). Make sure your response content length is of {blog_characteristics['Blog Length']} words.
|
||||
4). Include FAQs from 'People also Ask' section of provided context 'google search result'.
|
||||
|
||||
I want the post to offer unique insights, relatable examples, and a fresh perspective on the topic.
|
||||
@@ -40,7 +41,9 @@ def write_blog_google_serp(search_keyword, search_results):
|
||||
\n\n
|
||||
\"\"\"{search_results}\"\"\"
|
||||
"""
|
||||
|
||||
logger.info("Generating blog and FAQs from Google web search results.")
|
||||
|
||||
try:
|
||||
response = llm_text_gen(prompt)
|
||||
return response
|
||||
@@ -48,7 +51,6 @@ def write_blog_google_serp(search_keyword, search_results):
|
||||
logger.error(f"Exit: Failed to get response from LLM: {err}")
|
||||
exit(1)
|
||||
|
||||
|
||||
def improve_blog_intro(blog_content, blog_intro):
|
||||
"""Combine the given online research and gpt blog content"""
|
||||
prompt = f"""
|
||||
|
||||
@@ -44,9 +44,11 @@ def write_blog_from_keywords(search_keywords, url=None):
|
||||
|
||||
status.update(label=f"🛀 Starting Tavily AI research: {search_keywords}")
|
||||
tavily_search_result, t_titles, t_answer = do_tavily_ai_search(search_keywords)
|
||||
status.update(label=f"🙆 Finished Google Search & Tavily AI Search on: {search_keywords}", expanded=False)
|
||||
status.update(label=f"🙆 Finished Google Search & Tavily AI Search on: {search_keywords}",
|
||||
state="complete", expanded=False)
|
||||
|
||||
except Exception as err:
|
||||
st.error(f"Failed in web research: {err}")
|
||||
logger.error(f"Failed in web research: {err}")
|
||||
|
||||
with st.status("Started Writing blog from google search..", expanded=True) as status:
|
||||
@@ -56,12 +58,9 @@ def write_blog_from_keywords(search_keywords, url=None):
|
||||
status.update(label=f"🛀 Writing blog from Google Search on: {search_keywords}")
|
||||
blog_markdown_str = write_blog_google_serp(search_keywords, google_search_result)
|
||||
st.markdown(blog_markdown_str)
|
||||
|
||||
# Hate the robotic introductions.
|
||||
#blog_markdown_str = improve_blog_intro(blog_markdown_str, t_answer)
|
||||
#st.markdown(blog_markdown_str)
|
||||
status.update(label="🙎 Draft 1: Your Content from Google search result.", expanded=False)
|
||||
status.update(label="🙎 Draft 1: Your Content from Google search result.", state="complete", expanded=False)
|
||||
except Exception as err:
|
||||
st.error(f"Failed in Google web research: {err}")
|
||||
logger.error(f"Failed in Google web research: {err}")
|
||||
|
||||
# logger.info/check the final blog content.
|
||||
|
||||
@@ -60,22 +60,26 @@ def long_form_generator(content_keywords):
|
||||
"""
|
||||
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:
|
||||
try:
|
||||
status.update(label=f"Starting to write content on {content_keywords}.")
|
||||
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')
|
||||
content_tone, target_audience, content_type, content_language, output_format, content_length = read_return_config_section('blog_characteristics')
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to Read config params from main_config: {err}")
|
||||
return
|
||||
st.error(f"Failed to Read config params from main_config: {err}")
|
||||
return False
|
||||
|
||||
try:
|
||||
filepath = os.path.join(os.environ["PROMPTS_DIR"], "long_form_ai_writer.prompts")
|
||||
status.update(label=f"Reading Prompts from {filepath}.")
|
||||
# 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:
|
||||
st.error(f"Exit: Failed to read prompts from {filepath}: {err}")
|
||||
logger.error(f"Exit: Failed to read prompts from {filepath}: {err}")
|
||||
exit(1)
|
||||
|
||||
@@ -147,11 +151,12 @@ def long_form_generator(content_keywords):
|
||||
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.")
|
||||
status.update(label=f"Completed with Content Outline.")
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to generate content outline: {err}")
|
||||
|
||||
try:
|
||||
status.update(label=f"Do web research with Tavily to provide context for content creation.")
|
||||
logger.info("Do web research with Tavily to provide context for content creation.")
|
||||
# Do Metaphor/Exa AI search.
|
||||
table_data = []
|
||||
@@ -163,6 +168,7 @@ def long_form_generator(content_keywords):
|
||||
web_research_result = table_data
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to do Tavily AI search: {err}")
|
||||
st.error(f"Failed to do Tavily AI search: {err}")
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -172,6 +178,7 @@ def long_form_generator(content_keywords):
|
||||
web_research_result=web_research_result,
|
||||
writing_guidelines=writing_guidelines)).text
|
||||
except Exception as err:
|
||||
st.error(f"Failed to Generate Starting draft: {err}")
|
||||
logger.error(f"Failed to Generate Starting draft: {err}")
|
||||
return
|
||||
|
||||
@@ -194,10 +201,11 @@ def long_form_generator(content_keywords):
|
||||
logger.error(f"Failed as: {err} and {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")
|
||||
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.
|
||||
Do not explain your response, provide 8 google search sentences encompassing the given content outline.
|
||||
Provide the search term results as comma separated values.\n\n
|
||||
Content Outline:\n
|
||||
'{content_outline}'
|
||||
@@ -227,26 +235,36 @@ def long_form_generator(content_keywords):
|
||||
# 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:
|
||||
st.error(f"Failed to continually write the Essay: {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")
|
||||
status.update(label="Success: Finished writing Long form content.")
|
||||
|
||||
# FIXME: The current implementation is suited for normal length content.
|
||||
# In long content sending the whole content for each content metadata is expensive.
|
||||
# blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(final,
|
||||
# content_keywords, m_titles)
|
||||
# status.update(label="Success: Finished with Title, Meta Description, Tags, categories")
|
||||
# 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 = dedent(f"""
|
||||
# \n---------------------------------------------------------------------
|
||||
# title: {blog_title.strip()}\n
|
||||
# categories: [{blog_categories.strip()}]\n
|
||||
# tags: [{blog_tags.strip()}]\n
|
||||
# Meta description: {blog_meta_desc.replace(":", "-").strip()}\n
|
||||
# ---------------------------------------------------------------------\n
|
||||
# """)
|
||||
#
|
||||
# logger.info(f"\n{blog_frontmatter}{final}\n\n")
|
||||
# st.markdown(f"\n{blog_frontmatter}{final}\n\n")
|
||||
logger.info(f"\n{final}\n\n")
|
||||
|
||||
logger.info(f"\n\n ################ Finished writing Blog for : {content_keywords} #################### \n")
|
||||
with st.expander("**Click to View the final content draft:**"):
|
||||
st.markdown(f"\n{final}\n\n")
|
||||
|
||||
Reference in New Issue
Block a user