Spaces:
Running
Running
Update thinktank/app.py
Browse files- thinktank/app.py +24 -4
thinktank/app.py
CHANGED
@@ -7,6 +7,10 @@ import pandas as pd
|
|
7 |
import plotly.express as px
|
8 |
import tempfile
|
9 |
import time
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Title and Application Introduction
|
12 |
st.title("Patent Strategy and Innovation Consultant")
|
@@ -112,7 +116,7 @@ analyse = Task(
|
|
112 |
description=(
|
113 |
"1. Perform statistical analysis of patent filing trends, innovation hot spots, and growth projections.\n"
|
114 |
"2. Identify top regions, key players, and technology combinations.\n"
|
115 |
-
"3. Generate visualizations such as heatmaps and multi-line charts for trends.\n"
|
116 |
"4. Provide structured output with fields 'Category' and 'Values' for visualization.\n"
|
117 |
"5. Collaborate with the Planner and Writer to align on data needs."
|
118 |
),
|
@@ -148,12 +152,17 @@ def generate_pdf_report(result, charts=None, table_data=None, metadata=None):
|
|
148 |
|
149 |
if charts:
|
150 |
for chart_path in charts:
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
153 |
|
154 |
if table_data:
|
155 |
pdf.add_page()
|
156 |
pdf.set_font("Arial", size=10)
|
|
|
157 |
for row in table_data:
|
158 |
pdf.cell(200, 10, txt=str(row), ln=True)
|
159 |
|
@@ -181,6 +190,9 @@ def create_visualizations(analyst_output):
|
|
181 |
if validated_data:
|
182 |
data = pd.DataFrame(validated_data)
|
183 |
try:
|
|
|
|
|
|
|
184 |
bar_chart = px.bar(data, x="Category", y="Values", title="Patent Trends by Category")
|
185 |
st.plotly_chart(bar_chart)
|
186 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
|
@@ -199,7 +211,14 @@ def create_visualizations(analyst_output):
|
|
199 |
heatmap_chart.write_image(temp_chart.name)
|
200 |
chart_paths.append(temp_chart.name)
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
except Exception as e:
|
|
|
203 |
st.error(f"Error generating visualization: {e}")
|
204 |
return chart_paths
|
205 |
|
@@ -250,4 +269,5 @@ if st.button("Generate Patent Insights"):
|
|
250 |
st.download_button("Download Report", data=report_file, file_name="Patent_Strategy_Report.pdf")
|
251 |
|
252 |
except Exception as e:
|
253 |
-
|
|
|
|
7 |
import plotly.express as px
|
8 |
import tempfile
|
9 |
import time
|
10 |
+
import logging
|
11 |
+
|
12 |
+
# Setup logging
|
13 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
14 |
|
15 |
# Title and Application Introduction
|
16 |
st.title("Patent Strategy and Innovation Consultant")
|
|
|
116 |
description=(
|
117 |
"1. Perform statistical analysis of patent filing trends, innovation hot spots, and growth projections.\n"
|
118 |
"2. Identify top regions, key players, and technology combinations.\n"
|
119 |
+
"3. Generate visualizations such as heatmaps, bar charts, and multi-line charts for trends.\n"
|
120 |
"4. Provide structured output with fields 'Category' and 'Values' for visualization.\n"
|
121 |
"5. Collaborate with the Planner and Writer to align on data needs."
|
122 |
),
|
|
|
152 |
|
153 |
if charts:
|
154 |
for chart_path in charts:
|
155 |
+
try:
|
156 |
+
pdf.add_page()
|
157 |
+
pdf.image(chart_path, x=10, y=20, w=180)
|
158 |
+
logging.info(f"Successfully included chart: {chart_path}")
|
159 |
+
except Exception as e:
|
160 |
+
logging.error(f"Failed to include chart in PDF: {chart_path}. Error: {e}")
|
161 |
|
162 |
if table_data:
|
163 |
pdf.add_page()
|
164 |
pdf.set_font("Arial", size=10)
|
165 |
+
pdf.cell(200, 10, txt="Consolidated Table:", ln=True, align="L")
|
166 |
for row in table_data:
|
167 |
pdf.cell(200, 10, txt=str(row), ln=True)
|
168 |
|
|
|
190 |
if validated_data:
|
191 |
data = pd.DataFrame(validated_data)
|
192 |
try:
|
193 |
+
if data.empty:
|
194 |
+
raise ValueError("Data for visualizations is empty.")
|
195 |
+
|
196 |
bar_chart = px.bar(data, x="Category", y="Values", title="Patent Trends by Category")
|
197 |
st.plotly_chart(bar_chart)
|
198 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
|
|
|
211 |
heatmap_chart.write_image(temp_chart.name)
|
212 |
chart_paths.append(temp_chart.name)
|
213 |
|
214 |
+
multi_line_chart = px.line(data, x="Category", y="Values", title="Trends Over Time")
|
215 |
+
st.plotly_chart(multi_line_chart)
|
216 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
|
217 |
+
multi_line_chart.write_image(temp_chart.name)
|
218 |
+
chart_paths.append(temp_chart.name)
|
219 |
+
|
220 |
except Exception as e:
|
221 |
+
logging.error(f"Error generating visualization: {e}")
|
222 |
st.error(f"Error generating visualization: {e}")
|
223 |
return chart_paths
|
224 |
|
|
|
269 |
st.download_button("Download Report", data=report_file, file_name="Patent_Strategy_Report.pdf")
|
270 |
|
271 |
except Exception as e:
|
272 |
+
logging.error(f"An error occurred during execution: {e}")
|
273 |
+
st.error(f"An error occurred during execution: {e}")
|