Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils.py
|
| 2 |
+
import json
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
import re
|
| 8 |
+
import pdfkit
|
| 9 |
+
from jinja2 import Template
|
| 10 |
+
|
| 11 |
+
class ReportManager:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
if 'reports' not in st.session_state:
|
| 14 |
+
st.session_state.reports = {}
|
| 15 |
+
|
| 16 |
+
def save_report(self, topic: str, report_data: dict):
|
| 17 |
+
"""Save a new report with timestamp"""
|
| 18 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 19 |
+
report_id = f"{topic}_{timestamp}"
|
| 20 |
+
|
| 21 |
+
st.session_state.reports[report_id] = {
|
| 22 |
+
'topic': topic,
|
| 23 |
+
'timestamp': timestamp,
|
| 24 |
+
'exec_summary': report_data.get('exec_summary', ''),
|
| 25 |
+
'detailed_report': report_data.get('detailed_report', ''),
|
| 26 |
+
'sources': report_data.get('sources', []),
|
| 27 |
+
'charts': report_data.get('charts', [])
|
| 28 |
+
}
|
| 29 |
+
return report_id
|
| 30 |
+
|
| 31 |
+
def get_report(self, report_id: str):
|
| 32 |
+
"""Retrieve a specific report"""
|
| 33 |
+
return st.session_state.reports.get(report_id)
|
| 34 |
+
|
| 35 |
+
def get_all_reports(self):
|
| 36 |
+
"""Get all saved reports"""
|
| 37 |
+
return st.session_state.reports
|
| 38 |
+
|
| 39 |
+
def generate_visualizations(self, report_text: str):
|
| 40 |
+
"""Generate relevant visualizations based on report content"""
|
| 41 |
+
charts = []
|
| 42 |
+
|
| 43 |
+
# Extract numerical data using regex patterns
|
| 44 |
+
market_size_pattern = r"\$\s*(\d+(?:\.\d+)?)\s*(?:billion|million|trillion)"
|
| 45 |
+
growth_rate_pattern = r"(\d+(?:\.\d+)?)\s*%"
|
| 46 |
+
|
| 47 |
+
# Market size data
|
| 48 |
+
market_sizes = re.findall(market_size_pattern, report_text)
|
| 49 |
+
if market_sizes:
|
| 50 |
+
fig = go.Figure(data=[
|
| 51 |
+
go.Bar(
|
| 52 |
+
x=['Current Market Size', 'Projected Size'],
|
| 53 |
+
y=[float(market_sizes[0]), float(market_sizes[-1])],
|
| 54 |
+
text=[f'${x}B' for x in market_sizes],
|
| 55 |
+
textposition='auto',
|
| 56 |
+
)
|
| 57 |
+
])
|
| 58 |
+
fig.update_layout(title='Market Size Projection')
|
| 59 |
+
charts.append(fig)
|
| 60 |
+
|
| 61 |
+
# Growth rate data
|
| 62 |
+
growth_rates = re.findall(growth_rate_pattern, report_text)
|
| 63 |
+
if growth_rates:
|
| 64 |
+
fig = go.Figure(data=[
|
| 65 |
+
go.Line(
|
| 66 |
+
x=list(range(len(growth_rates))),
|
| 67 |
+
y=[float(x) for x in growth_rates],
|
| 68 |
+
mode='lines+markers'
|
| 69 |
+
)
|
| 70 |
+
])
|
| 71 |
+
fig.update_layout(title='Growth Rate Trends')
|
| 72 |
+
charts.append(fig)
|
| 73 |
+
|
| 74 |
+
return charts
|
| 75 |
+
|
| 76 |
+
def generate_pdf(self, report_id: str):
|
| 77 |
+
"""Generate PDF version of the report"""
|
| 78 |
+
report = self.get_report(report_id)
|
| 79 |
+
if not report:
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
# HTML template for PDF
|
| 83 |
+
template_str = """
|
| 84 |
+
<!DOCTYPE html>
|
| 85 |
+
<html>
|
| 86 |
+
<head>
|
| 87 |
+
<style>
|
| 88 |
+
body { font-family: Arial, sans-serif; }
|
| 89 |
+
.header { text-align: center; margin-bottom: 30px; }
|
| 90 |
+
.section { margin: 20px 0; }
|
| 91 |
+
.charts { text-align: center; }
|
| 92 |
+
</style>
|
| 93 |
+
</head>
|
| 94 |
+
<body>
|
| 95 |
+
<div class="header">
|
| 96 |
+
<h1>{{ report.topic }} - Market Research Report</h1>
|
| 97 |
+
<p>Generated on: {{ report.timestamp }}</p>
|
| 98 |
+
</div>
|
| 99 |
+
|
| 100 |
+
<div class="section">
|
| 101 |
+
<h2>Executive Summary</h2>
|
| 102 |
+
{{ report.exec_summary }}
|
| 103 |
+
</div>
|
| 104 |
+
|
| 105 |
+
<div class="section">
|
| 106 |
+
<h2>Detailed Analysis</h2>
|
| 107 |
+
{{ report.detailed_report }}
|
| 108 |
+
</div>
|
| 109 |
+
|
| 110 |
+
<div class="section">
|
| 111 |
+
<h2>Sources</h2>
|
| 112 |
+
<ul>
|
| 113 |
+
{% for source in report.sources %}
|
| 114 |
+
<li>{{ source }}</li>
|
| 115 |
+
{% endfor %}
|
| 116 |
+
</ul>
|
| 117 |
+
</div>
|
| 118 |
+
</body>
|
| 119 |
+
</html>
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
# Render template
|
| 123 |
+
template = Template(template_str)
|
| 124 |
+
html = template.render(report=report)
|
| 125 |
+
|
| 126 |
+
# Convert to PDF
|
| 127 |
+
try:
|
| 128 |
+
pdf = pdfkit.from_string(html, False)
|
| 129 |
+
return pdf
|
| 130 |
+
except Exception as e:
|
| 131 |
+
st.error(f"Error generating PDF: {str(e)}")
|
| 132 |
+
return None
|