Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
# app.py
|
| 4 |
import os
|
| 5 |
-
import yaml
|
| 6 |
import streamlit as st
|
| 7 |
from typing import List
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from crewai import Agent, Crew, Process, Task
|
| 10 |
-
from crewai.project import CrewBase, agent, crew, task
|
| 11 |
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
|
| 12 |
-
from pydantic import BaseModel, Field
|
| 13 |
|
| 14 |
# Load environment variables
|
| 15 |
load_dotenv()
|
|
@@ -37,103 +32,105 @@ st.markdown("""
|
|
| 37 |
</style>
|
| 38 |
""", unsafe_allow_html=True)
|
| 39 |
|
| 40 |
-
# Define output models
|
| 41 |
-
class MarketResearchReport(BaseModel):
|
| 42 |
-
"""Market research report model"""
|
| 43 |
-
title: str = Field(..., description="Title of the report")
|
| 44 |
-
executive_summary: str = Field(..., description="Executive summary of the report")
|
| 45 |
-
market_overview: str = Field(..., description="Market overview section")
|
| 46 |
-
key_findings: List[str] = Field(..., description="List of key findings")
|
| 47 |
-
recommendations: List[str] = Field(..., description="List of strategic recommendations")
|
| 48 |
-
|
| 49 |
-
@CrewBase
|
| 50 |
-
class MarketResearchCrew:
|
| 51 |
-
"""Market Research crew"""
|
| 52 |
-
agents_config = 'config/agents.yaml'
|
| 53 |
-
tasks_config = 'config/tasks.yaml'
|
| 54 |
-
|
| 55 |
-
@agent
|
| 56 |
-
def research_analyst(self) -> Agent:
|
| 57 |
-
return Agent(
|
| 58 |
-
config=self.agents_config['research_analyst'],
|
| 59 |
-
tools=[SerperDevTool(), ScrapeWebsiteTool()],
|
| 60 |
-
verbose=True,
|
| 61 |
-
memory=False,
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
@agent
|
| 65 |
-
def data_analyst(self) -> Agent:
|
| 66 |
-
return Agent(
|
| 67 |
-
config=self.agents_config['data_analyst'],
|
| 68 |
-
tools=[SerperDevTool()],
|
| 69 |
-
verbose=True,
|
| 70 |
-
memory=False,
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
@agent
|
| 74 |
-
def report_writer(self) -> Agent:
|
| 75 |
-
return Agent(
|
| 76 |
-
config=self.agents_config['report_writer'],
|
| 77 |
-
verbose=True,
|
| 78 |
-
memory=False,
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
@task
|
| 82 |
-
def research_task(self) -> Task:
|
| 83 |
-
return Task(
|
| 84 |
-
config=self.tasks_config['research_task'],
|
| 85 |
-
agent=self.research_analyst()
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
@task
|
| 89 |
-
def analysis_task(self) -> Task:
|
| 90 |
-
return Task(
|
| 91 |
-
config=self.tasks_config['analysis_task'],
|
| 92 |
-
agent=self.data_analyst(),
|
| 93 |
-
context=[self.research_task()]
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
@task
|
| 97 |
-
def report_task(self) -> Task:
|
| 98 |
-
return Task(
|
| 99 |
-
config=self.tasks_config['report_task'],
|
| 100 |
-
agent=self.report_writer(),
|
| 101 |
-
context=[self.research_task(), self.analysis_task()],
|
| 102 |
-
output_json=MarketResearchReport
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
@crew
|
| 106 |
-
def crew(self) -> Crew:
|
| 107 |
-
"""Creates the Market Research crew"""
|
| 108 |
-
return Crew(
|
| 109 |
-
agents=self.agents,
|
| 110 |
-
tasks=self.tasks,
|
| 111 |
-
process=Process.sequential,
|
| 112 |
-
verbose=2,
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
def run_market_research(topic: str):
|
| 116 |
with st.status("🤖 AI Crew working on your report...", expanded=True) as status:
|
| 117 |
try:
|
| 118 |
-
status.update(label="🔍
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
"""
|
| 129 |
-
}
|
| 130 |
|
| 131 |
-
result =
|
| 132 |
status.update(label="✅ Report generated successfully!", state="complete")
|
| 133 |
return result
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
status.update(label=f"❌ Error: {str(e)}", state="error")
|
|
|
|
| 137 |
return None
|
| 138 |
|
| 139 |
def main():
|
|
@@ -146,6 +143,8 @@ def main():
|
|
| 146 |
- ✍️ A Report Writer who creates the final document
|
| 147 |
""")
|
| 148 |
|
|
|
|
|
|
|
| 149 |
topic = st.text_input(
|
| 150 |
"Enter the market or product you want to research",
|
| 151 |
placeholder="e.g., Electric Vehicles Market",
|
|
@@ -166,6 +165,7 @@ def main():
|
|
| 166 |
st.markdown(report)
|
| 167 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 168 |
|
|
|
|
| 169 |
st.download_button(
|
| 170 |
label="Download Report",
|
| 171 |
data=report,
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
import os
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
from typing import List
|
| 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()
|
|
|
|
| 32 |
</style>
|
| 33 |
""", unsafe_allow_html=True)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def run_market_research(topic: str):
|
| 36 |
with st.status("🤖 AI Crew working on your report...", expanded=True) as status:
|
| 37 |
try:
|
| 38 |
+
status.update(label="🔍 Creating AI agents...")
|
| 39 |
+
|
| 40 |
+
# Initialize tools
|
| 41 |
+
search_tool = SerperDevTool()
|
| 42 |
+
scrape_tool = ScrapeWebsiteTool()
|
| 43 |
+
|
| 44 |
+
# Create agents
|
| 45 |
+
researcher = Agent(
|
| 46 |
+
role='Research Analyst',
|
| 47 |
+
goal=f'Conduct thorough market research about {topic}',
|
| 48 |
+
backstory='You are an experienced market research analyst with expertise in data analysis and trend identification.',
|
| 49 |
+
tools=[search_tool, scrape_tool],
|
| 50 |
+
verbose=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
analyst = Agent(
|
| 54 |
+
role='Data Analyst',
|
| 55 |
+
goal='Analyze research findings and identify key insights',
|
| 56 |
+
backstory='You are a skilled data analyst with expertise in interpreting market research and creating actionable insights.',
|
| 57 |
+
tools=[search_tool],
|
| 58 |
+
verbose=True
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
writer = Agent(
|
| 62 |
+
role='Report Writer',
|
| 63 |
+
goal='Create comprehensive and engaging market research reports',
|
| 64 |
+
backstory='You are a professional writer specializing in creating clear and compelling business reports.',
|
| 65 |
+
verbose=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
status.update(label="📋 Defining research tasks...")
|
| 69 |
+
|
| 70 |
+
# Define tasks with expected outputs
|
| 71 |
+
research_task = Task(
|
| 72 |
+
description=f"""
|
| 73 |
+
Conduct comprehensive market research on {topic}.
|
| 74 |
+
Focus on:
|
| 75 |
+
1. Current market size and growth projections
|
| 76 |
+
2. Key players and their market share
|
| 77 |
+
3. Consumer adoption trends
|
| 78 |
+
4. Regulatory environment
|
| 79 |
+
|
| 80 |
+
Output should be a detailed research findings document with data and sources.
|
| 81 |
+
""",
|
| 82 |
+
agent=researcher,
|
| 83 |
+
expected_output="A comprehensive research document containing market data, trends, and competitor analysis with clear citations and sources."
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
analysis_task = Task(
|
| 87 |
+
description=f"""
|
| 88 |
+
Analyze the research findings for {topic} and identify:
|
| 89 |
+
1. Key market opportunities
|
| 90 |
+
2. Potential challenges
|
| 91 |
+
3. Growth drivers
|
| 92 |
+
4. Competitive dynamics
|
| 93 |
+
|
| 94 |
+
Use the research provided by the Research Analyst to create an analytical report.
|
| 95 |
+
""",
|
| 96 |
+
agent=analyst,
|
| 97 |
+
expected_output="An analytical report with key insights, opportunities, challenges, and strategic implications based on the research data.",
|
| 98 |
+
context=[research_task]
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
report_task = Task(
|
| 102 |
+
description=f"""
|
| 103 |
+
Create a detailed market research report that includes:
|
| 104 |
+
1. Executive summary
|
| 105 |
+
2. Market overview
|
| 106 |
+
3. Key findings
|
| 107 |
+
4. Strategic recommendations
|
| 108 |
+
|
| 109 |
+
Base the report on the research and analysis provided by the other agents.
|
| 110 |
+
The report should be well-structured, professional, and actionable.
|
| 111 |
+
""",
|
| 112 |
+
agent=writer,
|
| 113 |
+
expected_output="A complete market research report in markdown format with executive summary, findings, and recommendations.",
|
| 114 |
+
context=[research_task, analysis_task]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
status.update(label="🚀 Assembling the crew and starting research...")
|
| 118 |
|
| 119 |
+
# Create and run the crew
|
| 120 |
+
crew = Crew(
|
| 121 |
+
agents=[researcher, analyst, writer],
|
| 122 |
+
tasks=[research_task, analysis_task, report_task],
|
| 123 |
+
verbose=2,
|
| 124 |
+
process=Process.sequential
|
| 125 |
+
)
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
result = crew.kickoff()
|
| 128 |
status.update(label="✅ Report generated successfully!", state="complete")
|
| 129 |
return result
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
status.update(label=f"❌ Error: {str(e)}", state="error")
|
| 133 |
+
st.error(f"Error details: {str(e)}")
|
| 134 |
return None
|
| 135 |
|
| 136 |
def main():
|
|
|
|
| 143 |
- ✍️ A Report Writer who creates the final document
|
| 144 |
""")
|
| 145 |
|
| 146 |
+
# Input section
|
| 147 |
+
st.subheader("Research Topic")
|
| 148 |
topic = st.text_input(
|
| 149 |
"Enter the market or product you want to research",
|
| 150 |
placeholder="e.g., Electric Vehicles Market",
|
|
|
|
| 165 |
st.markdown(report)
|
| 166 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 167 |
|
| 168 |
+
# Add download button
|
| 169 |
st.download_button(
|
| 170 |
label="Download Report",
|
| 171 |
data=report,
|