Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -201,6 +201,94 @@ def update_progress(container, progress, message=""):
|
|
| 201 |
"""
|
| 202 |
container.markdown(progress_html, unsafe_allow_html=True)
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
def run_market_research(topic: str, progress_container, chat_container):
|
| 205 |
"""Run the market research process"""
|
| 206 |
try:
|
|
@@ -227,21 +315,18 @@ def run_market_research(topic: str, progress_container, chat_container):
|
|
| 227 |
display_agent_message(chat_container, "writer",
|
| 228 |
"Compiling final report with executive summary and detailed analysis...")
|
| 229 |
update_progress(progress_container, 75, "Generating report...")
|
| 230 |
-
time.sleep(1)
|
| 231 |
|
| 232 |
-
#
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
}
|
| 243 |
|
| 244 |
-
# Update progress and display completion message
|
| 245 |
update_progress(progress_container, 100, "Report completed!")
|
| 246 |
display_agent_message(chat_container, "writer",
|
| 247 |
"✨ Report generation completed! You can now view the full report.")
|
|
@@ -352,5 +437,110 @@ def main():
|
|
| 352 |
for source in report['sources']:
|
| 353 |
st.markdown(f"- {source}")
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
if __name__ == "__main__":
|
| 356 |
main()
|
|
|
|
| 201 |
"""
|
| 202 |
container.markdown(progress_html, unsafe_allow_html=True)
|
| 203 |
|
| 204 |
+
def create_research_crew(topic: str):
|
| 205 |
+
"""Create the research crew with enhanced prompts"""
|
| 206 |
+
researcher = Agent(
|
| 207 |
+
role='Research Analyst',
|
| 208 |
+
goal=f'Conduct comprehensive market research about {topic} with detailed metrics and verifiable sources',
|
| 209 |
+
backstory="You are an experienced market research analyst who focuses on gathering concrete data points and always verifies sources.",
|
| 210 |
+
verbose=True
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
analyst = Agent(
|
| 214 |
+
role='Data Analyst',
|
| 215 |
+
goal='Transform research data into actionable insights with visualization-ready metrics',
|
| 216 |
+
backstory="You are a skilled data analyst who excels at interpreting market research and creating data-driven insights.",
|
| 217 |
+
verbose=True
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
writer = Agent(
|
| 221 |
+
role='Report Writer',
|
| 222 |
+
goal='Create professional market research reports with executive summary and detailed analysis',
|
| 223 |
+
backstory="You are an expert business writer who specializes in creating clear, comprehensive market research reports.",
|
| 224 |
+
verbose=True
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
research_task = Task(
|
| 228 |
+
description=f"""
|
| 229 |
+
Conduct extensive market research on {topic} with:
|
| 230 |
+
1. Market Overview (market size, growth rates, projections)
|
| 231 |
+
2. Competitive Analysis (market shares, positioning)
|
| 232 |
+
3. Market Dynamics (drivers, challenges, trends)
|
| 233 |
+
|
| 234 |
+
Provide specific numerical values and sources.
|
| 235 |
+
Format data for visualization.
|
| 236 |
+
""",
|
| 237 |
+
agent=researcher,
|
| 238 |
+
expected_output="Comprehensive research data with specific metrics and sources"
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
analysis_task = Task(
|
| 242 |
+
description=f"""
|
| 243 |
+
Analyze the findings and provide:
|
| 244 |
+
1. Growth projections and trends (5 years)
|
| 245 |
+
2. Market share analysis
|
| 246 |
+
3. Competitive landscape
|
| 247 |
+
4. Strategic recommendations
|
| 248 |
+
|
| 249 |
+
Include specific numbers and percentages.
|
| 250 |
+
""",
|
| 251 |
+
agent=analyst,
|
| 252 |
+
expected_output="Detailed analysis with visualization-ready metrics",
|
| 253 |
+
context=[research_task]
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
report_task = Task(
|
| 257 |
+
description=f"""
|
| 258 |
+
Create a professional report with:
|
| 259 |
+
1. Executive Summary (2-3 pages)
|
| 260 |
+
2. Detailed Report (10+ pages)
|
| 261 |
+
3. Sources and Citations
|
| 262 |
+
|
| 263 |
+
Format as JSON:
|
| 264 |
+
{{
|
| 265 |
+
"exec_summary": {{
|
| 266 |
+
"summary": "text",
|
| 267 |
+
"market_size": "data",
|
| 268 |
+
"growth_rate": "data",
|
| 269 |
+
"key_players": "data"
|
| 270 |
+
}},
|
| 271 |
+
"detailed_report": "text",
|
| 272 |
+
"sources": ["source1", "source2"],
|
| 273 |
+
"metrics": {{
|
| 274 |
+
"market_size_data": [],
|
| 275 |
+
"growth_rates": [],
|
| 276 |
+
"market_shares": []
|
| 277 |
+
}}
|
| 278 |
+
}}
|
| 279 |
+
""",
|
| 280 |
+
agent=writer,
|
| 281 |
+
expected_output="JSON containing executive summary and detailed report",
|
| 282 |
+
context=[research_task, analysis_task]
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
return Crew(
|
| 286 |
+
agents=[researcher, analyst, writer],
|
| 287 |
+
tasks=[research_task, analysis_task, report_task],
|
| 288 |
+
verbose=True,
|
| 289 |
+
process=Process.sequential
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
def run_market_research(topic: str, progress_container, chat_container):
|
| 293 |
"""Run the market research process"""
|
| 294 |
try:
|
|
|
|
| 315 |
display_agent_message(chat_container, "writer",
|
| 316 |
"Compiling final report with executive summary and detailed analysis...")
|
| 317 |
update_progress(progress_container, 75, "Generating report...")
|
|
|
|
| 318 |
|
| 319 |
+
# Create and run the crew
|
| 320 |
+
crew = create_research_crew(topic)
|
| 321 |
+
result = crew.kickoff()
|
| 322 |
+
|
| 323 |
+
# Format and process the result
|
| 324 |
+
report_data = format_json_output(result)
|
| 325 |
+
|
| 326 |
+
# Add visualizations
|
| 327 |
+
charts = generate_visualizations(report_data)
|
| 328 |
+
report_data['charts'] = charts
|
|
|
|
| 329 |
|
|
|
|
| 330 |
update_progress(progress_container, 100, "Report completed!")
|
| 331 |
display_agent_message(chat_container, "writer",
|
| 332 |
"✨ Report generation completed! You can now view the full report.")
|
|
|
|
| 437 |
for source in report['sources']:
|
| 438 |
st.markdown(f"- {source}")
|
| 439 |
|
| 440 |
+
def format_json_output(raw_output):
|
| 441 |
+
"""Format CrewOutput or raw string into proper JSON structure"""
|
| 442 |
+
try:
|
| 443 |
+
# Handle CrewOutput object
|
| 444 |
+
if hasattr(raw_output, 'raw_output'):
|
| 445 |
+
raw_text = str(raw_output.raw_output)
|
| 446 |
+
else:
|
| 447 |
+
raw_text = str(raw_output)
|
| 448 |
+
|
| 449 |
+
# Try to find and parse JSON structure
|
| 450 |
+
json_pattern = r"\{[\s\S]*\}"
|
| 451 |
+
match = re.search(json_pattern, raw_text)
|
| 452 |
+
if match:
|
| 453 |
+
try:
|
| 454 |
+
return json.loads(match.group())
|
| 455 |
+
except:
|
| 456 |
+
pass
|
| 457 |
+
|
| 458 |
+
# If no JSON found, create structured format
|
| 459 |
+
return {
|
| 460 |
+
"exec_summary": {
|
| 461 |
+
"summary": extract_section(raw_text, "Executive Summary"),
|
| 462 |
+
"market_size": extract_section(raw_text, "Market Size"),
|
| 463 |
+
"growth_rate": extract_section(raw_text, "Growth Rate"),
|
| 464 |
+
"key_players": extract_section(raw_text, "Key Players")
|
| 465 |
+
},
|
| 466 |
+
"detailed_report": raw_text,
|
| 467 |
+
"sources": extract_sources(raw_text),
|
| 468 |
+
"metrics": {
|
| 469 |
+
"market_size_data": [],
|
| 470 |
+
"growth_rates": [],
|
| 471 |
+
"market_shares": []
|
| 472 |
+
}
|
| 473 |
+
}
|
| 474 |
+
except Exception as e:
|
| 475 |
+
st.error(f"Error formatting output: {str(e)}")
|
| 476 |
+
return {
|
| 477 |
+
"exec_summary": {
|
| 478 |
+
"summary": "Error formatting report",
|
| 479 |
+
"market_size": "N/A",
|
| 480 |
+
"growth_rate": "N/A",
|
| 481 |
+
"key_players": "N/A"
|
| 482 |
+
},
|
| 483 |
+
"detailed_report": raw_text if 'raw_text' in locals() else str(raw_output),
|
| 484 |
+
"sources": [],
|
| 485 |
+
"metrics": {
|
| 486 |
+
"market_size_data": [],
|
| 487 |
+
"growth_rates": [],
|
| 488 |
+
"market_shares": []
|
| 489 |
+
}
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
def extract_section(text, section_name):
|
| 493 |
+
"""Extract a section from the text"""
|
| 494 |
+
pattern = f"{section_name}.*?\n(.*?)(?=\n\n|$)"
|
| 495 |
+
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
|
| 496 |
+
return match.group(1).strip() if match else ""
|
| 497 |
+
|
| 498 |
+
def extract_sources(text):
|
| 499 |
+
"""Extract sources from the text"""
|
| 500 |
+
sources = []
|
| 501 |
+
source_pattern = r"Source:.*?(?:\n|$)|\[.*?\]|\(https?://.*?\)"
|
| 502 |
+
matches = re.finditer(source_pattern, text, re.MULTILINE)
|
| 503 |
+
return [match.group().strip() for match in matches]
|
| 504 |
+
|
| 505 |
+
def generate_visualizations(data):
|
| 506 |
+
"""Generate visualizations based on the report data"""
|
| 507 |
+
charts = []
|
| 508 |
+
|
| 509 |
+
# Market Size Chart
|
| 510 |
+
if data['metrics']['market_size_data']:
|
| 511 |
+
fig = go.Figure(data=[
|
| 512 |
+
go.Bar(
|
| 513 |
+
x=['Current', 'Projected'],
|
| 514 |
+
y=data['metrics']['market_size_data'],
|
| 515 |
+
text=data['metrics']['market_size_data'],
|
| 516 |
+
textposition='auto',
|
| 517 |
+
)
|
| 518 |
+
])
|
| 519 |
+
fig.update_layout(title='Market Size Projection')
|
| 520 |
+
charts.append(fig)
|
| 521 |
+
|
| 522 |
+
# Growth Rate Chart
|
| 523 |
+
if data['metrics']['growth_rates']:
|
| 524 |
+
fig = go.Figure(data=[
|
| 525 |
+
go.Scatter(
|
| 526 |
+
x=list(range(len(data['metrics']['growth_rates']))),
|
| 527 |
+
y=data['metrics']['growth_rates'],
|
| 528 |
+
mode='lines+markers'
|
| 529 |
+
)
|
| 530 |
+
])
|
| 531 |
+
fig.update_layout(title='Growth Rate Trends')
|
| 532 |
+
charts.append(fig)
|
| 533 |
+
|
| 534 |
+
# Market Share Chart
|
| 535 |
+
if data['metrics']['market_shares']:
|
| 536 |
+
fig = go.Figure(data=[go.Pie(
|
| 537 |
+
labels=list(data['metrics']['market_shares'].keys()),
|
| 538 |
+
values=list(data['metrics']['market_shares'].values())
|
| 539 |
+
)])
|
| 540 |
+
fig.update_layout(title='Market Share Distribution')
|
| 541 |
+
charts.append(fig)
|
| 542 |
+
|
| 543 |
+
return charts
|
| 544 |
+
|
| 545 |
if __name__ == "__main__":
|
| 546 |
main()
|