PriyankaHundalekar commited on
Commit
8516c5a
·
verified ·
1 Parent(s): 178a867

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +116 -0
  2. streamlit_app.py +164 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from crewai import Crew, Agent, Task, LLM
2
+ from crewai_tools import SerperDevTool
3
+
4
+ from dotenv import load_dotenv
5
+ load_dotenv()
6
+
7
+ topic = "AI in Healthcare"
8
+
9
+
10
+ # query
11
+ # llm
12
+ # streamlit
13
+
14
+ # A1 Researcher + Web Search (Serper API)------> A2 Content Creator(Summarization)
15
+ # LLM Provider
16
+ llm = LLM(
17
+ model="gemini/gemini-2.0-flash",
18
+ )
19
+ # Tool
20
+ search_tool = SerperDevTool(n=2)
21
+
22
+ # Agent 1
23
+ senior_research_analyst = Agent(
24
+ role = "Senior Research Analyst",
25
+ goal= f"Research, analyze, and synthesize comprehensive information on {topic} from reliable web sources",
26
+ backstory="You're an expert research analyst with advanced web research skills. "
27
+ "You excel at finding, analyzing, and synthesizing information from "
28
+ "across the internet using search tools. You're skilled at "
29
+ "distinguishing reliable sources from unreliable ones, "
30
+ "fact-checking, cross-referencing information, and "
31
+ "identifying key patterns and insights. You provide "
32
+ "well-organized research briefs with proper citations "
33
+ "and source verification. Your analysis includes both "
34
+ "raw data and interpreted insights, making complex "
35
+ "information accessible and actionable.",
36
+ verbose = True,
37
+ allow_delegation=False,
38
+ tools = [search_tool],
39
+ llm = llm
40
+ )
41
+
42
+ # Agent 2 Content Creator
43
+
44
+
45
+ content_writer = Agent(
46
+ role="Content Writer",
47
+ goal="Transform research findings into engaging blog posts while maintaining accuracy",
48
+ backstory="You're a skilled content writer specialized in creating "
49
+ "engaging, accessible content from technical research. "
50
+ "You work closely with the Senior Research Analyst and excel at maintaining the perfect "
51
+ "balance between informative and entertaining writing, "
52
+ "while ensuring all facts and citations from the research "
53
+ "are properly incorporated. You have a talent for making "
54
+ "complex topics approachable without oversimplifying them.",
55
+ verbose=True,
56
+ allow_delegation=False,
57
+ llm = llm
58
+ )
59
+
60
+
61
+
62
+ research_tasks = Task(
63
+ description=("""
64
+ 1. Conduct comprehensive research on {topic} including:
65
+ - Recent developments and news
66
+ - Key industry trends and innovations
67
+ - Expert opinions and analyses
68
+ - Statistical data and market insights
69
+ 2. Evaluate source credibility and fact-check all information
70
+ 3. Organize findings into a structured research brief
71
+ 4. Include all relevant citations and sources
72
+ """),
73
+ expected_output = """A detailed research report containing:
74
+ - Executive summary of key findings
75
+ - Comprehensive analysis of current trends and developments
76
+ - List of verified facts and statistics
77
+ - All citations and links to original sources
78
+ - Clear categorization of main themes and patterns
79
+ Please format with clear sections and bullet points for easy reference.""",
80
+ agent = senior_research_analyst)
81
+
82
+
83
+
84
+ # Content Writer Task
85
+ # Task 2 Content Writing
86
+ writing_task = Task(
87
+ description=("""
88
+ Using the research brief provided, create an engaging blog post that:
89
+ 1. Transforms technical information into accessible content
90
+ 2. Maintains all factual accuracy and citations from the research
91
+ 3. Includes:
92
+ - Attention-grabbing introduction
93
+ - Well-structured body sections with clear headings
94
+ - Compelling conclusion
95
+ 4. Preserves all source citations in [Source: URL] format
96
+ 5. Includes a References section at the end
97
+ """),
98
+ expected_output = """A polished blog post in markdown format that:
99
+ - Engages readers while maintaining accuracy
100
+ - Contains properly structured sections
101
+ - Includes Inline citations hyperlinked to the original source url
102
+ - Presents information in an accessible yet informative way
103
+ - Follows proper markdown formatting, use H1 for the title and H3 for the sub-sections""",
104
+ agent = content_writer)
105
+
106
+
107
+
108
+ crew = Crew(
109
+ agents= [senior_research_analyst, content_writer],
110
+ tasks= [research_tasks, writing_task],
111
+ verbose = True
112
+ )
113
+
114
+ result = crew.kickoff(inputs= {"topic" : topic})
115
+
116
+ print(result)
streamlit_app.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from crewai import Agent, Task, Crew, LLM
4
+ from crewai_tools import SerperDevTool
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
+
10
+ # Streamlit page config
11
+ st.set_page_config(page_title="Content Reseracher & Writer", page_icon="📰", layout="wide")
12
+
13
+ # Title and description
14
+ st.title("🤖 Content Researcher & Writer, powered by CrewAI")
15
+ st.markdown("Generate blog posts about any topic using AI agents.")
16
+
17
+ # Sidebar
18
+ with st.sidebar:
19
+ st.header("Content Settings")
20
+
21
+ # Make the text input take up more space
22
+ topic = st.text_area(
23
+ "Enter your topic",
24
+ height=100,
25
+ placeholder="Enter the topic"
26
+ )
27
+
28
+ # Add more sidebar controls if needed
29
+ st.markdown("### LLM Settings")
30
+ temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
31
+
32
+ # Add some spacing
33
+ st.markdown("---")
34
+
35
+ # Make the generate button more prominent in the sidebar
36
+ generate_button = st.button("Generate Content", type="primary", use_container_width=True)
37
+
38
+ # Add some helpful information
39
+ with st.expander("ℹ️ How to use"):
40
+ st.markdown("""
41
+ 1. Enter your desired content topic
42
+ 2. Play with the temperature
43
+ 3. Click 'Generate Content' to start
44
+ 4. Wait for the AI to generate your article
45
+ 5. Download the result as a markdown file
46
+ """)
47
+
48
+ def generate_content(topic):
49
+ llm = LLM(
50
+ model="gemini/gemini-2.0-flash",
51
+ )
52
+
53
+ search_tool = SerperDevTool(n_results=1)
54
+
55
+ # First Agent: Senior Research Analyst
56
+ senior_research_analyst = Agent(
57
+ role="Senior Research Analyst",
58
+ goal=f"Research, analyze, and synthesize comprehensive information on {topic} from reliable web sources",
59
+ backstory="You're an expert research analyst with advanced web research skills. "
60
+ "You excel at finding, analyzing, and synthesizing information from "
61
+ "across the internet using search tools. You're skilled at "
62
+ "distinguishing reliable sources from unreliable ones, "
63
+ "fact-checking, cross-referencing information, and "
64
+ "identifying key patterns and insights. You provide "
65
+ "well-organized research briefs with proper citations "
66
+ "and source verification. Your analysis includes both "
67
+ "raw data and interpreted insights, making complex "
68
+ "information accessible and actionable.",
69
+ allow_delegation=False,
70
+ verbose=True,
71
+ tools=[search_tool],
72
+ llm=llm
73
+ )
74
+
75
+ # Second Agent: Content Writer
76
+ content_writer = Agent(
77
+ role="Content Writer",
78
+ goal="Transform research findings into engaging blog posts while maintaining accuracy",
79
+ backstory="You're a skilled content writer specialized in creating "
80
+ "engaging, accessible content from technical research. "
81
+ "You work closely with the Senior Research Analyst and excel at maintaining the perfect "
82
+ "balance between informative and entertaining writing, "
83
+ "while ensuring all facts and citations from the research "
84
+ "are properly incorporated. You have a talent for making "
85
+ "complex topics approachable without oversimplifying them.",
86
+ allow_delegation=False,
87
+ verbose=True,
88
+ llm=llm
89
+ )
90
+
91
+ # Research Task
92
+ research_task = Task(
93
+ description=("""
94
+ 1. Conduct comprehensive research on {topic} including:
95
+ - Recent developments and news
96
+ - Key industry trends and innovations
97
+ - Expert opinions and analyses
98
+ - Statistical data and market insights
99
+ 2. Evaluate source credibility and fact-check all information
100
+ 3. Organize findings into a structured research brief
101
+ 4. Include all relevant citations and sources
102
+ """),
103
+ expected_output="""A detailed research report containing:
104
+ - Executive summary of key findings
105
+ - Comprehensive analysis of current trends and developments
106
+ - List of verified facts and statistics
107
+ - All citations and links to original sources
108
+ - Clear categorization of main themes and patterns
109
+ Please format with clear sections and bullet points for easy reference.""",
110
+ agent=senior_research_analyst
111
+ )
112
+
113
+ # Writing Task
114
+ writing_task = Task(
115
+ description=("""
116
+ Using the research brief provided, create an engaging blog post that:
117
+ 1. Transforms technical information into accessible content
118
+ 2. Maintains all factual accuracy and citations from the research
119
+ 3. Includes:
120
+ - Attention-grabbing introduction
121
+ - Well-structured body sections with clear headings
122
+ - Compelling conclusion
123
+ 4. Preserves all source citations in [Source: URL] format
124
+ 5. Includes a References section at the end
125
+ """),
126
+ expected_output="""A polished blog post in markdown format that:
127
+ - Engages readers while maintaining accuracy
128
+ - Contains properly structured sections
129
+ - Includes Inline citations hyperlinked to the original source url
130
+ - Presents information in an accessible yet informative way
131
+ - Follows proper markdown formatting, use H1 for the title and H3 for the sub-sections""",
132
+ agent=content_writer
133
+ )
134
+
135
+ # Create Crew
136
+ crew = Crew(
137
+ agents=[senior_research_analyst, content_writer],
138
+ tasks=[research_task, writing_task],
139
+ verbose=True
140
+ )
141
+
142
+ return crew.kickoff(inputs={"topic": topic})
143
+
144
+ # Main content area
145
+ if generate_button:
146
+ with st.spinner('Generating content... This may take a moment.'):
147
+ try:
148
+ result = generate_content(topic)
149
+ st.markdown("### Generated Content")
150
+ st.markdown(result)
151
+
152
+ # Add download button
153
+ st.download_button(
154
+ label="Download Content",
155
+ data=result.raw,
156
+ file_name=f"{topic.lower().replace(' ', '_')}_article.md",
157
+ mime="text/markdown"
158
+ )
159
+ except Exception as e:
160
+ st.error(f"An error occurred: {str(e)}")
161
+
162
+ # Footer
163
+ st.markdown("---")
164
+ st.markdown("Built with CrewAI, Streamlit and ChatGPT")