WIP - Streamlit UI, Porting CLI

This commit is contained in:
ajaysi
2024-06-03 16:10:47 +05:30
parent f9599bd346
commit f372169daa
4 changed files with 73 additions and 28 deletions

View File

@@ -20,6 +20,7 @@ from .blog_from_google_serp import write_blog_google_serp, blog_with_research
from ..ai_web_researcher.you_web_reseacher import get_rag_results, search_ydc_index
from ..blog_metadata.get_blog_metadata import blog_metadata
from ..blog_postprocessing.save_blog_to_file import save_blog_to_file
from ..gpt_providers.text_to_image_generation.main_generate_image_from_prompt import generate_image
def write_blog_from_keywords(search_keywords, url=None):
@@ -80,22 +81,29 @@ def write_blog_from_keywords(search_keywords, url=None):
logger.error(f"Failed to do Tavily AI research: {err}")
status.update(label="🙎 Generating - Title, Meta Description, Tags, Categories for the content.")
blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(blog_markdown_str,
search_keywords, example_blog_titles)
generated_image_filepath = None
try:
blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(blog_markdown_str)
except Exception as err:
st.error(f"Failed to get blog metadata: {err}")
try:
generated_image_filepath = generate_image(f"{blog_title} + ' ' + {blog_meta_desc}")
except Exception as err:
st.error(f"Failed in Image generation: {err}")
saved_blog_to_file = save_blog_to_file(blog_markdown_str, blog_title, blog_meta_desc,
blog_tags, blog_categories, generated_image_filepath)
status.update(label=f"Saved the content in this file: {saved_blog_to_file}")
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
title: {blog_title}\n
categories: [{blog_categories}]\n
tags: [{blog_tags}]\n
Meta description: {blog_meta_desc.replace(":", "-")}\n
---------------------------------------------------------------------\n
""")
logger.info(f"\n\n --------- Finished writing Blog for : {search_keywords} -------------- \n")
st.markdown(f"{blog_frontmatter}\n\n{blog_markdown_str}")
st.markdown(f"{blog_frontmatter"}
st.image(generated_image_filepath)
st.markdown(f"{blog_markdown_str}")
status.update(label=f"Finished, Review & Use your Original Content Below: {saved_blog_to_file}")

View File

@@ -1,26 +1,58 @@
import sys
from .get_blog_meta_desc import generate_blog_description
from .get_tags import get_blog_tags
from .get_blog_category import get_blog_categories
from .get_blog_title import generate_blog_title
import configparser
import json
from loguru import logger
logger.remove()
logger.add(sys.stdout,
colorize=True,
format="<level>{level}</level>|<green>{file}:{line}:{function}</green>| {message}"
)
)
from ..gpt_providers.text_generation.main_text_generation import llm_text_gen
def blog_metadata(blog_content, search_keywords=None, blog_titles=None):
def blog_metadata(blog_content):
""" Common function to get blog metadata """
blog_title = generate_blog_title(blog_content, search_keywords, blog_titles)
blog_meta_desc = generate_blog_description(blog_content)
logger.info(f"The blog meta description is: {blog_meta_desc}\n")
blog_tags = get_blog_tags(blog_content)
logger.info(f"Blog tags for generated content: {blog_tags}")
blog_categories = get_blog_categories(blog_content)
logger.info(f"Generated blog categories: {blog_categories}\n")
logger.info(f"Generating Content MetaData\n")
return(blog_title, blog_meta_desc, blog_tags, blog_categories)
blog_metadata_prompt = """
As an expert SEO and content writer, I will provide you with blog content.
1. Suggest only 2 blog categories which are most relevant to the provided blog content, by identifying the main topic.
Also consider the target audience and the blog's category taxonomy. Only reply with comma-separated values.
2. Compose a compelling meta description for the given blog content, adhering to SEO best practices.
Keep it between 150-160 characters. Provide a glimpse of the content's value to entice readers.
Respond with only one of your best efforts and do not include your explanations.
3. Write 1 blog title following SEO best practices. Please keep the title concise, not exceeding 60 words.
Respond with only 1 title and no explanations. Negative Keywords: Unveiling, unleash, power of. Don't use such words in your title.
4. Suggest only 2 relevant and specific blog tags for the given blog content. Only reply with comma-separated values.
The blog content is: '{blog_article}'
Please provide the result in the following JSON format:
{
"title": "Your generated blog title",
"meta_description": "Your generated meta description",
"tags": ["tag1", "tag2"],
"categories": ["category1", "category2"]
}
"""
try:
response = llm_text_gen(blog_metadata_prompt)
""" Cleans the response by removing ``` and 'json' strings """
result_json = response.replace("```", "").replace("json", "").strip()
# Convert the cleaned response to JSON
result_json = json.loads(result_json)
except Exception as err:
logger.error(f"Failed to get response from LLM: {err}")
raise err
# Extract the data from the JSON response
blog_title = result_json.get("title")
blog_meta_desc = result_json.get("meta_description")
blog_tags = result_json.get("tags")
blog_categories = result_json.get("categories")
return blog_title, blog_meta_desc, blog_tags, blog_categories

View File

@@ -45,7 +45,7 @@ def generate_stable_diffusion_image(prompt):
raise Exception("Non-200 response: " + str(response.text))
data = response.json()
save_generated_image(data)
img_path = save_generated_image(data)
for i, image in enumerate(data["artifacts"]):
# Decode base64 image data
@@ -53,4 +53,6 @@ def generate_stable_diffusion_image(prompt):
# Open image using PIL
img = Image.open(BytesIO(img_data))
# Display the image
img.show()
img.show()
return img_path

View File

@@ -26,7 +26,7 @@ from .gen_stabl_diff_img import generate_stable_diffusion_image
from ..text_generation.main_text_generation import llm_text_gen
def generate_image(user_prompt, image_engine):
def generate_image(user_prompt):
"""
The generation API endpoint creates an image based on a text prompt.
@@ -42,12 +42,15 @@ def generate_image(user_prompt, image_engine):
Must be one of "url" or "b64_json". Defaults to "url".
--> user (str): A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
"""
# FIXME: Need to remove default value to match sidebar input.
image_engine = 'Stability-AI'
try:
img_prompt = generate_img_prompt(user_prompt)
if 'Dalle3' in image_engine:
logger.info(f"Calling Dalle3 text-to-image with prompt: {img_prompt}")
image_stored_at = generate_dalle3_images(img_prompt)
elif 'Stability-Stable-Diffusion' in image_engine:
elif 'Stability-AI' in image_engine:
logger.info(f"Calling Stable diffusion text-to-image with prompt: \n{img_prompt}")
print("\n\n")
image_stored_at = generate_stable_diffusion_image(img_prompt)