Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.express as px
|
| 5 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def update_progress(container, percentage, message=""):
|
| 8 |
if container:
|
|
@@ -221,82 +225,174 @@ def display_presentation_slide(slide, slide_num, total_slides):
|
|
| 221 |
if slide_num < total_slides - 1:
|
| 222 |
st.button("Next ➡️", key=f"next_{slide_num}")
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
def display_report(report_data):
|
| 225 |
try:
|
| 226 |
-
# Display metrics
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
cols = st.columns(3)
|
| 230 |
-
metrics = report_data['metrics']
|
| 231 |
-
|
| 232 |
-
cols[0].metric("Market Size", metrics.get('market_size', 'N/A'))
|
| 233 |
-
cols[0].metric("CAGR", metrics.get('cagr', 'N/A'))
|
| 234 |
-
cols[1].metric("Leader Share", metrics.get('leader_share', 'N/A'))
|
| 235 |
-
cols[1].metric("Key Players", metrics.get('key_players', 'N/A'))
|
| 236 |
-
cols[2].metric("Key Region", metrics.get('key_regions', 'N/A'))
|
| 237 |
-
cols[2].metric("Dominant Segment", metrics.get('dominant_segment', 'N/A'))
|
| 238 |
|
| 239 |
-
#
|
| 240 |
-
|
| 241 |
-
st.markdown(report_data['content'])
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
except Exception as e:
|
| 253 |
st.error(f"Error displaying report: {str(e)}")
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
st.subheader("📊 Market Analysis Visualizations")
|
| 259 |
|
| 260 |
-
#
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
names='company',
|
| 267 |
-
title='Market Share Distribution')
|
| 268 |
-
st.plotly_chart(fig_pie, use_container_width=True)
|
| 269 |
|
| 270 |
-
#
|
| 271 |
-
if '
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
title='Market Growth Trend')
|
| 278 |
-
st.plotly_chart(fig_line, use_container_width=True)
|
| 279 |
|
| 280 |
-
#
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
st.dataframe(
|
| 299 |
-
|
| 300 |
column_config={
|
| 301 |
"company": "Company",
|
| 302 |
"marketShare": st.column_config.NumberColumn(
|
|
@@ -306,46 +402,12 @@ def display_market_tables(market_data):
|
|
| 306 |
"strengths": "Key Strengths",
|
| 307 |
"developments": "Recent Developments"
|
| 308 |
},
|
| 309 |
-
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
-
# Regional Analysis Table
|
| 313 |
-
if 'regionalAnalysis' in market_data:
|
| 314 |
-
st.write("### Regional Market Analysis")
|
| 315 |
-
regional_df = pd.DataFrame(market_data['regionalAnalysis'])
|
| 316 |
-
st.dataframe(
|
| 317 |
-
regional_df,
|
| 318 |
-
column_config={
|
| 319 |
-
"name": "Region",
|
| 320 |
-
"marketSize": "Market Size",
|
| 321 |
-
"growthRate": st.column_config.NumberColumn(
|
| 322 |
-
"Growth Rate (%)",
|
| 323 |
-
format="%.1f%%"
|
| 324 |
-
),
|
| 325 |
-
"trends": "Key Trends"
|
| 326 |
-
},
|
| 327 |
-
hide_index=True
|
| 328 |
-
)
|
| 329 |
-
|
| 330 |
-
# Technology Adoption Table
|
| 331 |
-
if 'techAdoption' in market_data:
|
| 332 |
-
st.write("### Technology Adoption Analysis")
|
| 333 |
-
tech_df = pd.DataFrame(market_data['techAdoption'])
|
| 334 |
-
st.dataframe(
|
| 335 |
-
tech_df,
|
| 336 |
-
column_config={
|
| 337 |
-
"name": "Technology",
|
| 338 |
-
"adoptionRate": st.column_config.NumberColumn(
|
| 339 |
-
"Adoption Rate (%)",
|
| 340 |
-
format="%.1f%%"
|
| 341 |
-
),
|
| 342 |
-
"impact": "Impact",
|
| 343 |
-
"outlook": "Future Outlook"
|
| 344 |
-
},
|
| 345 |
hide_index=True
|
| 346 |
)
|
| 347 |
|
| 348 |
|
|
|
|
| 349 |
def apply_report_styling():
|
| 350 |
return """
|
| 351 |
<style>
|
|
@@ -456,71 +518,6 @@ def apply_report_styling():
|
|
| 456 |
</style>
|
| 457 |
"""
|
| 458 |
|
| 459 |
-
def display_report(report_data):
|
| 460 |
-
try:
|
| 461 |
-
# Apply custom styling
|
| 462 |
-
st.markdown(apply_report_styling(), unsafe_allow_html=True)
|
| 463 |
-
|
| 464 |
-
# Display metrics dashboard
|
| 465 |
-
if 'metrics' in report_data and report_data['metrics']:
|
| 466 |
-
st.subheader("📊 Market Metrics Dashboard")
|
| 467 |
-
metrics = report_data['metrics']
|
| 468 |
-
cols = st.columns(3)
|
| 469 |
-
|
| 470 |
-
with cols[0]:
|
| 471 |
-
st.metric("Market Size", metrics.get('market_size', 'N/A'))
|
| 472 |
-
st.metric("CAGR", metrics.get('cagr', 'N/A'))
|
| 473 |
-
with cols[1]:
|
| 474 |
-
key_players = metrics.get('key_players', [])
|
| 475 |
-
key_players_count = len(key_players) if isinstance(key_players, list) else 'N/A'
|
| 476 |
-
st.metric("Market Leader Share", metrics.get('leader_share', 'N/A'))
|
| 477 |
-
st.metric("Number of Key Players", key_players_count)
|
| 478 |
-
with cols[2]:
|
| 479 |
-
st.metric("Key Region", metrics.get('key_regions', 'N/A'))
|
| 480 |
-
st.metric("Dominant Segment", metrics.get('dominant_segment', 'N/A'))
|
| 481 |
-
|
| 482 |
-
# Display visualizations
|
| 483 |
-
if 'market_data' in report_data:
|
| 484 |
-
display_market_visualizations(report_data['market_data'])
|
| 485 |
-
|
| 486 |
-
# Display data tables
|
| 487 |
-
if 'market_data' in report_data:
|
| 488 |
-
display_market_tables(report_data['market_data'])
|
| 489 |
-
|
| 490 |
-
# Display main report content
|
| 491 |
-
if 'content' in report_data:
|
| 492 |
-
st.markdown("## 📑 Comprehensive Report")
|
| 493 |
-
st.markdown(report_data['content'])
|
| 494 |
-
|
| 495 |
-
# Display agent outputs
|
| 496 |
-
if 'agent_outputs' in report_data:
|
| 497 |
-
st.markdown("## 🤖 Agent Analysis - Raw Feed")
|
| 498 |
-
tabs = st.tabs(["Researcher", "Analyst", "Writer"])
|
| 499 |
-
|
| 500 |
-
with tabs[0]:
|
| 501 |
-
researcher_data = report_data['agent_outputs'].get('researcher', {})
|
| 502 |
-
st.markdown(f"**Timestamp:** {researcher_data.get('timestamp', 'N/A')}")
|
| 503 |
-
st.markdown(f"**Analysis Type:** {researcher_data.get('analysis_type', 'N/A')}")
|
| 504 |
-
st.markdown("### Raw Output")
|
| 505 |
-
st.markdown(researcher_data.get('raw_output', 'No output available'))
|
| 506 |
-
|
| 507 |
-
with tabs[1]:
|
| 508 |
-
analyst_data = report_data['agent_outputs'].get('analyst', {})
|
| 509 |
-
st.markdown(f"**Timestamp:** {analyst_data.get('timestamp', 'N/A')}")
|
| 510 |
-
st.markdown(f"**Analysis Type:** {analyst_data.get('analysis_type', 'N/A')}")
|
| 511 |
-
st.markdown("### Raw Output")
|
| 512 |
-
st.markdown(analyst_data.get('raw_output', 'No output available'))
|
| 513 |
-
|
| 514 |
-
with tabs[2]:
|
| 515 |
-
writer_data = report_data['agent_outputs'].get('writer', {})
|
| 516 |
-
st.markdown(f"**Timestamp:** {writer_data.get('timestamp', 'N/A')}")
|
| 517 |
-
st.markdown(f"**Analysis Type:** {writer_data.get('analysis_type', 'N/A')}")
|
| 518 |
-
st.markdown("### Raw Output")
|
| 519 |
-
st.markdown(writer_data.get('raw_output', 'No output available'))
|
| 520 |
-
|
| 521 |
-
except Exception as e:
|
| 522 |
-
st.error(f"Error displaying report: {str(e)}")
|
| 523 |
-
|
| 524 |
|
| 525 |
def extract_sources(text):
|
| 526 |
pattern = r'(?:Source|Reference):\s*(.*?)(?:\n|$)'
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import json
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.express as px
|
| 5 |
from datetime import datetime
|
| 6 |
+
from reportlab.lib import colors
|
| 7 |
+
from reportlab.lib.pagesizes import letter, landscape
|
| 8 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, PageBreak
|
| 9 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 10 |
|
| 11 |
def update_progress(container, percentage, message=""):
|
| 12 |
if container:
|
|
|
|
| 225 |
if slide_num < total_slides - 1:
|
| 226 |
st.button("Next ➡️", key=f"next_{slide_num}")
|
| 227 |
|
| 228 |
+
# utils.py
|
| 229 |
+
import re
|
| 230 |
+
import json
|
| 231 |
+
import pandas as pd
|
| 232 |
+
import plotly.express as px
|
| 233 |
+
from datetime import datetime
|
| 234 |
+
from reportlab.lib import colors
|
| 235 |
+
from reportlab.lib.pagesizes import letter, landscape
|
| 236 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, PageBreak
|
| 237 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 238 |
+
|
| 239 |
def display_report(report_data):
|
| 240 |
try:
|
| 241 |
+
# Display key metrics in tiles
|
| 242 |
+
st.write("### 📊 Key Market Insights")
|
| 243 |
+
metrics = report_data.get('metrics', {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
+
# Create metric tiles in a grid
|
| 246 |
+
col1, col2, col3 = st.columns(3)
|
|
|
|
| 247 |
|
| 248 |
+
with col1:
|
| 249 |
+
st.markdown("""
|
| 250 |
+
<div style='background-color: #f0f7ff; padding: 20px; border-radius: 10px; height: 150px;'>
|
| 251 |
+
<h4 style='color: #1e88e5;'>Market Size</h4>
|
| 252 |
+
<h2>{}</h2>
|
| 253 |
+
<p>CAGR: {}</p>
|
| 254 |
+
</div>
|
| 255 |
+
""".format(
|
| 256 |
+
metrics.get('market_size', 'N/A'),
|
| 257 |
+
metrics.get('cagr', 'N/A')
|
| 258 |
+
), unsafe_allow_html=True)
|
| 259 |
+
|
| 260 |
+
with col2:
|
| 261 |
+
st.markdown("""
|
| 262 |
+
<div style='background-color: #fff8e1; padding: 20px; border-radius: 10px; height: 150px;'>
|
| 263 |
+
<h4 style='color: #ffa000;'>Market Leadership</h4>
|
| 264 |
+
<h2>{}</h2>
|
| 265 |
+
<p>Key Players: {}</p>
|
| 266 |
+
</div>
|
| 267 |
+
""".format(
|
| 268 |
+
metrics.get('leader_share', 'N/A'),
|
| 269 |
+
metrics.get('key_players', 'N/A')
|
| 270 |
+
), unsafe_allow_html=True)
|
| 271 |
+
|
| 272 |
+
with col3:
|
| 273 |
+
st.markdown("""
|
| 274 |
+
<div style='background-color: #e8f5e9; padding: 20px; border-radius: 10px; height: 150px;'>
|
| 275 |
+
<h4 style='color: #43a047;'>Regional Focus</h4>
|
| 276 |
+
<h2>{}</h2>
|
| 277 |
+
<p>Dominant Segment: {}</p>
|
| 278 |
+
</div>
|
| 279 |
+
""".format(
|
| 280 |
+
metrics.get('key_regions', 'N/A'),
|
| 281 |
+
metrics.get('dominant_segment', 'N/A')
|
| 282 |
+
), unsafe_allow_html=True)
|
| 283 |
+
|
| 284 |
+
# Create tabs for different sections of the report
|
| 285 |
+
report_tabs = st.tabs([
|
| 286 |
+
"Executive Summary",
|
| 287 |
+
"Market Analysis",
|
| 288 |
+
"Competitive Landscape",
|
| 289 |
+
"Regional Analysis",
|
| 290 |
+
"Future Outlook"
|
| 291 |
+
])
|
| 292 |
+
|
| 293 |
+
# Split content into sections
|
| 294 |
+
content = report_data.get('content', '')
|
| 295 |
+
sections = content.split('#')
|
| 296 |
+
|
| 297 |
+
with report_tabs[0]:
|
| 298 |
+
st.markdown("""
|
| 299 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; border-left: 5px solid #1e88e5;'>
|
| 300 |
+
""", unsafe_allow_html=True)
|
| 301 |
+
st.markdown(sections[1] if len(sections) > 1 else "Executive Summary not available")
|
| 302 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 303 |
+
|
| 304 |
+
with report_tabs[1]:
|
| 305 |
+
st.markdown("""
|
| 306 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; border-left: 5px solid #43a047;'>
|
| 307 |
+
""", unsafe_allow_html=True)
|
| 308 |
+
st.markdown(sections[2] if len(sections) > 2 else "Market Analysis not available")
|
| 309 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 310 |
+
|
| 311 |
+
# Add market visualizations if available
|
| 312 |
+
if 'market_data' in report_data:
|
| 313 |
+
display_market_visualizations(report_data['market_data'])
|
| 314 |
+
|
| 315 |
+
with report_tabs[2]:
|
| 316 |
+
st.markdown("""
|
| 317 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; border-left: 5px solid #ffa000;'>
|
| 318 |
+
""", unsafe_allow_html=True)
|
| 319 |
+
st.markdown(sections[3] if len(sections) > 3 else "Competitive Landscape not available")
|
| 320 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 321 |
+
|
| 322 |
+
# Add competitor table if available
|
| 323 |
+
if 'market_data' in report_data and 'keyPlayers' in report_data['market_data']:
|
| 324 |
+
display_competitor_table(report_data['market_data']['keyPlayers'])
|
| 325 |
+
|
| 326 |
+
with report_tabs[3]:
|
| 327 |
+
st.markdown("""
|
| 328 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; border-left: 5px solid #e91e63;'>
|
| 329 |
+
""", unsafe_allow_html=True)
|
| 330 |
+
st.markdown(sections[4] if len(sections) > 4 else "Regional Analysis not available")
|
| 331 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 332 |
+
|
| 333 |
+
with report_tabs[4]:
|
| 334 |
+
st.markdown("""
|
| 335 |
+
<div style='background-color: white; padding: 20px; border-radius: 10px; border-left: 5px solid #9c27b0;'>
|
| 336 |
+
""", unsafe_allow_html=True)
|
| 337 |
+
st.markdown(sections[5] if len(sections) > 5 else "Future Outlook not available")
|
| 338 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 339 |
+
|
| 340 |
except Exception as e:
|
| 341 |
st.error(f"Error displaying report: {str(e)}")
|
| 342 |
|
| 343 |
+
def create_presentation_slides(report_data):
|
| 344 |
+
"""Convert report data into presentation-style slides"""
|
| 345 |
+
slides = []
|
|
|
|
| 346 |
|
| 347 |
+
# Slide 1: Title
|
| 348 |
+
slides.append({
|
| 349 |
+
'title': "Executive Summary",
|
| 350 |
+
'type': 'title',
|
| 351 |
+
'content': report_data.get('title', 'Market Research Report')
|
| 352 |
+
})
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
+
# Slide 2: Key Metrics
|
| 355 |
+
if 'metrics' in report_data:
|
| 356 |
+
slides.append({
|
| 357 |
+
'title': "Key Market Metrics",
|
| 358 |
+
'type': 'metrics',
|
| 359 |
+
'content': report_data['metrics']
|
| 360 |
+
})
|
|
|
|
|
|
|
| 361 |
|
| 362 |
+
# Extract sections from content
|
| 363 |
+
content = report_data.get('content', '')
|
| 364 |
+
sections = content.split('#')
|
| 365 |
+
|
| 366 |
+
# Create slides for each major section
|
| 367 |
+
for section in sections[1:]: # Skip the first empty section
|
| 368 |
+
if section.strip():
|
| 369 |
+
title = section.split('\n')[0].strip()
|
| 370 |
+
content = '\n'.join(section.split('\n')[1:]).strip()
|
| 371 |
+
slides.append({
|
| 372 |
+
'title': title,
|
| 373 |
+
'type': 'text',
|
| 374 |
+
'content': content
|
| 375 |
+
})
|
| 376 |
|
| 377 |
+
return slides
|
| 378 |
+
|
| 379 |
+
def display_market_visualizations(market_data):
|
| 380 |
+
"""Display market visualizations using Plotly"""
|
| 381 |
+
if 'marketShares' in market_data:
|
| 382 |
+
fig = px.pie(
|
| 383 |
+
market_data['marketShares'],
|
| 384 |
+
values='share',
|
| 385 |
+
names='company',
|
| 386 |
+
title='Market Share Distribution'
|
| 387 |
+
)
|
| 388 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 389 |
+
|
| 390 |
+
def display_competitor_table(competitors_data):
|
| 391 |
+
"""Display competitor information in a styled table"""
|
| 392 |
+
if competitors_data:
|
| 393 |
+
df = pd.DataFrame(competitors_data)
|
| 394 |
st.dataframe(
|
| 395 |
+
df,
|
| 396 |
column_config={
|
| 397 |
"company": "Company",
|
| 398 |
"marketShare": st.column_config.NumberColumn(
|
|
|
|
| 402 |
"strengths": "Key Strengths",
|
| 403 |
"developments": "Recent Developments"
|
| 404 |
},
|
| 405 |
+
use_container_width=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
hide_index=True
|
| 407 |
)
|
| 408 |
|
| 409 |
|
| 410 |
+
|
| 411 |
def apply_report_styling():
|
| 412 |
return """
|
| 413 |
<style>
|
|
|
|
| 518 |
</style>
|
| 519 |
"""
|
| 520 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
|
| 522 |
def extract_sources(text):
|
| 523 |
pattern = r'(?:Source|Reference):\s*(.*?)(?:\n|$)'
|