Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,101 @@
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
-
import
|
| 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 |
|
|
@@ -305,71 +395,102 @@ def run_market_research(topic: str, progress_bar, chat_container):
|
|
| 305 |
def main():
|
| 306 |
st.title("🤖 AI Market Research Generator")
|
| 307 |
|
| 308 |
-
if 'current_tab' not in st.session_state:
|
| 309 |
-
st.session_state.current_tab = "generate"
|
| 310 |
-
|
| 311 |
# Create tabs
|
| 312 |
-
tab1, tab2 = st.tabs(["Generate Report", "View
|
| 313 |
|
| 314 |
with tab1:
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
placeholder="e.g., Electric Vehicles Market",
|
| 323 |
-
key="research_topic"
|
| 324 |
-
)
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
# Create progress bar and containers
|
| 332 |
-
progress = st.progress(0)
|
| 333 |
-
chat_container = st.container()
|
| 334 |
|
| 335 |
-
#
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
if report:
|
| 342 |
-
# Store results in session state
|
| 343 |
-
st.session_state.report = report
|
| 344 |
-
st.session_state.current_tab = "report"
|
| 345 |
-
st.rerun() # Using st.rerun() instead of experimental_rerun
|
| 346 |
|
| 347 |
with tab2:
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
-
# Display report
|
| 352 |
-
|
| 353 |
-
for section in sections:
|
| 354 |
-
if section.strip():
|
| 355 |
-
st.markdown(f"""
|
| 356 |
-
<div class="report-section">
|
| 357 |
-
{section}
|
| 358 |
-
</div>
|
| 359 |
-
""", unsafe_allow_html=True)
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
st.
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
if __name__ == "__main__":
|
| 375 |
main()
|
|
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
+
from utils import ReportManager
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from crewai import Agent, Crew, Process, Task
|
| 7 |
|
| 8 |
+
# Initialize report manager
|
| 9 |
+
report_manager = ReportManager()
|
| 10 |
+
|
| 11 |
+
def create_research_crew(topic: str):
|
| 12 |
+
researcher = Agent(
|
| 13 |
+
role='Research Analyst',
|
| 14 |
+
goal=f'Conduct thorough market research about {topic} with verifiable sources',
|
| 15 |
+
backstory='You are an experienced market research analyst with expertise in data analysis and trend identification. You always provide specific numbers and data points with sources.',
|
| 16 |
+
verbose=True
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
analyst = Agent(
|
| 20 |
+
role='Data Analyst',
|
| 21 |
+
goal='Create data-driven insights with specific metrics and visualizable data points',
|
| 22 |
+
backstory='You are a skilled data analyst who specializes in turning research into actionable insights. You focus on quantifiable metrics and clear data points that can be visualized.',
|
| 23 |
+
verbose=True
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
writer = Agent(
|
| 27 |
+
role='Report Writer',
|
| 28 |
+
goal='Create both executive summary and detailed reports with proper citations',
|
| 29 |
+
backstory='You are a professional writer who creates clear, structured reports with both high-level summaries and detailed analysis. You always include sources and references.',
|
| 30 |
+
verbose=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Define tasks with improved prompts
|
| 34 |
+
research_task = Task(
|
| 35 |
+
description=f"""
|
| 36 |
+
Research {topic} with a focus on:
|
| 37 |
+
1. Market size (provide specific numbers)
|
| 38 |
+
2. Growth rates and projections
|
| 39 |
+
3. Key players and market share percentages
|
| 40 |
+
4. Competitive analysis
|
| 41 |
+
5. Find and verify credible sources for all data points
|
| 42 |
+
|
| 43 |
+
Format data in a way that can be easily visualized.
|
| 44 |
+
Include URLs and references for all sources.
|
| 45 |
+
""",
|
| 46 |
+
agent=researcher,
|
| 47 |
+
expected_output="Detailed research data with sources and numerical data points"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
analysis_task = Task(
|
| 51 |
+
description=f"""
|
| 52 |
+
Analyze the research findings and:
|
| 53 |
+
1. Create growth projections with specific percentages
|
| 54 |
+
2. Break down market share data
|
| 55 |
+
3. Identify key trends with supporting data
|
| 56 |
+
4. Analyze competitive landscape with market positions
|
| 57 |
+
|
| 58 |
+
Present all data in a format suitable for charts and graphs.
|
| 59 |
+
""",
|
| 60 |
+
agent=analyst,
|
| 61 |
+
expected_output="Analysis with specific metrics and visualization-ready data",
|
| 62 |
+
context=[research_task]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
report_task = Task(
|
| 66 |
+
description=f"""
|
| 67 |
+
Create two report versions:
|
| 68 |
+
1. Executive Summary (2-3 paragraphs)
|
| 69 |
+
- Key findings and recommendations
|
| 70 |
+
- Major metrics and insights
|
| 71 |
+
- Strategic implications
|
| 72 |
+
|
| 73 |
+
2. Detailed Report
|
| 74 |
+
- Comprehensive market analysis
|
| 75 |
+
- Detailed data presentation
|
| 76 |
+
- Competitive analysis
|
| 77 |
+
- Future projections
|
| 78 |
+
- Source citations and references
|
| 79 |
+
|
| 80 |
+
Format the output as JSON with the following structure:
|
| 81 |
+
{{
|
| 82 |
+
"exec_summary": "executive summary text",
|
| 83 |
+
"detailed_report": "detailed report text",
|
| 84 |
+
"sources": ["source1", "source2", ...]
|
| 85 |
+
}}
|
| 86 |
+
""",
|
| 87 |
+
agent=writer,
|
| 88 |
+
expected_output="JSON containing exec summary, detailed report, and sources",
|
| 89 |
+
context=[research_task, analysis_task]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return Crew(
|
| 93 |
+
agents=[researcher, analyst, writer],
|
| 94 |
+
tasks=[research_task, analysis_task, report_task],
|
| 95 |
+
verbose=True,
|
| 96 |
+
process=Process.sequential
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
# Load environment variables
|
| 100 |
load_dotenv()
|
| 101 |
|
|
|
|
| 395 |
def main():
|
| 396 |
st.title("🤖 AI Market Research Generator")
|
| 397 |
|
|
|
|
|
|
|
|
|
|
| 398 |
# Create tabs
|
| 399 |
+
tab1, tab2, tab3 = st.tabs(["Generate Report", "View Reports", "About"])
|
| 400 |
|
| 401 |
with tab1:
|
| 402 |
+
col1, col2 = st.columns([2, 3])
|
| 403 |
+
|
| 404 |
+
with col1:
|
| 405 |
+
st.subheader("Enter Research Topic")
|
| 406 |
+
topic = st.text_input(
|
| 407 |
+
"What market would you like to research?",
|
| 408 |
+
placeholder="e.g., Electric Vehicles Market"
|
| 409 |
+
)
|
| 410 |
|
| 411 |
+
if st.button("Generate Report", type="primary"):
|
| 412 |
+
if not topic:
|
| 413 |
+
st.error("Please enter a research topic")
|
| 414 |
+
return
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
+
with st.spinner("Generating report..."):
|
| 417 |
+
# Create and run crew
|
| 418 |
+
crew = create_research_crew(topic)
|
| 419 |
+
result = crew.kickoff()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
+
# Process and save report
|
| 422 |
+
try:
|
| 423 |
+
report_data = json.loads(result)
|
| 424 |
+
# Generate visualizations
|
| 425 |
+
charts = report_manager.generate_visualizations(report_data['detailed_report'])
|
| 426 |
+
report_data['charts'] = charts
|
| 427 |
+
|
| 428 |
+
# Save report
|
| 429 |
+
report_id = report_manager.save_report(topic, report_data)
|
| 430 |
+
st.success("Report generated successfully!")
|
| 431 |
+
|
| 432 |
+
# Switch to the view tab
|
| 433 |
+
st.session_state.current_report = report_id
|
| 434 |
+
st.rerun()
|
| 435 |
|
| 436 |
+
except Exception as e:
|
| 437 |
+
st.error(f"Error processing report: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
|
| 439 |
with tab2:
|
| 440 |
+
st.subheader("Saved Reports")
|
| 441 |
+
reports = report_manager.get_all_reports()
|
| 442 |
+
|
| 443 |
+
if not reports:
|
| 444 |
+
st.info("No reports generated yet. Create your first report in the Generate tab!")
|
| 445 |
+
return
|
| 446 |
+
|
| 447 |
+
# Report selection
|
| 448 |
+
report_options = {f"{data['topic']} ({data['timestamp']})": rid
|
| 449 |
+
for rid, data in reports.items()}
|
| 450 |
+
selected_report = st.selectbox(
|
| 451 |
+
"Select a report to view",
|
| 452 |
+
options=list(report_options.keys())
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
if selected_report:
|
| 456 |
+
report_id = report_options[selected_report]
|
| 457 |
+
report = report_manager.get_report(report_id)
|
| 458 |
|
| 459 |
+
# Display report
|
| 460 |
+
col1, col2 = st.columns([2, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
|
| 462 |
+
with col1:
|
| 463 |
+
st.subheader("Executive Summary")
|
| 464 |
+
st.markdown(report['exec_summary'])
|
| 465 |
+
|
| 466 |
+
st.subheader("Detailed Report")
|
| 467 |
+
st.markdown(report['detailed_report'])
|
| 468 |
+
|
| 469 |
+
st.subheader("Sources")
|
| 470 |
+
for source in report['sources']:
|
| 471 |
+
st.markdown(f"- {source}")
|
| 472 |
|
| 473 |
+
with col2:
|
| 474 |
+
st.subheader("Visualizations")
|
| 475 |
+
for chart in report['charts']:
|
| 476 |
+
st.plotly_chart(chart, use_container_width=True)
|
| 477 |
+
|
| 478 |
+
# Download buttons
|
| 479 |
+
col1, col2 = st.columns(2)
|
| 480 |
+
with col1:
|
| 481 |
+
st.download_button(
|
| 482 |
+
"Download as PDF",
|
| 483 |
+
data=report_manager.generate_pdf(report_id),
|
| 484 |
+
file_name=f"{report['topic']}_report.pdf",
|
| 485 |
+
mime="application/pdf"
|
| 486 |
+
)
|
| 487 |
+
with col2:
|
| 488 |
+
st.download_button(
|
| 489 |
+
"Download as Markdown",
|
| 490 |
+
data=report['detailed_report'],
|
| 491 |
+
file_name=f"{report['topic']}_report.md",
|
| 492 |
+
mime="text/markdown"
|
| 493 |
+
)
|
| 494 |
|
| 495 |
if __name__ == "__main__":
|
| 496 |
main()
|