|
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_dotenv() |
|
llm = ChatGoogleGenerativeAI( |
|
api_key=os.getenv('GOOGLE_API_KEY'), |
|
model="models/gemini-pro" |
|
) |
|
|
|
yt_tool = YoutubeChannelSearchTool(youtube_channel_handle='@krishnaik06') |
|
|
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
|
|
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, |
|
) |
|
|
|
|
|
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' |
|
) |
|
|
|
|
|
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 |
|
) |
|
|
|
async def run_task_async(topic): |
|
|
|
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() |
|
|