Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import time
|
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from crewai import Agent, Crew, Process, Task
|
| 7 |
-
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
|
@@ -16,7 +15,7 @@ st.set_page_config(
|
|
| 16 |
layout="wide"
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# Add custom CSS
|
| 20 |
st.markdown("""
|
| 21 |
<style>
|
| 22 |
.stButton>button {
|
|
@@ -29,44 +28,32 @@ st.markdown("""
|
|
| 29 |
border-radius: 10px;
|
| 30 |
margin-top: 20px;
|
| 31 |
}
|
| 32 |
-
.agent-
|
| 33 |
padding: 10px 15px;
|
| 34 |
border-radius: 15px;
|
| 35 |
margin: 5px 0;
|
| 36 |
max-width: 80%;
|
| 37 |
-
|
| 38 |
}
|
| 39 |
.researcher {
|
| 40 |
background-color: #E3F2FD;
|
| 41 |
-
|
| 42 |
}
|
| 43 |
.analyst {
|
| 44 |
background-color: #F3E5F5;
|
| 45 |
-
|
| 46 |
}
|
| 47 |
.writer {
|
| 48 |
background-color: #E8F5E9;
|
| 49 |
-
|
| 50 |
-
}
|
| 51 |
-
@keyframes fadeIn {
|
| 52 |
-
from { opacity: 0; transform: translateY(10px); }
|
| 53 |
-
to { opacity: 1; transform: translateY(0); }
|
| 54 |
-
}
|
| 55 |
-
.agent-name {
|
| 56 |
-
font-weight: bold;
|
| 57 |
-
margin-bottom: 5px;
|
| 58 |
-
}
|
| 59 |
-
.agent-thinking {
|
| 60 |
-
color: #666;
|
| 61 |
-
font-style: italic;
|
| 62 |
}
|
| 63 |
</style>
|
| 64 |
""", unsafe_allow_html=True)
|
| 65 |
|
| 66 |
-
def
|
| 67 |
-
"""
|
| 68 |
-
class_name =
|
| 69 |
-
|
| 70 |
"Research Analyst": "🔍",
|
| 71 |
"Data Analyst": "📊",
|
| 72 |
"Report Writer": "✍️"
|
|
@@ -74,8 +61,8 @@ def create_agent_message(agent_role: str, message: str, container):
|
|
| 74 |
|
| 75 |
container.markdown(
|
| 76 |
f"""
|
| 77 |
-
<div class="agent-
|
| 78 |
-
<
|
| 79 |
{message}
|
| 80 |
</div>
|
| 81 |
""",
|
|
@@ -84,34 +71,11 @@ def create_agent_message(agent_role: str, message: str, container):
|
|
| 84 |
|
| 85 |
def run_market_research(topic: str, chat_container):
|
| 86 |
try:
|
| 87 |
-
#
|
| 88 |
-
search_tool = SerperDevTool()
|
| 89 |
-
scrape_tool = ScrapeWebsiteTool()
|
| 90 |
-
|
| 91 |
-
# Create custom callback for agent interactions
|
| 92 |
-
class AgentCallback:
|
| 93 |
-
def on_agent_start(self, agent):
|
| 94 |
-
create_agent_message(
|
| 95 |
-
agent.role,
|
| 96 |
-
f"<div class='agent-thinking'>Starting my task to {agent.goal.lower()}...</div>",
|
| 97 |
-
chat_container
|
| 98 |
-
)
|
| 99 |
-
time.sleep(1) # Add slight delay for visual effect
|
| 100 |
-
|
| 101 |
-
def on_agent_end(self, agent, result):
|
| 102 |
-
create_agent_message(
|
| 103 |
-
agent.role,
|
| 104 |
-
f"I've completed my analysis. Here are the key points:\n\n{result[:200]}...",
|
| 105 |
-
chat_container
|
| 106 |
-
)
|
| 107 |
-
time.sleep(1)
|
| 108 |
-
|
| 109 |
-
# Create agents
|
| 110 |
researcher = Agent(
|
| 111 |
role='Research Analyst',
|
| 112 |
goal=f'Conduct thorough market research about {topic}',
|
| 113 |
backstory='You are an experienced market research analyst with expertise in data analysis and trend identification.',
|
| 114 |
-
tools=[search_tool, scrape_tool],
|
| 115 |
verbose=True
|
| 116 |
)
|
| 117 |
|
|
@@ -119,7 +83,6 @@ def run_market_research(topic: str, chat_container):
|
|
| 119 |
role='Data Analyst',
|
| 120 |
goal='Analyze research findings and identify key insights',
|
| 121 |
backstory='You are a skilled data analyst with expertise in interpreting market research and creating actionable insights.',
|
| 122 |
-
tools=[search_tool],
|
| 123 |
verbose=True
|
| 124 |
)
|
| 125 |
|
|
@@ -129,8 +92,15 @@ def run_market_research(topic: str, chat_container):
|
|
| 129 |
backstory='You are a professional writer specializing in creating clear and compelling business reports.',
|
| 130 |
verbose=True
|
| 131 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
# Define tasks
|
| 134 |
research_task = Task(
|
| 135 |
description=f"""
|
| 136 |
Conduct comprehensive market research on {topic}.
|
|
@@ -139,11 +109,15 @@ def run_market_research(topic: str, chat_container):
|
|
| 139 |
2. Key players and their market share
|
| 140 |
3. Consumer adoption trends
|
| 141 |
4. Regulatory environment
|
| 142 |
-
|
| 143 |
-
Output should be a detailed research findings document with data and sources.
|
| 144 |
""",
|
| 145 |
agent=researcher,
|
| 146 |
-
expected_output="A comprehensive research document
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
analysis_task = Task(
|
|
@@ -153,14 +127,18 @@ def run_market_research(topic: str, chat_container):
|
|
| 153 |
2. Potential challenges
|
| 154 |
3. Growth drivers
|
| 155 |
4. Competitive dynamics
|
| 156 |
-
|
| 157 |
-
Use the research provided by the Research Analyst to create an analytical report.
|
| 158 |
""",
|
| 159 |
agent=analyst,
|
| 160 |
-
expected_output="An analytical report with key insights
|
| 161 |
context=[research_task]
|
| 162 |
)
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
report_task = Task(
|
| 165 |
description=f"""
|
| 166 |
Create a detailed market research report that includes:
|
|
@@ -168,15 +146,12 @@ def run_market_research(topic: str, chat_container):
|
|
| 168 |
2. Market overview
|
| 169 |
3. Key findings
|
| 170 |
4. Strategic recommendations
|
| 171 |
-
|
| 172 |
-
Base the report on the research and analysis provided by the other agents.
|
| 173 |
-
The report should be well-structured, professional, and actionable.
|
| 174 |
""",
|
| 175 |
agent=writer,
|
| 176 |
-
expected_output="A complete market research report in markdown format
|
| 177 |
context=[research_task, analysis_task]
|
| 178 |
)
|
| 179 |
-
|
| 180 |
# Create and run the crew
|
| 181 |
crew = Crew(
|
| 182 |
agents=[researcher, analyst, writer],
|
|
@@ -184,65 +159,66 @@ def run_market_research(topic: str, chat_container):
|
|
| 184 |
verbose=True,
|
| 185 |
process=Process.sequential
|
| 186 |
)
|
| 187 |
-
|
| 188 |
-
# Add callback to crew
|
| 189 |
-
crew.callback = AgentCallback()
|
| 190 |
|
| 191 |
result = crew.kickoff()
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
except Exception as e:
|
| 195 |
-
st.error(f"Error
|
| 196 |
return None
|
| 197 |
|
| 198 |
def main():
|
| 199 |
st.title("🤖 AI Market Research Generator")
|
| 200 |
-
st.markdown("""
|
| 201 |
-
This tool uses a crew of AI agents to generate comprehensive market research reports.
|
| 202 |
-
Watch the agents collaborate in real-time below!
|
| 203 |
-
""")
|
| 204 |
|
| 205 |
# Create two columns
|
| 206 |
col1, col2 = st.columns([2, 3])
|
| 207 |
|
| 208 |
with col1:
|
| 209 |
-
st.subheader("Research Topic")
|
| 210 |
topic = st.text_input(
|
| 211 |
-
"
|
| 212 |
placeholder="e.g., Electric Vehicles Market",
|
| 213 |
key="research_topic"
|
| 214 |
)
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
with st.session_state.chat_container:
|
| 236 |
-
st.subheader("🤖 Agents at Work")
|
| 237 |
-
report = run_market_research(topic, st.session_state.chat_container)
|
| 238 |
-
|
| 239 |
-
if report:
|
| 240 |
-
with st.session_state.report_container:
|
| 241 |
-
st.subheader("📑 Generated Report")
|
| 242 |
st.markdown('<div class="report-container">', unsafe_allow_html=True)
|
| 243 |
st.markdown(report)
|
| 244 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 245 |
|
|
|
|
| 246 |
st.download_button(
|
| 247 |
label="Download Report",
|
| 248 |
data=report,
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from crewai import Agent, Crew, Process, Task
|
|
|
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
|
|
|
| 15 |
layout="wide"
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# Add custom CSS
|
| 19 |
st.markdown("""
|
| 20 |
<style>
|
| 21 |
.stButton>button {
|
|
|
|
| 28 |
border-radius: 10px;
|
| 29 |
margin-top: 20px;
|
| 30 |
}
|
| 31 |
+
.agent-message {
|
| 32 |
padding: 10px 15px;
|
| 33 |
border-radius: 15px;
|
| 34 |
margin: 5px 0;
|
| 35 |
max-width: 80%;
|
| 36 |
+
line-height: 1.4;
|
| 37 |
}
|
| 38 |
.researcher {
|
| 39 |
background-color: #E3F2FD;
|
| 40 |
+
border-left: 4px solid #1976D2;
|
| 41 |
}
|
| 42 |
.analyst {
|
| 43 |
background-color: #F3E5F5;
|
| 44 |
+
border-left: 4px solid #9C27B0;
|
| 45 |
}
|
| 46 |
.writer {
|
| 47 |
background-color: #E8F5E9;
|
| 48 |
+
border-left: 4px solid #43A047;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
</style>
|
| 51 |
""", unsafe_allow_html=True)
|
| 52 |
|
| 53 |
+
def display_agent_message(container, role: str, message: str):
|
| 54 |
+
"""Display an agent message in the container"""
|
| 55 |
+
class_name = role.lower().replace(" ", "-")
|
| 56 |
+
icons = {
|
| 57 |
"Research Analyst": "🔍",
|
| 58 |
"Data Analyst": "📊",
|
| 59 |
"Report Writer": "✍️"
|
|
|
|
| 61 |
|
| 62 |
container.markdown(
|
| 63 |
f"""
|
| 64 |
+
<div class="agent-message {class_name}">
|
| 65 |
+
<b>{icons.get(role, '')} {role}:</b><br/>
|
| 66 |
{message}
|
| 67 |
</div>
|
| 68 |
""",
|
|
|
|
| 71 |
|
| 72 |
def run_market_research(topic: str, chat_container):
|
| 73 |
try:
|
| 74 |
+
# Create agents without external tools for now
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
researcher = Agent(
|
| 76 |
role='Research Analyst',
|
| 77 |
goal=f'Conduct thorough market research about {topic}',
|
| 78 |
backstory='You are an experienced market research analyst with expertise in data analysis and trend identification.',
|
|
|
|
| 79 |
verbose=True
|
| 80 |
)
|
| 81 |
|
|
|
|
| 83 |
role='Data Analyst',
|
| 84 |
goal='Analyze research findings and identify key insights',
|
| 85 |
backstory='You are a skilled data analyst with expertise in interpreting market research and creating actionable insights.',
|
|
|
|
| 86 |
verbose=True
|
| 87 |
)
|
| 88 |
|
|
|
|
| 92 |
backstory='You are a professional writer specializing in creating clear and compelling business reports.',
|
| 93 |
verbose=True
|
| 94 |
)
|
| 95 |
+
|
| 96 |
+
# Display initial agent messages
|
| 97 |
+
display_agent_message(
|
| 98 |
+
chat_container,
|
| 99 |
+
"Research Analyst",
|
| 100 |
+
f"Starting research on {topic}..."
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
# Define tasks
|
| 104 |
research_task = Task(
|
| 105 |
description=f"""
|
| 106 |
Conduct comprehensive market research on {topic}.
|
|
|
|
| 109 |
2. Key players and their market share
|
| 110 |
3. Consumer adoption trends
|
| 111 |
4. Regulatory environment
|
|
|
|
|
|
|
| 112 |
""",
|
| 113 |
agent=researcher,
|
| 114 |
+
expected_output="A comprehensive research document with market analysis."
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
display_agent_message(
|
| 118 |
+
chat_container,
|
| 119 |
+
"Data Analyst",
|
| 120 |
+
"Preparing to analyze the research findings..."
|
| 121 |
)
|
| 122 |
|
| 123 |
analysis_task = Task(
|
|
|
|
| 127 |
2. Potential challenges
|
| 128 |
3. Growth drivers
|
| 129 |
4. Competitive dynamics
|
|
|
|
|
|
|
| 130 |
""",
|
| 131 |
agent=analyst,
|
| 132 |
+
expected_output="An analytical report with key insights and strategic implications.",
|
| 133 |
context=[research_task]
|
| 134 |
)
|
| 135 |
|
| 136 |
+
display_agent_message(
|
| 137 |
+
chat_container,
|
| 138 |
+
"Report Writer",
|
| 139 |
+
"Getting ready to compile the final report..."
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
report_task = Task(
|
| 143 |
description=f"""
|
| 144 |
Create a detailed market research report that includes:
|
|
|
|
| 146 |
2. Market overview
|
| 147 |
3. Key findings
|
| 148 |
4. Strategic recommendations
|
|
|
|
|
|
|
|
|
|
| 149 |
""",
|
| 150 |
agent=writer,
|
| 151 |
+
expected_output="A complete market research report in markdown format.",
|
| 152 |
context=[research_task, analysis_task]
|
| 153 |
)
|
| 154 |
+
|
| 155 |
# Create and run the crew
|
| 156 |
crew = Crew(
|
| 157 |
agents=[researcher, analyst, writer],
|
|
|
|
| 159 |
verbose=True,
|
| 160 |
process=Process.sequential
|
| 161 |
)
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
result = crew.kickoff()
|
| 164 |
+
|
| 165 |
+
# Format the final result
|
| 166 |
+
if hasattr(result, 'raw_output'):
|
| 167 |
+
final_report = str(result.raw_output)
|
| 168 |
+
else:
|
| 169 |
+
final_report = str(result)
|
| 170 |
+
|
| 171 |
+
# Display completion message
|
| 172 |
+
display_agent_message(
|
| 173 |
+
chat_container,
|
| 174 |
+
"Report Writer",
|
| 175 |
+
"Report generation completed! ✨"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
return final_report
|
| 179 |
|
| 180 |
except Exception as e:
|
| 181 |
+
st.error(f"Error: {str(e)}")
|
| 182 |
return None
|
| 183 |
|
| 184 |
def main():
|
| 185 |
st.title("🤖 AI Market Research Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
# Create two columns
|
| 188 |
col1, col2 = st.columns([2, 3])
|
| 189 |
|
| 190 |
with col1:
|
| 191 |
+
st.subheader("Enter Research Topic")
|
| 192 |
topic = st.text_input(
|
| 193 |
+
"What market would you like to research?",
|
| 194 |
placeholder="e.g., Electric Vehicles Market",
|
| 195 |
key="research_topic"
|
| 196 |
)
|
| 197 |
|
| 198 |
+
if st.button("Generate Report", type="primary"):
|
| 199 |
+
if not topic:
|
| 200 |
+
st.error("Please enter a research topic")
|
| 201 |
+
return
|
| 202 |
+
|
| 203 |
+
# Clear previous chat
|
| 204 |
+
if "chat_container" in st.session_state:
|
| 205 |
+
st.session_state.chat_container.empty()
|
| 206 |
+
|
| 207 |
+
# Create new chat container
|
| 208 |
+
st.session_state.chat_container = col2.container()
|
| 209 |
+
|
| 210 |
+
# Run the research
|
| 211 |
+
with st.session_state.chat_container:
|
| 212 |
+
st.subheader("🤖 Live Agent Updates")
|
| 213 |
+
report = run_market_research(topic, st.session_state.chat_container)
|
| 214 |
+
|
| 215 |
+
if report:
|
| 216 |
+
st.subheader("📊 Generated Report")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
st.markdown('<div class="report-container">', unsafe_allow_html=True)
|
| 218 |
st.markdown(report)
|
| 219 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 220 |
|
| 221 |
+
# Download button
|
| 222 |
st.download_button(
|
| 223 |
label="Download Report",
|
| 224 |
data=report,
|