Spaces:
Sleeping
Sleeping
File size: 10,736 Bytes
d5ad1fb ec07c78 7d6dd45 9f14bb5 4925c76 ec07c78 187e6d0 9f14bb5 a10143f 9f14bb5 d5ad1fb 9f14bb5 d5ad1fb 9f14bb5 d5ad1fb 235e70b 699d379 9f14bb5 7da1f57 03bc604 df45ab2 8aefd62 5da984d 8aefd62 699d379 5da984d 8aefd62 699d379 5da984d 8aefd62 df45ab2 5da984d 40fb758 699d379 8aefd62 235e70b 8aefd62 235e70b 8aefd62 699d379 8aefd62 235e70b 8aefd62 5da984d 235e70b 8aefd62 235e70b 44fed4e 235e70b 8aefd62 235e70b 44fed4e 235e70b 699d379 982e3fa 44fed4e df45ab2 03bc604 9f14bb5 df45ab2 235e70b ec07c78 0d48b65 31fe386 0d48b65 31fe386 0d48b65 0bf80ee c897b65 0d48b65 d5ad1fb 9f14bb5 0d48b65 902bfc2 9f14bb5 0d48b65 9f14bb5 0d48b65 9f14bb5 0d48b65 9f14bb5 0d48b65 9f14bb5 0d48b65 0495c3a 3ae0511 0d48b65 b54644d 248a45e b54644d 248a45e b54644d 248a45e 4b19933 248a45e 4b19933 248a45e 4b19933 248a45e 4b19933 248a45e 4b19933 b54644d 4b19933 b54644d 248a45e b54644d 3ae0511 b54644d 3ae0511 0d48b65 44fed4e 0d48b65 248a45e 44fed4e 0d48b65 44fed4e 0d48b65 248a45e 44fed4e 0d48b65 44fed4e 0d48b65 44fed4e 0d48b65 44fed4e 0d48b65 44fed4e 0d48b65 44fed4e 0d48b65 44fed4e 0d48b65 248a45e 3ae0511 4b19933 3ae0511 c144ee0 0d48b65 b54644d 5c001ed 75f8517 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | # app.py
import os
import streamlit as st
from dotenv import load_dotenv
from agents import create_research_crew
from utils import format_json_output, update_progress
from styles import main_styles
# Load environment variables
load_dotenv()
# Streamlit server configuration for Hugging Face Spaces
st.set_page_config(
page_title="Market Research Generator",
page_icon="๐",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'Get Help': 'https://www.huggingface.co',
'Report a bug': "https://www.huggingface.co",
'About': "Market Research Report Generator using AI"
}
)
# Set the server to allow external connections
from streamlit.web.server.server import Server
Server.address = "0.0.0.0"
# Add styles
st.markdown(main_styles, unsafe_allow_html=True)
def get_api_keys():
"""Get API keys from environment or Hugging Face Secrets"""
try:
serper_key = os.environ.get('SERPER_API_KEY')
openai_key = os.environ.get('OPENAI_API_KEY')
print("API Keys loaded successfully")
return serper_key, openai_key
except Exception as e:
st.error(f"Error loading API keys: {str(e)}")
return None, None
def parse_section_content(content, section_marker):
"""Parse content between section markers"""
if section_marker in content:
start = content.find(section_marker) + len(section_marker)
next_marker = content.find('###', start)
if next_marker != -1:
return content[start:next_marker].strip()
return content[start:].strip()
return ""
def run_market_research(topic: str, progress_container):
"""Run the market research process"""
try:
# Create and run the crew
crew = create_research_crew(topic)
# Update progress
update_progress(progress_container, 25, "Gathering market data...")
st.write("๐ Research Analyst is gathering market data...")
update_progress(progress_container, 50, "Analyzing findings...")
st.write("๐ Data Analyst is processing insights...")
update_progress(progress_container, 75, "Generating report...")
st.write("โ๏ธ Report Writer is creating the document...")
# Execute the crew and get result
result = crew.kickoff()
# Get raw text from result
if hasattr(result, 'raw_output'):
raw_text = str(result.raw_output)
else:
raw_text = str(result)
# Split into main sections
sections = {}
current_section = ""
current_content = []
for line in raw_text.split('\n'):
if line.strip().startswith('==='):
if current_section:
sections[current_section] = '\n'.join(current_content)
current_section = line.strip().replace('===', '').strip()
current_content = []
else:
current_content.append(line)
if current_section and current_content:
sections[current_section] = '\n'.join(current_content)
# Process Executive Summary
exec_summary = sections.get('EXECUTIVE SUMMARY', '')
exec_summary_parts = {
'summary': parse_section_content(exec_summary, 'EXECUTIVE SUMMARY'),
'market_highlights': parse_section_content(exec_summary, '### Key Market Findings'),
'strategic_implications': parse_section_content(exec_summary, '### Strategic Implications'),
'recommendations': parse_section_content(exec_summary, '### Recommendations')
}
# Process Market Analysis
market_analysis = sections.get('MARKET ANALYSIS', '')
market_analysis_parts = {
'overview': parse_section_content(market_analysis, '### Market Overview'),
'dynamics': parse_section_content(market_analysis, '### Industry Dynamics'),
'competitive_landscape': parse_section_content(market_analysis, '### Competitive Landscape'),
'strategic_analysis': parse_section_content(market_analysis, '### Strategic Analysis')
}
# Process Sources
sources_section = sections.get('SOURCES', '')
sources = [
line.strip()[2:] for line in sources_section.split('\n')
if line.strip().startswith('-')
]
# Create final report structure
processed_report = {
'exec_summary': exec_summary_parts,
'market_analysis': market_analysis_parts,
'future_outlook': sections.get('FUTURE OUTLOOK', ''),
'sources': sources
}
# Debug: Print sections found
print("Sections found:", list(sections.keys()))
print("Market Analysis sections:", list(market_analysis_parts.keys()))
update_progress(progress_container, 100, "Report completed!")
return processed_report
except Exception as e:
st.error(f"Error during research: {str(e)}")
print(f"Full error details: {str(e)}") # For debugging
return None
def main():
st.title("๐ค AI Market Research Generator")
# Get API keys
serper_key, openai_key = get_api_keys()
if not serper_key or not openai_key:
st.error("Failed to load required API keys. Please check your Hugging Face Space secrets.")
return
# Initialize session states
if 'generating' not in st.session_state:
st.session_state.generating = False
# Create tabs
tab1, tab2 = st.tabs(["Generate Report", "View Reports"])
with tab1:
st.subheader("Enter Research Topic")
topic = st.text_input(
"What market would you like to research?",
placeholder="e.g., Electric Vehicles Market"
)
if st.session_state.generating:
st.button("Generating Report...", disabled=True)
else:
if st.button("Generate Report", type="primary"):
if not topic:
st.error("Please enter a research topic")
else:
st.session_state.generating = True
# Create progress container
progress_container = st.container()
try:
with st.spinner("Generating comprehensive market research report..."):
result = run_market_research(topic, progress_container)
if result:
st.session_state.current_report = result
st.session_state.current_topic = topic
st.success("Report generated successfully! View it in the Reports tab.")
except Exception as e:
st.error(f"Error generating report: {str(e)}")
finally:
st.session_state.generating = False
with tab2:
if 'current_report' in st.session_state:
try:
report = st.session_state.current_report
topic = st.session_state.current_topic
# Display AI Disclaimer
st.warning("โ ๏ธ This report was generated using AI. While we strive for accuracy, please verify critical information independently.")
# Report Title
st.title(f"๐ Market Research Report: {topic}")
# Executive Summary Section
st.header("Executive Summary")
if report['exec_summary']['summary']:
st.markdown(report['exec_summary']['summary'])
# Key Findings and Implications
col1, col2 = st.columns(2)
with col1:
with st.expander("Key Market Findings", expanded=True):
st.markdown(report['exec_summary']['market_highlights'])
with st.expander("Strategic Implications", expanded=True):
st.markdown(report['exec_summary']['strategic_implications'])
with col2:
with st.expander("Recommendations", expanded=True):
st.markdown(report['exec_summary']['recommendations'])
# Market Analysis Section
st.header("Market Analysis")
# Market Analysis Tabs
market_tabs = st.tabs([
"Market Overview",
"Industry Dynamics",
"Competitive Landscape",
"Strategic Analysis"
])
with market_tabs[0]:
st.markdown(report['market_analysis']['overview'])
with market_tabs[1]:
st.markdown(report['market_analysis']['dynamics'])
with market_tabs[2]:
st.markdown(report['market_analysis']['competitive_landscape'])
with market_tabs[3]:
st.markdown(report['market_analysis']['strategic_analysis'])
# Future Outlook
st.header("Future Outlook")
st.markdown(report['future_outlook'])
# Sources
if report['sources']:
st.header("Sources")
for source in report['sources']:
st.markdown(f"โข {source}")
# Download Report
st.download_button(
label="๐ฅ Download Complete Report",
data=f"""# Market Research Report: {topic}
## Executive Summary
{report['exec_summary']['summary']}
### Key Market Findings
{report['exec_summary']['market_highlights']}
### Strategic Implications
{report['exec_summary']['strategic_implications']}
### Recommendations
{report['exec_summary']['recommendations']}
## Market Analysis
### Market Overview
{report['market_analysis']['overview']}
### Industry Dynamics
{report['market_analysis']['dynamics']}
### Competitive Landscape
{report['market_analysis']['competitive_landscape']}
### Strategic Analysis
{report['market_analysis']['strategic_analysis']}
## Future Outlook
{report['future_outlook']}
## Sources
{chr(10).join([f"โข {source}" for source in report['sources']])}""",
file_name=f"{topic.lower().replace(' ', '_')}_market_research.md",
mime="text/markdown"
)
except Exception as e:
st.error(f"Error displaying report: {str(e)}")
st.write("Raw report data:", report) # Debug information
if __name__ == "__main__":
main() |