Prathamesh1420 commited on
Commit
e929452
Β·
verified Β·
1 Parent(s): a68d089

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -57
app.py CHANGED
@@ -1,41 +1,43 @@
1
  import streamlit as st
2
- from crewai import Task, Agent, Crew, Process
3
  from crewai_tools import YoutubeChannelSearchTool
 
4
  from dotenv import load_dotenv
5
  import os
6
- import asyncio
7
- from langchain_google_genai import ChatGoogleGenerativeAI
8
  import nest_asyncio
 
9
  # Load environment variables
10
  load_dotenv()
 
 
 
 
 
11
  llm = ChatGoogleGenerativeAI(
12
  api_key=os.getenv('GOOGLE_API_KEY'),
13
  model="models/gemini-pro" # Replace with the correct model name
14
  )
 
15
  # Initialize the YoutubeChannelSearchTool with a specific Youtube channel handle to target your search
16
  yt_tool = YoutubeChannelSearchTool(youtube_channel_handle='@krishnaik06')
17
 
18
- # Define agents
19
- ## Create a senior blog content researcher
20
  blog_researcher = Agent(
21
  role='Blog Researcher from Youtube Videos',
22
  goal='Get the relevant video transcription for the topic {topic} from the provided YT channel',
23
- verbose=True,
24
- memory=True,
25
  backstory=(
26
  "Expert in understanding videos in AI, Data Science, Machine Learning, and Gen AI and providing suggestions."
27
  ),
28
  llm=llm,
29
  tools=[yt_tool],
30
- allow_delegation=True
 
 
31
  )
32
 
