Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,11 @@
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
-
import
|
| 5 |
-
import random
|
| 6 |
import streamlit as st
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from crewai import Agent, Crew, Process, Task
|
| 9 |
|
| 10 |
-
# Add plotly for interactive charts
|
| 11 |
-
import plotly.graph_objects as go
|
| 12 |
-
import plotly.express as px
|
| 13 |
-
import pandas as pd
|
| 14 |
-
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
| 17 |
|
|
@@ -75,12 +69,34 @@ st.markdown("""
|
|
| 75 |
100% { transform: translateX(100%); opacity: 0; }
|
| 76 |
}
|
| 77 |
|
| 78 |
-
/*
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
border-radius: 10px;
|
|
|
|
| 82 |
margin: 10px 0;
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
@keyframes slideIn {
|
|
@@ -105,6 +121,15 @@ st.markdown("""
|
|
| 105 |
.stTabs [aria-selected="true"] {
|
| 106 |
background-color: #e6f3ff;
|
| 107 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
</style>
|
| 109 |
""", unsafe_allow_html=True)
|
| 110 |
|
|
@@ -124,43 +149,6 @@ def create_agent_flow(container):
|
|
| 124 |
</div>
|
| 125 |
""", unsafe_allow_html=True)
|
| 126 |
|
| 127 |
-
def generate_sample_charts(topic, analysis_text):
|
| 128 |
-
"""Generate relevant charts based on the analysis"""
|
| 129 |
-
charts = []
|
| 130 |
-
|
| 131 |
-
# Extract numerical data using simple patterns
|
| 132 |
-
# This is a simplified example - in practice, you'd want more sophisticated parsing
|
| 133 |
-
growth_pattern = r"(\d+(?:\.\d+)?)\s*%"
|
| 134 |
-
values = [float(x) for x in re.findall(growth_pattern, analysis_text)]
|
| 135 |
-
|
| 136 |
-
if values:
|
| 137 |
-
# Market Growth Chart
|
| 138 |
-
fig_growth = go.Figure(data=[
|
| 139 |
-
go.Scatter(
|
| 140 |
-
x=list(range(2024, 2024 + len(values))),
|
| 141 |
-
y=values,
|
| 142 |
-
mode='lines+markers',
|
| 143 |
-
name='Projected Growth'
|
| 144 |
-
)
|
| 145 |
-
])
|
| 146 |
-
fig_growth.update_layout(
|
| 147 |
-
title=f"{topic} Market Growth Projection",
|
| 148 |
-
xaxis_title="Year",
|
| 149 |
-
yaxis_title="Growth Rate (%)"
|
| 150 |
-
)
|
| 151 |
-
charts.append(fig_growth)
|
| 152 |
-
|
| 153 |
-
# Market Share Donut Chart
|
| 154 |
-
fig_share = go.Figure(data=[go.Pie(
|
| 155 |
-
labels=['Major Players', 'Other Companies'],
|
| 156 |
-
values=[65, 35],
|
| 157 |
-
hole=.3
|
| 158 |
-
)])
|
| 159 |
-
fig_share.update_layout(title='Market Share Distribution')
|
| 160 |
-
charts.append(fig_share)
|
| 161 |
-
|
| 162 |
-
return charts
|
| 163 |
-
|
| 164 |
def run_market_research(topic: str, progress_bar, chat_container):
|
| 165 |
try:
|
| 166 |
# Create agents
|
|
@@ -185,41 +173,73 @@ def run_market_research(topic: str, progress_bar, chat_container):
|
|
| 185 |
verbose=True
|
| 186 |
)
|
| 187 |
|
| 188 |
-
# Update progress
|
| 189 |
progress_bar.progress(0, "Initializing research...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
time.sleep(1)
|
| 191 |
|
| 192 |
# Research Phase
|
| 193 |
research_task = Task(
|
| 194 |
-
description=f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
agent=researcher,
|
| 196 |
expected_output="A comprehensive research document with market analysis."
|
| 197 |
)
|
| 198 |
|
| 199 |
-
progress_bar.progress(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
time.sleep(1)
|
| 201 |
|
| 202 |
# Analysis Phase
|
| 203 |
analysis_task = Task(
|
| 204 |
-
description=f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
agent=analyst,
|
| 206 |
expected_output="An analytical report with key insights.",
|
| 207 |
context=[research_task]
|
| 208 |
)
|
| 209 |
|
| 210 |
-
progress_bar.progress(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
time.sleep(1)
|
| 212 |
|
| 213 |
# Report Phase
|
| 214 |
report_task = Task(
|
| 215 |
-
description=f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
agent=writer,
|
| 217 |
expected_output="A complete market research report in markdown format.",
|
| 218 |
context=[research_task, analysis_task]
|
| 219 |
)
|
| 220 |
|
| 221 |
-
progress_bar.progress(75, "Generating report...")
|
| 222 |
-
|
| 223 |
# Create and run the crew
|
| 224 |
crew = Crew(
|
| 225 |
agents=[researcher, analyst, writer],
|
|
@@ -237,14 +257,11 @@ def run_market_research(topic: str, progress_bar, chat_container):
|
|
| 237 |
else:
|
| 238 |
final_report = str(result)
|
| 239 |
|
| 240 |
-
|
| 241 |
-
charts = generate_sample_charts(topic, final_report)
|
| 242 |
-
|
| 243 |
-
return final_report, charts
|
| 244 |
|
| 245 |
except Exception as e:
|
| 246 |
st.error(f"Error: {str(e)}")
|
| 247 |
-
return None
|
| 248 |
|
| 249 |
def main():
|
| 250 |
st.title("🤖 AI Market Research Generator")
|
|
@@ -276,12 +293,11 @@ def main():
|
|
| 276 |
create_agent_flow(col2)
|
| 277 |
|
| 278 |
# Run the research
|
| 279 |
-
report
|
| 280 |
|
| 281 |
if report:
|
| 282 |
# Store results in session state
|
| 283 |
st.session_state.report = report
|
| 284 |
-
st.session_state.charts = charts
|
| 285 |
st.session_state.current_tab = "report"
|
| 286 |
|
| 287 |
# Switch to report tab
|
|
@@ -291,14 +307,15 @@ def main():
|
|
| 291 |
if hasattr(st.session_state, 'report'):
|
| 292 |
st.subheader("📊 Market Research Report")
|
| 293 |
|
| 294 |
-
# Display report sections
|
| 295 |
-
st.
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
| 302 |
|
| 303 |
# Download button
|
| 304 |
st.download_button(
|
|
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
+
import re
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from crewai import Agent, Crew, Process, Task
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
| 11 |
|
|
|
|
| 69 |
100% { transform: translateX(100%); opacity: 0; }
|
| 70 |
}
|
| 71 |
|
| 72 |
+
/* Progress animation */
|
| 73 |
+
@keyframes progress {
|
| 74 |
+
0% { width: 0; }
|
| 75 |
+
100% { width: 100%; }
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
.progress-bar {
|
| 79 |
+
height: 20px;
|
| 80 |
+
background-color: #f0f0f0;
|
| 81 |
border-radius: 10px;
|
| 82 |
+
overflow: hidden;
|
| 83 |
margin: 10px 0;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.progress-fill {
|
| 87 |
+
height: 100%;
|
| 88 |
+
background-color: #4CAF50;
|
| 89 |
+
animation: progress 2s ease-in-out;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
/* Agent status indicators */
|
| 93 |
+
.agent-status {
|
| 94 |
+
padding: 10px;
|
| 95 |
+
margin: 5px 0;
|
| 96 |
+
border-radius: 5px;
|
| 97 |
+
background: #f8f9fa;
|
| 98 |
+
border-left: 4px solid #4CAF50;
|
| 99 |
+
animation: slideIn 0.3s ease-out;
|
| 100 |
}
|
| 101 |
|
| 102 |
@keyframes slideIn {
|
|
|
|
| 121 |
.stTabs [aria-selected="true"] {
|
| 122 |
background-color: #e6f3ff;
|
| 123 |
}
|
| 124 |
+
|
| 125 |
+
/* Report container */
|
| 126 |
+
.report-section {
|
| 127 |
+
background: white;
|
| 128 |
+
padding: 20px;
|
| 129 |
+
border-radius: 10px;
|
| 130 |
+
margin: 10px 0;
|
| 131 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 132 |
+
}
|
| 133 |
</style>
|
| 134 |
""", unsafe_allow_html=True)
|
| 135 |
|
|
|
|
| 149 |
</div>
|
| 150 |
""", unsafe_allow_html=True)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
def run_market_research(topic: str, progress_bar, chat_container):
|
| 153 |
try:
|
| 154 |
# Create agents
|
|
|
|
| 173 |
verbose=True
|
| 174 |
)
|
| 175 |
|
| 176 |
+
# Update progress
|
| 177 |
progress_bar.progress(0, "Initializing research...")
|
| 178 |
+
chat_container.markdown("""
|
| 179 |
+
<div class="agent-status">
|
| 180 |
+
🔍 Research Analyst: Starting market analysis...
|
| 181 |
+
</div>
|
| 182 |
+
""", unsafe_allow_html=True)
|
| 183 |
time.sleep(1)
|
| 184 |
|
| 185 |
# Research Phase
|
| 186 |
research_task = Task(
|
| 187 |
+
description=f"""
|
| 188 |
+
Conduct comprehensive market research on {topic}.
|
| 189 |
+
Focus on:
|
| 190 |
+
1. Current market size and growth projections
|
| 191 |
+
2. Key players and their market share
|
| 192 |
+
3. Consumer adoption trends
|
| 193 |
+
4. Regulatory environment
|
| 194 |
+
""",
|
| 195 |
agent=researcher,
|
| 196 |
expected_output="A comprehensive research document with market analysis."
|
| 197 |
)
|
| 198 |
|
| 199 |
+
progress_bar.progress(33, "Analyzing data...")
|
| 200 |
+
chat_container.markdown("""
|
| 201 |
+
<div class="agent-status">
|
| 202 |
+
📊 Data Analyst: Processing research findings...
|
| 203 |
+
</div>
|
| 204 |
+
""", unsafe_allow_html=True)
|
| 205 |
time.sleep(1)
|
| 206 |
|
| 207 |
# Analysis Phase
|
| 208 |
analysis_task = Task(
|
| 209 |
+
description=f"""
|
| 210 |
+
Analyze the research findings for {topic} and identify:
|
| 211 |
+
1. Key market opportunities
|
| 212 |
+
2. Potential challenges
|
| 213 |
+
3. Growth drivers
|
| 214 |
+
4. Competitive dynamics
|
| 215 |
+
""",
|
| 216 |
agent=analyst,
|
| 217 |
expected_output="An analytical report with key insights.",
|
| 218 |
context=[research_task]
|
| 219 |
)
|
| 220 |
|
| 221 |
+
progress_bar.progress(66, "Generating report...")
|
| 222 |
+
chat_container.markdown("""
|
| 223 |
+
<div class="agent-status">
|
| 224 |
+
✍️ Report Writer: Compiling final report...
|
| 225 |
+
</div>
|
| 226 |
+
""", unsafe_allow_html=True)
|
| 227 |
time.sleep(1)
|
| 228 |
|
| 229 |
# Report Phase
|
| 230 |
report_task = Task(
|
| 231 |
+
description=f"""
|
| 232 |
+
Create a detailed market research report that includes:
|
| 233 |
+
1. Executive summary
|
| 234 |
+
2. Market overview
|
| 235 |
+
3. Key findings
|
| 236 |
+
4. Strategic recommendations
|
| 237 |
+
""",
|
| 238 |
agent=writer,
|
| 239 |
expected_output="A complete market research report in markdown format.",
|
| 240 |
context=[research_task, analysis_task]
|
| 241 |
)
|
| 242 |
|
|
|
|
|
|
|
| 243 |
# Create and run the crew
|
| 244 |
crew = Crew(
|
| 245 |
agents=[researcher, analyst, writer],
|
|
|
|
| 257 |
else:
|
| 258 |
final_report = str(result)
|
| 259 |
|
| 260 |
+
return final_report
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
except Exception as e:
|
| 263 |
st.error(f"Error: {str(e)}")
|
| 264 |
+
return None
|
| 265 |
|
| 266 |
def main():
|
| 267 |
st.title("🤖 AI Market Research Generator")
|
|
|
|
| 293 |
create_agent_flow(col2)
|
| 294 |
|
| 295 |
# Run the research
|
| 296 |
+
report = run_market_research(topic, progress, chat_container)
|
| 297 |
|
| 298 |
if report:
|
| 299 |
# Store results in session state
|
| 300 |
st.session_state.report = report
|
|
|
|
| 301 |
st.session_state.current_tab = "report"
|
| 302 |
|
| 303 |
# Switch to report tab
|
|
|
|
| 307 |
if hasattr(st.session_state, 'report'):
|
| 308 |
st.subheader("📊 Market Research Report")
|
| 309 |
|
| 310 |
+
# Display report in sections
|
| 311 |
+
sections = st.session_state.report.split('\n\n')
|
| 312 |
+
for section in sections:
|
| 313 |
+
if section.strip():
|
| 314 |
+
st.markdown(f"""
|
| 315 |
+
<div class="report-section">
|
| 316 |
+
{section}
|
| 317 |
+
</div>
|
| 318 |
+
""", unsafe_allow_html=True)
|
| 319 |
|
| 320 |
# Download button
|
| 321 |
st.download_button(
|