Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -212,72 +212,91 @@ def create_research_crew(topic: str):
|
|
| 212 |
process=Process.sequential
|
| 213 |
)
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
def run_market_research(topic: str, progress_container, chat_container, log_container):
|
| 216 |
"""Run the market research process"""
|
| 217 |
try:
|
| 218 |
-
# Clear previous logs
|
| 219 |
st.session_state.agent_logs = []
|
| 220 |
|
| 221 |
-
#
|
| 222 |
update_progress(progress_container, 0, "Initializing research team...")
|
| 223 |
log_agent_activity("System", f"Starting market research for: {topic}")
|
| 224 |
-
time.sleep(1)
|
| 225 |
|
| 226 |
-
#
|
|
|
|
|
|
|
|
|
|
| 227 |
update_progress(progress_container, 25, "Gathering market data...")
|
| 228 |
log_agent_activity("Research Analyst", f"Beginning comprehensive research on {topic}")
|
| 229 |
-
time.sleep(1)
|
| 230 |
-
log_agent_activity("Research Analyst", "Collecting market size data...")
|
| 231 |
-
time.sleep(1)
|
| 232 |
-
log_agent_activity("Research Analyst", "Analyzing competitive landscape...")
|
| 233 |
-
time.sleep(1)
|
| 234 |
-
log_agent_activity("Research Analyst", "Gathering growth projections...")
|
| 235 |
-
time.sleep(1)
|
| 236 |
|
| 237 |
-
#
|
| 238 |
update_progress(progress_container, 50, "Analyzing findings...")
|
| 239 |
-
log_agent_activity("Data Analyst", "
|
| 240 |
-
time.sleep(1)
|
| 241 |
-
log_agent_activity("Data Analyst", "Processing market size metrics...")
|
| 242 |
-
time.sleep(1)
|
| 243 |
-
log_agent_activity("Data Analyst", "Calculating growth rates...")
|
| 244 |
-
time.sleep(1)
|
| 245 |
-
log_agent_activity("Data Analyst", "Generating market share analysis...")
|
| 246 |
-
time.sleep(1)
|
| 247 |
|
| 248 |
-
#
|
| 249 |
-
update_progress(progress_container, 75, "
|
| 250 |
-
log_agent_activity("Report Writer", "
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
time.sleep(1)
|
| 254 |
-
log_agent_activity("Report Writer", "Drafting detailed analysis...")
|
| 255 |
-
time.sleep(1)
|
| 256 |
-
log_agent_activity("Report Writer", "Adding citations and sources...")
|
| 257 |
-
time.sleep(1)
|
| 258 |
-
|
| 259 |
-
# Create and run the crew
|
| 260 |
-
crew = create_research_crew(topic)
|
| 261 |
result = crew.kickoff()
|
| 262 |
|
| 263 |
-
#
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
-
# Format
|
| 267 |
-
report_data = format_json_output(
|
| 268 |
|
|
|
|
| 269 |
update_progress(progress_container, 100, "Report completed!")
|
| 270 |
-
|
| 271 |
-
log_agent_activity("System", "Report generation completed!")
|
| 272 |
|
| 273 |
-
# Store
|
| 274 |
st.session_state.current_report = report_data
|
| 275 |
st.session_state.current_topic = topic
|
| 276 |
|
|
|
|
|
|
|
| 277 |
except Exception as e:
|
| 278 |
error_msg = f"Error during research: {str(e)}"
|
| 279 |
log_agent_activity("System", f"Error: {error_msg}")
|
| 280 |
st.error(error_msg)
|
|
|
|
|
|
|
| 281 |
|
| 282 |
def main():
|
| 283 |
st.title("🤖 AI Market Research Generator")
|
|
@@ -301,7 +320,6 @@ def main():
|
|
| 301 |
placeholder="e.g., Electric Vehicles Market"
|
| 302 |
)
|
| 303 |
|
| 304 |
-
# Dynamic button state
|
| 305 |
if st.session_state.generating:
|
| 306 |
st.button("Generating Report...", disabled=True)
|
| 307 |
else:
|
|
@@ -311,17 +329,33 @@ def main():
|
|
| 311 |
else:
|
| 312 |
st.session_state.generating = True
|
| 313 |
|
| 314 |
-
# Create containers
|
| 315 |
progress_container = st.container()
|
| 316 |
chat_container = st.container()
|
| 317 |
log_container = st.container()
|
| 318 |
|
| 319 |
try:
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
except Exception as e:
|
| 322 |
st.error(f"Error generating report: {str(e)}")
|
| 323 |
finally:
|
| 324 |
st.session_state.generating = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
with tab2:
|
| 327 |
if 'current_report' in st.session_state:
|
|
|
|
| 212 |
process=Process.sequential
|
| 213 |
)
|
| 214 |
|
| 215 |
+
def extract_section(text, section_name):
|
| 216 |
+
"""Extract a section from the text"""
|
| 217 |
+
pattern = f"{section_name}.*?\n(.*?)(?=\n\n|$)"
|
| 218 |
+
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
|
| 219 |
+
if match:
|
| 220 |
+
return match.group(1).strip()
|
| 221 |
+
|
| 222 |
+
# Try alternative patterns
|
| 223 |
+
pattern2 = f"{section_name}[:\s](.*?)(?=\n\n|$)"
|
| 224 |
+
match = re.search(pattern2, text, re.DOTALL | re.IGNORECASE)
|
| 225 |
+
return match.group(1).strip() if match else "Information not found"
|
| 226 |
+
|
| 227 |
+
def extract_sources(text):
|
| 228 |
+
"""Extract sources from the text"""
|
| 229 |
+
sources = []
|
| 230 |
+
patterns = [
|
| 231 |
+
r"Source:.*?(?:\n|$)",
|
| 232 |
+
r"\[.*?\]",
|
| 233 |
+
r"\(https?://.*?\)",
|
| 234 |
+
r"Reference:.*?(?:\n|$)",
|
| 235 |
+
r"Retrieved from:.*?(?:\n|$)"
|
| 236 |
+
]
|
| 237 |
+
|
| 238 |
+
for pattern in patterns:
|
| 239 |
+
matches = re.finditer(pattern, text, re.MULTILINE)
|
| 240 |
+
sources.extend([match.group().strip() for match in matches])
|
| 241 |
+
|
| 242 |
+
return sources if sources else ["Sources not explicitly mentioned in the report"]
|
| 243 |
+
|
| 244 |
def run_market_research(topic: str, progress_container, chat_container, log_container):
|
| 245 |
"""Run the market research process"""
|
| 246 |
try:
|
| 247 |
+
# Clear previous logs
|
| 248 |
st.session_state.agent_logs = []
|
| 249 |
|
| 250 |
+
# Initialize research team
|
| 251 |
update_progress(progress_container, 0, "Initializing research team...")
|
| 252 |
log_agent_activity("System", f"Starting market research for: {topic}")
|
|
|
|
| 253 |
|
| 254 |
+
# Create and run the crew
|
| 255 |
+
crew = create_research_crew(topic)
|
| 256 |
+
|
| 257 |
+
# Research Phase
|
| 258 |
update_progress(progress_container, 25, "Gathering market data...")
|
| 259 |
log_agent_activity("Research Analyst", f"Beginning comprehensive research on {topic}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
+
# Analysis Phase
|
| 262 |
update_progress(progress_container, 50, "Analyzing findings...")
|
| 263 |
+
log_agent_activity("Data Analyst", "Processing research data...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
# Report Phase
|
| 266 |
+
update_progress(progress_container, 75, "Generating report...")
|
| 267 |
+
log_agent_activity("Report Writer", "Compiling final report...")
|
| 268 |
+
|
| 269 |
+
# Execute the crew
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
result = crew.kickoff()
|
| 271 |
|
| 272 |
+
# Process the result
|
| 273 |
+
if hasattr(result, 'raw_output'):
|
| 274 |
+
raw_text = str(result.raw_output)
|
| 275 |
+
else:
|
| 276 |
+
raw_text = str(result)
|
| 277 |
+
|
| 278 |
+
# Log the raw output for debugging
|
| 279 |
+
print("Raw output:", raw_text)
|
| 280 |
|
| 281 |
+
# Format the result
|
| 282 |
+
report_data = format_json_output(raw_text)
|
| 283 |
|
| 284 |
+
# Update progress and log completion
|
| 285 |
update_progress(progress_container, 100, "Report completed!")
|
| 286 |
+
log_agent_activity("System", "Report generation completed successfully")
|
|
|
|
| 287 |
|
| 288 |
+
# Store in session state
|
| 289 |
st.session_state.current_report = report_data
|
| 290 |
st.session_state.current_topic = topic
|
| 291 |
|
| 292 |
+
return report_data
|
| 293 |
+
|
| 294 |
except Exception as e:
|
| 295 |
error_msg = f"Error during research: {str(e)}"
|
| 296 |
log_agent_activity("System", f"Error: {error_msg}")
|
| 297 |
st.error(error_msg)
|
| 298 |
+
print(f"Full error details: {str(e)}") # For debugging
|
| 299 |
+
return None
|
| 300 |
|
| 301 |
def main():
|
| 302 |
st.title("🤖 AI Market Research Generator")
|
|
|
|
| 320 |
placeholder="e.g., Electric Vehicles Market"
|
| 321 |
)
|
| 322 |
|
|
|
|
| 323 |
if st.session_state.generating:
|
| 324 |
st.button("Generating Report...", disabled=True)
|
| 325 |
else:
|
|
|
|
| 329 |
else:
|
| 330 |
st.session_state.generating = True
|
| 331 |
|
| 332 |
+
# Create containers
|
| 333 |
progress_container = st.container()
|
| 334 |
chat_container = st.container()
|
| 335 |
log_container = st.container()
|
| 336 |
|
| 337 |
try:
|
| 338 |
+
with st.spinner("Generating report..."):
|
| 339 |
+
result = run_market_research(
|
| 340 |
+
topic,
|
| 341 |
+
progress_container,
|
| 342 |
+
chat_container,
|
| 343 |
+
log_container
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
if result:
|
| 347 |
+
st.success("Report generated successfully! Switch to the Reports tab to view it.")
|
| 348 |
except Exception as e:
|
| 349 |
st.error(f"Error generating report: {str(e)}")
|
| 350 |
finally:
|
| 351 |
st.session_state.generating = False
|
| 352 |
+
|
| 353 |
+
# Show live agent activity in col2
|
| 354 |
+
with col2:
|
| 355 |
+
st.subheader("Live Agent Activity")
|
| 356 |
+
st.markdown('<div class="agent-log">', unsafe_allow_html=True)
|
| 357 |
+
display_agent_logs()
|
| 358 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 359 |
|
| 360 |
with tab2:
|
| 361 |
if 'current_report' in st.session_state:
|