33
- ## Create a senior blog writer agent with YT tool
34
  blog_writer = Agent(
35
  role='Blog Writer',
36
  goal='Narrate compelling tech stories about the video {topic} from YT video',
37
- verbose=True,
38
- memory=True,
39
  backstory=(
40
  "With a flair for simplifying complex topics, you craft"
41
  "engaging narratives that captivate and educate, bringing new"
@@ -43,61 +45,48 @@ blog_writer = Agent(
43
  ),
44
  llm=llm,
45
  tools=[yt_tool],
46
- allow_delegation=False
 
 
47
  )
48
 
49
- # Define tasks
50
- ## Research Task
51
  research_task = Task(
52
- description=(
53
- "Identify the video {topic}."
54
- "Get detailed information about the video from the channel video."
55
- ),
56
- expected_output='A comprehensive 3 paragraphs long report based on the {topic} of video content.',
57
- tools=[yt_tool],
58
- agent=blog_researcher,
59
  )
60
 
61
- # Writing task with language model configuration
62
  write_task = Task(
63
- description=(
64
- "get the info from the youtube channel on the topic {topic}."
65
- ),
66
- expected_output='Summarize the info from the youtube channel video on the topic {topic} and create the content for the blog',
67
- tools=[yt_tool],
68
- agent=blog_writer,
69
- async_execution=False,
70
- output_file='new-blog-post.md' # Example of output customization
71
  )
72
 
73
- # Forming the tech-focused crew with some enhanced configurations
74
  crew = Crew(
75
- agents=[blog_researcher, blog_writer],
76
- tasks=[research_task, write_task],
77
- process=Process.sequential, # Optional: Sequential task execution is default
78
- memory=True,
79
- cache=True,
80
- max_rpm=100,
81
- share_crew=True
82
  )
83
 
84
- async def run_task_async(topic):
85
- # Start the task execution process with enhanced feedback
86
- result = await crew.kickoff(inputs={'topic': topic})
87
- with open('new-blog-post.md', 'r') as file:
88
- blog_content = file.read()
89
- return blog_content
90
-
91
- def run_task(topic):
92
- loop = asyncio.new_event_loop()
93
- asyncio.set_event_loop(loop)
94
- result = loop.run_until_complete(run_task_async(topic))
95
- loop.close()
96
- return result
97
-
98
  def main():
99
  st.set_page_config(page_title="AI Blog Generator", page_icon="πŸ“")
100
-
101
  st.title("AI Blog Generator")
102
  st.write("Enter a topic to generate a blog post based on YouTube videos.")
103
 
@@ -105,11 +94,15 @@ def main():
105
  if st.button("Generate Blog Post"):
106
  if topic:
107
  with st.spinner("Generating blog post..."):
108
- result = run_task(topic)
109
- st.success("Blog post generated!")
110
- st.markdown(result)
 
 
 
 
111
  else:
112
  st.error("Please enter a topic to continue.")
113
-
114
  if __name__ == "__main__":
115
  main()
 
1
  import streamlit as st
2
+ from crewai import Agent, Task, Crew
3
  from crewai_tools import YoutubeChannelSearchTool
4
+ from langchain_google_genai import ChatGoogleGenerativeAI
5
  from dotenv import load_dotenv
6
  import os
 
 
7
  import nest_asyncio
8
+
9
  # Load environment variables
10
  load_dotenv()
11
+
12
+ # Apply nest_asyncio to allow nested event loops
13
+ nest_asyncio.apply()
14
+
15
+ # Initialize Google Gemini AI
16
  llm = ChatGoogleGenerativeAI(
17
  api_key=os.getenv('GOOGLE_API_KEY'),
18
  model="models/gemini-pro" # Replace with the correct model name
19
  )
20
+
21
  # Initialize the YoutubeChannelSearchTool with a specific Youtube channel handle to target your search
22
  yt_tool = YoutubeChannelSearchTool(youtube_channel_handle='@krishnaik06')
23
 
24
+ # Define Agents
 
25
  blog_researcher = Agent(
26
  role='Blog Researcher from Youtube Videos',
27
  goal='Get the relevant video transcription for the topic {topic} from the provided YT channel',
 
 
28
  backstory=(
29
  "Expert in understanding videos in AI, Data Science, Machine Learning, and Gen AI and providing suggestions."
30
  ),
31
  llm=llm,
32
  tools=[yt_tool],
33
+ allow_delegation=True,
34
+ verbose=True,
35
+ memory=True
36
  )
37
 
 
38
  blog_writer = Agent(
39
  role='Blog Writer',
40
  goal='Narrate compelling tech stories about the video {topic} from YT video',
 
 
41
  backstory=(
42
  "With a flair for simplifying complex topics, you craft"
43
  "engaging narratives that captivate and educate, bringing new"
 
45
  ),
46
  llm=llm,
47
  tools=[yt_tool],
48
+ allow_delegation=False,
49
+ verbose=True,
50
+ memory=True
51
  )
52
 
53
+ # Define Tasks
 
54
  research_task = Task(
55
+ description=(
56
+ "Identify the video {topic}. "
57
+ "Get detailed information about the video from the channel video."
58
+ ),
59
+ expected_output='A comprehensive 3 paragraphs long report based on the {topic} of video content.',
60
+ agent=blog_researcher,
61
+ tools=[yt_tool]
62
  )
63
 
 
64
  write_task = Task(
65
+ description=(
66
+ "Get the info from the YouTube channel on the topic {topic}."
67
+ ),
68
+ expected_output='Summarize the info from the YouTube channel video on the topic {topic} and create the content for the blog',
69
+ agent=blog_writer,
70
+ tools=[yt_tool],
71
+ async_execution=False,
72
+ output_file='new-blog-post.md'
73
  )
74
 
75
+ # Define Crew
76
  crew = Crew(
77
+ agents=[blog_researcher, blog_writer],
78
+ tasks=[research_task, write_task],
79
+ verbose=2,
80
+ memory=True,
81
+ cache=True,
82
+ max_rpm=100,
83
+ share_crew=True
84
  )
85
 
86
+ # Streamlit App
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def main():
88
  st.set_page_config(page_title="AI Blog Generator", page_icon="πŸ“")
89
+
90
  st.title("AI Blog Generator")
91
  st.write("Enter a topic to generate a blog post based on YouTube videos.")
92
 
 
94
  if st.button("Generate Blog Post"):
95
  if topic:
96
  with st.spinner("Generating blog post..."):
97
+ try:
98
+ result = crew.kickoff(inputs={'topic': topic})
99
+ with open('new-blog-post.md', 'r') as file:
100
+ blog_content = file.read()
101
+ st.markdown(blog_content)
102
+ except Exception as e:
103
+ st.error(f"An error occurred: {e}")
104
  else:
105
  st.error("Please enter a topic to continue.")
106
+
107
  if __name__ == "__main__":
108
  main()