Prathamesh1420's picture
Update app.py
a68d089 verified
raw
history blame
3.68 kB
import streamlit as st
from crewai import Task, Agent, Crew, Process
from crewai_tools import YoutubeChannelSearchTool
from dotenv import load_dotenv
import os
import asyncio
from langchain_google_genai import ChatGoogleGenerativeAI
import nest_asyncio
# Load environment variables
load_dotenv()
llm = ChatGoogleGenerativeAI(
api_key=os.getenv('GOOGLE_API_KEY'),
model="models/gemini-pro" # Replace with the correct model name
)
# Initialize the YoutubeChannelSearchTool with a specific Youtube channel handle to target your search
yt_tool = YoutubeChannelSearchTool(youtube_channel_handle='@krishnaik06')
# Define agents
## Create a senior blog content researcher
blog_researcher = Agent(
role='Blog Researcher from Youtube Videos',
goal='Get the relevant video transcription for the topic {topic} from the provided YT channel',
verbose=True,
memory=True,
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
)
## Create a senior blog writer agent with YT tool
blog_writer = Agent(
role='Blog Writer',
goal='Narrate compelling tech stories about the video {topic} from YT video',
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."
),
llm=llm,
tools=[yt_tool],
allow_delegation=False
)
# Define tasks
## Research Task
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.',
tools=[yt_tool],
agent=blog_researcher,
)
# Writing task with language model configuration
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',
tools=[yt_tool],
agent=blog_writer,
async_execution=False,
output_file='new-blog-post.md' # Example of output customization
)
# Forming the tech-focused crew with some enhanced configurations
crew = Crew(
agents=[blog_researcher, blog_writer],
tasks=[research_task, write_task],
process=Process.sequential, # Optional: Sequential task execution is default
memory=True,
cache=True,
max_rpm=100,
share_crew=True
)
async def run_task_async(topic):
# Start the task execution process with enhanced feedback
result = await crew.kickoff(inputs={'topic': topic})
with open('new-blog-post.md', 'r') as file:
blog_content = file.read()
return blog_content
def run_task(topic):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
result = loop.run_until_complete(run_task_async(topic))
loop.close()
return result
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..."):
result = run_task(topic)
st.success("Blog post generated!")
st.markdown(result)
else:
st.error("Please enter a topic to continue.")
if __name__ == "__main__":
main()