import streamlit as st from crewai import Agent, Task, Crew, Process from crewai_tools import WebsiteSearchTool from dotenv import load_dotenv import os # Load environment variables load_dotenv() # Streamlit App st.title("Blog Generator with AI") st.sidebar.header("Input") # OpenAI API key input user_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password") news_link = st.sidebar.text_input("Enter News Link", "") # User inputs for agent configurations st.sidebar.subheader("Configure Agents") blog_researcher_role = st.sidebar.text_input("Researcher Role", "AI Blog Researcher from News Website") blog_researcher_goal = st.sidebar.text_area("Researcher Goal", "Get the relevant latest AI related news from News Website") blog_writer_role = st.sidebar.text_input("Writer Role", "Blog Writer") blog_writer_goal = st.sidebar.text_area("Writer Goal", "Narrate compelling tech stories about the News Article and add the reference links at the end of the blog.") generate_blog = st.sidebar.button("Generate Blog") if generate_blog: if user_api_key: # Set the OpenAI API key dynamically os.environ["OPENAI_API_KEY"] = user_api_key if news_link: st.info("Fetching and processing the news...") # Define tools web_tool = WebsiteSearchTool() # Create agents blog_researcher = Agent( role=blog_researcher_role, goal=blog_researcher_goal, verbose=True, memory=True, backstory=("Expert in understanding videos in AI, Data Science, Machine Learning, and GEN AI."), tools=[web_tool], allow_delegation=True ) blog_writer = Agent( role=blog_writer_role, goal=blog_writer_goal, verbose=True, memory=True, backstory=( "With a flair for simplifying complex topics, you craft engaging narratives that captivate and educate," "bringing new discoveries to light in an accessible manner." ), tools=[web_tool], allow_delegation=False ) # Define tasks research_task = Task( description=( "Identify the News Article and get detailed information about the News from the website." ), expected_output='A comprehensive 3-paragraph-long report based on the {topic} of News.', tools=[web_tool], agent=blog_researcher, ) write_task = Task( description=( "Get the info from the News Website on the topic {topic}." ), expected_output='Summarize the info from the News website on the topic {topic} and create the content for the blog.', tools=[web_tool], agent=blog_writer, async_execution=False, output_file="" # Provide an empty string or valid file path ) # Create crew crew = Crew( agents=[blog_researcher, blog_writer], tasks=[research_task, write_task], process=Process.sequential, memory=True, cache=True, max_rpm=100, share_crew=True ) # Kickoff the process and fetch the result try: with st.spinner("Processing tasks..."): result = crew.kickoff(inputs={'topic': news_link}) # Access task outputs try: task_outputs = result.tasks_output except AttributeError: st.error("The result object does not have 'tasks_output'.") task_outputs = [] # Extract blog content try: blog_content = task_outputs[1].raw except (IndexError, AttributeError): blog_content = "Unable to fetch blog content from the task outputs." # Display the blog content st.subheader("Generated Blog") st.text_area("Blog Content", value=blog_content, height=400) # Actions to save the blog st.subheader("Actions") save_blog = st.button("Save Blog Locally") if save_blog: try: # Save Markdown content to a local file file_path = "generated_blog.md" with open(file_path, "w") as markdown_file: markdown_file.write(blog_content) st.success(f"Blog saved successfully as '{file_path}' on your local machine!") except Exception as e: st.error(f"Failed to save blog locally: {e}") except Exception as e: st.error(f"An error occurred during the process: {e}") else: st.error("Please enter a valid news link.") else: st.error("Please provide your OpenAI API Key.")