Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
utils.py
CHANGED
|
@@ -1,132 +1,91 @@
|
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 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 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
if
|
| 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 |
-
{% 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
|
|
|
|
| 1 |
# utils.py
|
| 2 |
+
import re
|
| 3 |
import json
|
|
|
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def update_progress(container, percentage, message=""):
|
| 7 |
+
"""Update the progress bar"""
|
| 8 |
+
progress_html = f"""
|
| 9 |
+
<div class="progress-container">
|
| 10 |
+
<div class="progress-bar" style="width: {percentage}%">{message}</div>
|
| 11 |
+
</div>
|
| 12 |
+
"""
|
| 13 |
+
container.markdown(progress_html, unsafe_allow_html=True)
|
| 14 |
|
| 15 |
+
def extract_section(text, section_name):
|
| 16 |
+
"""Extract a section from the text"""
|
| 17 |
+
pattern = f"{section_name}.*?\n(.*?)(?=\n\n|$)"
|
| 18 |
+
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
|
| 19 |
+
if match:
|
| 20 |
+
return match.group(1).strip()
|
| 21 |
+
|
| 22 |
+
pattern2 = f"{section_name}[:\s](.*?)(?=\n\n|$)"
|
| 23 |
+
match = re.search(pattern2, text, re.DOTALL | re.IGNORECASE)
|
| 24 |
+
return match.group(1).strip() if match else "Information not found"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def extract_sources(text):
|
| 27 |
+
"""Extract sources from the text"""
|
| 28 |
+
sources = []
|
| 29 |
+
patterns = [
|
| 30 |
+
r"Source:.*?(?:\n|$)",
|
| 31 |
+
r"\[.*?\]",
|
| 32 |
+
r"\(https?://.*?\)",
|
| 33 |
+
r"Reference:.*?(?:\n|$)"
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
for pattern in patterns:
|
| 37 |
+
matches = re.finditer(pattern, text, re.MULTILINE)
|
| 38 |
+
sources.extend([match.group().strip() for match in matches])
|
| 39 |
+
|
| 40 |
+
return sources if sources else ["Sources not explicitly mentioned"]
|
| 41 |
|
| 42 |
+
def format_json_output(raw_output):
|
| 43 |
+
"""Format CrewOutput into proper JSON structure"""
|
| 44 |
+
try:
|
| 45 |
+
if hasattr(raw_output, 'raw_output'):
|
| 46 |
+
raw_text = str(raw_output.raw_output)
|
| 47 |
+
else:
|
| 48 |
+
raw_text = str(raw_output)
|
| 49 |
|
| 50 |
+
# Try to find JSON structure
|
| 51 |
+
json_pattern = r"\{[\s\S]*\}"
|
| 52 |
+
match = re.search(json_pattern, raw_text)
|
| 53 |
+
if match:
|
| 54 |
+
try:
|
| 55 |
+
return json.loads(match.group())
|
| 56 |
+
except:
|
| 57 |
+
pass
|
| 58 |
|
| 59 |
+
# Create structured format
|
| 60 |
+
return {
|
| 61 |
+
"exec_summary": {
|
| 62 |
+
"summary": extract_section(raw_text, "Executive Summary"),
|
| 63 |
+
"market_size": extract_section(raw_text, "Market Size"),
|
| 64 |
+
"growth_rate": extract_section(raw_text, "Growth Rate"),
|
| 65 |
+
"key_players": extract_section(raw_text, "Key Players")
|
| 66 |
+
},
|
| 67 |
+
"detailed_report": raw_text,
|
| 68 |
+
"sources": extract_sources(raw_text),
|
| 69 |
+
"metrics": {
|
| 70 |
+
"market_size_data": [],
|
| 71 |
+
"growth_rates": [],
|
| 72 |
+
"market_shares": {}
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
except Exception as e:
|
| 76 |
+
st.error(f"Error formatting output: {str(e)}")
|
| 77 |
+
return {
|
| 78 |
+
"exec_summary": {
|
| 79 |
+
"summary": "Error formatting report",
|
| 80 |
+
"market_size": "N/A",
|
| 81 |
+
"growth_rate": "N/A",
|
| 82 |
+
"key_players": "N/A"
|
| 83 |
+
},
|
| 84 |
+
"detailed_report": raw_text if 'raw_text' in locals() else str(raw_output),
|
| 85 |
+
"sources": [],
|
| 86 |
+
"metrics": {
|
| 87 |
+
"market_size_data": [],
|
| 88 |
+
"growth_rates": [],
|
| 89 |
+
"market_shares": {}
|
| 90 |
+
}
|
| 91 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|