Prathamesh1420's picture
Update app.py
95eb3ba verified
import streamlit as st
from crewai import Agent, Task, Crew
from crewai_tools import YoutubeChannelSearchTool
from langchain_google_genai import ChatGoogleGenerativeAI
from dotenv import load_dotenv
import os
import nest_asyncio
# Load environment variables
load_dotenv()
# Apply nest_asyncio to allow nested event loops
nest_asyncio.apply()
# Initialize Google Gemini AI
llm = ChatGoogleGenerativeAI(
api_key=os.getenv('GOOGLE_API_KEY'),
model="models/gemini-pro" # Replace with the correct model name
)
# Initialize the YoutubeChannelSearchTool without OpenAI dependency
# (Assuming it can be configured to not use OpenAI, or you might need to create a custom search tool)
yt_tool = YoutubeChannelSearchTool(youtube_channel_handle='@krishnaik06', use_openai=False)
# Define Agents
blog_researcher = Agent(
role='Blog Researcher from Youtube Videos',
goal='Get the relevant video transcription for the topic {topic} from the provided YT channel',
backstory=(
"Expert in understanding videos in AI, Data Science, Machine Learning, and Gen AI and providing suggestions."
),
llm=llm,
tools=[yt_tool],
allow_delegation=True,
verbose=True,
memory=True
)
blog_writer = Agent(
role='Blog Writer',
goal='Narrate compelling tech stories about the video {topic} from YT video',
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."
),
llm=llm,
tools=[yt_tool],
allow_delegation=False,
verbose=True,
memory=True
)
# Define Tasks
research_task = Task(
description=(
"Identify the video {topic}. "
"Get detailed information about the video from the channel video."
),
expected_output='A comprehensive 3 paragraphs long report based on the {topic} of video content.',
agent=blog_researcher,
tools=[yt_tool]
)
write_task = Task(
description=(
"Get the info from the YouTube channel on the topic {topic}."
),
expected_output='Summarize the info from the YouTube channel video on the topic {topic} and create the content for the blog',
agent=blog_writer,
tools=[yt_tool],
async_execution=False,
output_file='new-blog-post.md'
)
# Define Crew
crew = Crew(
agents=[blog_researcher, blog_writer],
tasks=[research_task, write_task],
verbose=2,
memory=True,
cache=True,
max_rpm=100,
share_crew=True
)
# Streamlit App
def main():
st.set_page_config(page_title="AI Blog Generator", page_icon="πŸ“")
st.title("AI Blog Generator")
st.write("Enter a topic to generate a blog post based on YouTube videos.")
topic = st.text_input("Topic", "")
if st.button("Generate Blog Post"):
if topic:
with st.spinner("Generating blog post..."):
try:
result = crew.kickoff(inputs={'topic': topic})
with open('new-blog-post.md', 'r') as file:
blog_content = file.read()
st.markdown(blog_content)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.error("Please enter a topic to continue.")
if __name__ == "__main__":
main()