WIP - UI, Audio, firecrawl, long-form - V0.5
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
import sys
|
||||
import os
|
||||
import asyncio
|
||||
from textwrap import dedent
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import streamlit as st
|
||||
|
||||
from gtts import gTTS
|
||||
import base64
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv(Path('../../.env'))
|
||||
# Logger setup
|
||||
from loguru import logger
|
||||
logger.remove()
|
||||
logger.add(sys.stdout,
|
||||
colorize=True,
|
||||
format="<level>{level}</level>|<green>{file}:{line}:{function}</green>| {message}"
|
||||
)
|
||||
colorize=True,
|
||||
format="<level>{level}</level>|<green>{file}:{line}:{function}</green>| {message}")
|
||||
|
||||
from ..ai_web_researcher.gpt_online_researcher import do_google_serp_search,\
|
||||
do_tavily_ai_search, do_metaphor_ai_research, do_google_pytrends_analysis
|
||||
# Import other necessary modules
|
||||
from ..ai_web_researcher.gpt_online_researcher import (
|
||||
do_google_serp_search, do_tavily_ai_search,
|
||||
do_metaphor_ai_research, do_google_pytrends_analysis)
|
||||
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
|
||||
@@ -23,6 +29,21 @@ 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
|
||||
|
||||
|
||||
# Function to convert text to speech and save as an audio file
|
||||
def text_to_speech(text, lang='en'):
|
||||
tts = gTTS(text=text, lang=lang)
|
||||
tts.save("output.mp3")
|
||||
return "output.mp3"
|
||||
|
||||
|
||||
# Function to get audio file as a downloadable link
|
||||
def get_audio_file(audio_file):
|
||||
with open(audio_file, "rb") as file:
|
||||
data = file.read()
|
||||
b64_data = base64.b64encode(data).decode()
|
||||
return f'<a href="data:audio/mp3;base64,{b64_data}" download="output.mp3">Download audio file</a>'
|
||||
|
||||
|
||||
def write_blog_from_keywords(search_keywords, url=None):
|
||||
"""
|
||||
This function will take a blog Topic to first generate sections for it
|
||||
@@ -45,8 +66,8 @@ 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}",
|
||||
state="complete", 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}")
|
||||
@@ -66,21 +87,21 @@ def write_blog_from_keywords(search_keywords, url=None):
|
||||
|
||||
# logger.info/check the final blog content.
|
||||
logger.info("######### Draft1: Finished Blog from Google web search: ###########")
|
||||
|
||||
|
||||
with st.status("Started Writing blog from Tavily Web search..", expanded=True) as status:
|
||||
# Do Tavily AI research to augument the above blog.
|
||||
# Do Tavily AI research to augment the above blog.
|
||||
try:
|
||||
#example_blog_titles.append(t_titles)
|
||||
# example_blog_titles.append(t_titles)
|
||||
if blog_markdown_str and tavily_search_result:
|
||||
logger.info(f"\n\n######### Blog content after Tavily AI research: ######### \n\n")
|
||||
blog_markdown_str = write_blog_google_serp(search_keywords, tavily_search_result)
|
||||
status.update(label="Finished Writing Blog From Tavily Results:{blog_markdown_str}", expanded=True)
|
||||
status.update(label=f"Finished Writing Blog From Tavily Results:{blog_markdown_str}", expanded=True)
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to do Tavily AI research: {err}")
|
||||
|
||||
status.update(label="🙎 Generating - Title, Meta Description, Tags, Categories for the content.", expanded=True)
|
||||
try:
|
||||
blog_title, blog_meta_desc, blog_tags, blog_categories = blog_metadata(blog_markdown_str)
|
||||
blog_title, blog_meta_desc, blog_tags, blog_categories = asyncio.run(blog_metadata(blog_markdown_str))
|
||||
except Exception as err:
|
||||
st.error(f"Failed to get blog metadata: {err}")
|
||||
|
||||
@@ -94,38 +115,21 @@ def write_blog_from_keywords(search_keywords, url=None):
|
||||
except Exception as err:
|
||||
st.warning(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)
|
||||
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}")
|
||||
logger.info(f"\n\n --------- Finished writing Blog for : {search_keywords} -------------- \n")
|
||||
|
||||
# Render the result on streamlit UI
|
||||
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}", state="complete")
|
||||
|
||||
# Display options below the content
|
||||
col1, col2, col3, col4, col5 = st.columns(5)
|
||||
if col1.button('Copy'):
|
||||
pyperclip.copy(blog_markdown_str)
|
||||
st.success("Text copied to clipboard!")
|
||||
|
||||
if col2.button('Rephrase'):
|
||||
rephrased_text = rephrase_text(blog_markdown_str)
|
||||
st.markdown(rephrased_text)
|
||||
|
||||
if col3.button('Change Tone'):
|
||||
tone = st.selectbox("Select Tone", ["Formal", "Casual", "Professional"])
|
||||
if st.button("Apply Tone"):
|
||||
toned_text = change_tone(blog_markdown_str, tone)
|
||||
st.markdown(toned_text)
|
||||
|
||||
if col4.button('Make Shorter'):
|
||||
shorter_text = make_shorter(blog_markdown_str)
|
||||
st.markdown(shorter_text)
|
||||
|
||||
if col5.button('Translate'):
|
||||
language = st.selectbox("Select Language", ["Spanish", "French", "German"])
|
||||
if st.button("Translate"):
|
||||
translated_text = translate_text(blog_markdown_str, language)
|
||||
st.markdown(translated_text)
|
||||
# Render the result on streamlit UI
|
||||
if generated_image_filepath:
|
||||
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}",
|
||||
state="complete")
|
||||
|
||||
# Passing the text and language to the engine, here we have marked slow=False. Which tells
|
||||
# the module that the converted audio should have a high speed
|
||||
tts = gTTS(text=blog_markdown_str, lang='en', slow=False)
|
||||
# Saving the converted audio in a mp3 file
|
||||
tts.save("delete_me.mp3")
|
||||
st.audio("delete_me.mp3")
|
||||
|
||||
Reference in New Issue
Block a user