Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -185,27 +185,30 @@ st.markdown("""
|
|
| 185 |
""", unsafe_allow_html=True)
|
| 186 |
|
| 187 |
def format_json_output(raw_output):
|
| 188 |
-
"""Format
|
| 189 |
try:
|
| 190 |
-
#
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
json_pattern = r"\{[\s\S]*\}"
|
| 195 |
-
match = re.search(json_pattern,
|
| 196 |
if match:
|
| 197 |
try:
|
| 198 |
return json.loads(match.group())
|
| 199 |
except:
|
| 200 |
pass
|
| 201 |
-
|
| 202 |
-
# If JSON
|
| 203 |
return {
|
| 204 |
-
"exec_summary": "
|
| 205 |
-
"detailed_report":
|
| 206 |
-
"sources":
|
| 207 |
}
|
| 208 |
-
|
| 209 |
def display_thinking_animation():
|
| 210 |
"""Display thinking animation dots"""
|
| 211 |
return """
|
|
@@ -359,12 +362,33 @@ def run_market_research(topic: str, progress_container, chat_container):
|
|
| 359 |
return format_json_output(result)
|
| 360 |
|
| 361 |
except Exception as e:
|
| 362 |
-
st.error(f"Error: {str(e)}")
|
| 363 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
def main():
|
| 366 |
st.title("🤖 AI Market Research Generator")
|
| 367 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
tab1, tab2 = st.tabs(["Generate Report", "View Reports"])
|
| 369 |
|
| 370 |
with tab1:
|
|
@@ -377,48 +401,75 @@ def main():
|
|
| 377 |
placeholder="e.g., Electric Vehicles Market"
|
| 378 |
)
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
-
# Create containers for progress and chat
|
| 386 |
progress_container = st.container()
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
st.
|
|
|
|
| 398 |
|
| 399 |
with tab2:
|
| 400 |
if hasattr(st.session_state, 'current_report'):
|
| 401 |
-
st.
|
| 402 |
|
| 403 |
-
# Display Executive Summary
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
-
# Display Detailed Report
|
| 408 |
-
|
| 409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
-
# Display Sources
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
# Download buttons
|
| 417 |
col1, col2 = st.columns(2)
|
| 418 |
with col1:
|
| 419 |
st.download_button(
|
| 420 |
"Download Full Report",
|
| 421 |
-
data=
|
| 422 |
file_name=f"{topic}_report.md",
|
| 423 |
mime="text/markdown"
|
| 424 |
)
|
|
|
|
| 185 |
""", unsafe_allow_html=True)
|
| 186 |
|
| 187 |
def format_json_output(raw_output):
|
| 188 |
+
"""Format CrewOutput or raw string into proper JSON structure"""
|
| 189 |
try:
|
| 190 |
+
# Handle CrewOutput object
|
| 191 |
+
if hasattr(raw_output, 'raw_output'):
|
| 192 |
+
raw_text = str(raw_output.raw_output)
|
| 193 |
+
else:
|
| 194 |
+
raw_text = str(raw_output)
|
| 195 |
+
|
| 196 |
+
# Try to find and parse JSON structure
|
| 197 |
json_pattern = r"\{[\s\S]*\}"
|
| 198 |
+
match = re.search(json_pattern, raw_text)
|
| 199 |
if match:
|
| 200 |
try:
|
| 201 |
return json.loads(match.group())
|
| 202 |
except:
|
| 203 |
pass
|
| 204 |
+
|
| 205 |
+
# If no JSON found, create structured format
|
| 206 |
return {
|
| 207 |
+
"exec_summary": extract_section(raw_text, "Executive Summary"),
|
| 208 |
+
"detailed_report": raw_text,
|
| 209 |
+
"sources": extract_sources(raw_text)
|
| 210 |
}
|
| 211 |
+
|
| 212 |
def display_thinking_animation():
|
| 213 |
"""Display thinking animation dots"""
|
| 214 |
return """
|
|
|
|
| 362 |
return format_json_output(result)
|
| 363 |
|
| 364 |
except Exception as e:
|
| 365 |
+
st.error(f"Error formatting output: {str(e)}")
|
| 366 |
+
return {
|
| 367 |
+
"exec_summary": "Error formatting report",
|
| 368 |
+
"detailed_report": raw_text if 'raw_text' in locals() else str(raw_output),
|
| 369 |
+
"sources": []
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
def extract_section(text, section_name):
|
| 373 |
+
"""Extract a section from the text"""
|
| 374 |
+
pattern = f"{section_name}.*?\n(.*?)(?=\n\n|$)"
|
| 375 |
+
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
|
| 376 |
+
return match.group(1).strip() if match else ""
|
| 377 |
+
|
| 378 |
+
def extract_sources(text):
|
| 379 |
+
"""Extract sources from the text"""
|
| 380 |
+
sources = []
|
| 381 |
+
source_pattern = r"Source:.*?(?:\n|$)|\[.*?\]|\(https?://.*?\)"
|
| 382 |
+
matches = re.finditer(source_pattern, text, re.MULTILINE)
|
| 383 |
+
return [match.group().strip() for match in matches]
|
| 384 |
|
| 385 |
def main():
|
| 386 |
st.title("🤖 AI Market Research Generator")
|
| 387 |
|
| 388 |
+
# Initialize session state for button control
|
| 389 |
+
if 'generating' not in st.session_state:
|
| 390 |
+
st.session_state.generating = False
|
| 391 |
+
|
| 392 |
tab1, tab2 = st.tabs(["Generate Report", "View Reports"])
|
| 393 |
|
| 394 |
with tab1:
|
|
|
|
| 401 |
placeholder="e.g., Electric Vehicles Market"
|
| 402 |
)
|
| 403 |
|
| 404 |
+
# Dynamic button state
|
| 405 |
+
if st.session_state.generating:
|
| 406 |
+
st.button("Generating Report...", disabled=True)
|
| 407 |
+
else:
|
| 408 |
+
if st.button("Generate Report", type="primary"):
|
| 409 |
+
if not topic:
|
| 410 |
+
st.error("Please enter a research topic")
|
| 411 |
+
else:
|
| 412 |
+
st.session_state.generating = True
|
| 413 |
+
st.rerun()
|
| 414 |
+
|
| 415 |
+
# Only show chat window if generating
|
| 416 |
+
if st.session_state.generating:
|
| 417 |
+
with col2:
|
| 418 |
+
st.markdown('<div class="chat-window">', unsafe_allow_html=True)
|
| 419 |
+
chat_container = st.empty()
|
| 420 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 421 |
|
|
|
|
| 422 |
progress_container = st.container()
|
| 423 |
|
| 424 |
+
try:
|
| 425 |
+
result = run_market_research(topic, progress_container, chat_container)
|
| 426 |
+
|
| 427 |
+
if result:
|
| 428 |
+
st.session_state.current_report = result
|
| 429 |
+
st.success("Report generated successfully! View it in the Reports tab.")
|
| 430 |
+
except Exception as e:
|
| 431 |
+
st.error(f"Error generating report: {str(e)}")
|
| 432 |
+
finally:
|
| 433 |
+
st.session_state.generating = False
|
| 434 |
+
st.rerun()
|
| 435 |
|
| 436 |
with tab2:
|
| 437 |
if hasattr(st.session_state, 'current_report'):
|
| 438 |
+
report = st.session_state.current_report
|
| 439 |
|
| 440 |
+
# Display Executive Summary in a card
|
| 441 |
+
st.markdown("""
|
| 442 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; margin: 10px 0;'>
|
| 443 |
+
<h3>Executive Summary</h3>
|
| 444 |
+
<p>{}</p>
|
| 445 |
+
</div>
|
| 446 |
+
""".format(report['exec_summary']), unsafe_allow_html=True)
|
| 447 |
|
| 448 |
+
# Display Detailed Report in a card
|
| 449 |
+
st.markdown("""
|
| 450 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; margin: 10px 0;'>
|
| 451 |
+
<h3>Detailed Report</h3>
|
| 452 |
+
<p>{}</p>
|
| 453 |
+
</div>
|
| 454 |
+
""".format(report['detailed_report']), unsafe_allow_html=True)
|
| 455 |
|
| 456 |
+
# Display Sources in a card
|
| 457 |
+
st.markdown("""
|
| 458 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; margin: 10px 0;'>
|
| 459 |
+
<h3>Sources</h3>
|
| 460 |
+
<ul>
|
| 461 |
+
{}
|
| 462 |
+
</ul>
|
| 463 |
+
</div>
|
| 464 |
+
""".format('\n'.join([f"<li>{source}</li>" for source in report['sources']])),
|
| 465 |
+
unsafe_allow_html=True)
|
| 466 |
|
| 467 |
# Download buttons
|
| 468 |
col1, col2 = st.columns(2)
|
| 469 |
with col1:
|
| 470 |
st.download_button(
|
| 471 |
"Download Full Report",
|
| 472 |
+
data=report['detailed_report'],
|
| 473 |
file_name=f"{topic}_report.md",
|
| 474 |
mime="text/markdown"
|
| 475 |
)
|