update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Load environment variables
|
6 |
+
load_dotenv()
|
7 |
+
os.environ['SERPER_API_KEY'] = os.getenv('SERPER_API_KEY')
|
8 |
+
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY')
|
9 |
+
|
10 |
+
# Import necessary modules from crewai
|
11 |
+
from crewai_tools import SerperDevTool
|
12 |
+
from crewai import Agent, Task, Crew, Process
|
13 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
14 |
+
|
15 |
+
# Initialize the tool for internet searching capabilities
|
16 |
+
tool = SerperDevTool()
|
17 |
+
|
18 |
+
# Call the Gemini models
|
19 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", verbose=True, temperature=0.5, google_api_key=os.getenv("GOOGLE_API_KEY"))
|
20 |
+
|
21 |
+
# Creating a senior researcher agent with memory and verbose mode
|
22 |
+
news_researcher = Agent(
|
23 |
+
role="Senior Researcher",
|
24 |
+
goal='Uncover groundbreaking technologies in {topic}',
|
25 |
+
verbose=True,
|
26 |
+
memory=True,
|
27 |
+
backstory=(
|
28 |
+
"Driven by curiosity, you're at the forefront of innovation, eager to explore and share knowledge that could change the world."
|
29 |
+
),
|
30 |
+
tools=[tool],
|
31 |
+
llm=llm,
|
32 |
+
allow_delegation=True
|
33 |
+
)
|
34 |
+
|
35 |
+
# Creating a writer agent with custom tools responsible for writing news blog
|
36 |
+
news_writer = Agent(
|
37 |
+
role='Writer',
|
38 |
+
goal='Narrate compelling tech stories about {topic}',
|
39 |
+
verbose=True,
|
40 |
+
memory=True,
|
41 |
+
backstory=(
|
42 |
+
"With a flair for simplifying complex topics, you craft engaging narratives that captivate and educate, bringing new discoveries to light in an accessible manner."
|
43 |
+
),
|
44 |
+
tools=[tool],
|
45 |
+
llm=llm,
|
46 |
+
allow_delegation=False
|
47 |
+
)
|
48 |
+
|
49 |
+
# Research task
|
50 |
+
research_task = Task(
|
51 |
+
description=(
|
52 |
+
"Identify the next big trend in {topic}. Focus on identifying pros and cons and the overall narrative. Your final report should clearly articulate the key points, its market opportunities, and potential risks."
|
53 |
+
),
|
54 |
+
expected_output='A comprehensive 3 paragraphs long report on the latest AI trends.',
|
55 |
+
tools=[tool],
|
56 |
+
agent=news_researcher,
|
57 |
+
)
|
58 |
+
|
59 |
+
# Writing task with language model configuration
|
60 |
+
write_task = Task(
|
61 |
+
description=(
|
62 |
+
"Compose an insightful article on {topic}. Focus on the latest trends and how it's impacting the industry. This article should be easy to understand, engaging, and positive."
|
63 |
+
),
|
64 |
+
expected_output='A 4 paragraph article on {topic} advancements formatted as markdown.',
|
65 |
+
tools=[tool],
|
66 |
+
agent=news_writer,
|
67 |
+
async_execution=False,
|
68 |
+
output_file='new-blog-post.md'
|
69 |
+
)
|
70 |
+
|
71 |
+
# Forming the tech-focused crew with some enhanced configuration
|
72 |
+
crew = Crew(
|
73 |
+
agents=[news_researcher, news_writer],
|
74 |
+
tasks=[research_task, write_task],
|
75 |
+
process=Process.sequential,
|
76 |
+
)
|
77 |
+
|
78 |
+
# Streamlit app
|
79 |
+
def main():
|
80 |
+
st.title("AI News Generation")
|
81 |
+
|
82 |
+
# Input for the topic
|
83 |
+
topic = st.text_input("Enter the topic for research", "AI in healthcare")
|
84 |
+
|
85 |
+
if st.button("Generate Report and Article"):
|
86 |
+
result = crew.kickoff(inputs={'topic': topic})
|
87 |
+
st.success("Task execution completed!")
|
88 |
+
st.subheader("Research Report")
|
89 |
+
st.write(result.get(research_task))
|
90 |
+
st.subheader("News Article")
|
91 |
+
st.write(result.get(write_task))
|
92 |
+
|
93 |
+
# Display the content of the generated markdown file (if it exists)
|
94 |
+
if os.path.exists('new-blog-post.md'):
|
95 |
+
with open('new-blog-post.md', 'r') as file:
|
96 |
+
st.markdown(file.read())
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
main()
|