DrishtiSharma commited on
Commit
204fea0
·
verified ·
1 Parent(s): 12d39e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -24
app.py CHANGED
@@ -5,6 +5,8 @@ from langchain_groq import ChatGroq
5
  from fpdf import FPDF
6
  import pandas as pd
7
  import plotly.express as px
 
 
8
  import time
9
 
10
  # Title and Sidebar
@@ -129,29 +131,43 @@ crew = Crew(
129
  verbose=True
130
  )
131
 
132
- def generate_pdf_report(result):
133
  """Generate a professional PDF report from the Crew output."""
134
- pdf = FPDF()
135
- pdf.add_page()
136
- pdf.set_font("Arial", size=12)
137
- pdf.set_auto_page_break(auto=True, margin=15)
138
-
139
- # Title
140
- pdf.set_font("Arial", size=16, style="B")
141
- pdf.cell(200, 10, txt="Patent Insights Report", ln=True, align="C")
142
- pdf.ln(10)
143
-
144
- # Content
145
- pdf.set_font("Arial", size=12)
146
- pdf.multi_cell(0, 10, txt=result)
147
-
148
- # Save PDF
149
- report_path = "Patent_Insights_Report.pdf"
150
- pdf.output(report_path)
151
- return report_path
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  def create_visualizations(analyst_output):
154
  """Create visualizations for advanced insights."""
 
155
  if enable_custom_visualization and analyst_output:
156
  try:
157
  st.markdown("#### Debugging Analyst Output")
@@ -162,17 +178,26 @@ def create_visualizations(analyst_output):
162
  data = pd.DataFrame(analyst_output)
163
  if 'Category' in data.columns and 'Values' in data.columns:
164
  st.markdown("### Advanced Visualization")
 
 
165
  fig = px.bar(data, x="Category", y="Values", title="Patent Trends by Category")
166
  st.plotly_chart(fig)
 
 
 
 
 
167
  else:
168
  st.warning("Data does not have the required columns 'Category' and 'Values'.")
169
  else:
170
  st.warning("Analyst output is not in the correct format for visualization.")
171
  except Exception as e:
172
  st.error(f"Failed to create visualizations: {e}")
 
173
 
174
  def display_table(analyst_output):
175
  """Display tabular data for detailed insights."""
 
176
  if analyst_output and isinstance(analyst_output, list):
177
  try:
178
  st.markdown("#### Debugging Analyst Output for Table")
@@ -181,8 +206,10 @@ def display_table(analyst_output):
181
  data = pd.DataFrame(analyst_output)
182
  st.markdown("### Data Table")
183
  st.dataframe(data)
 
184
  except Exception as e:
185
  st.error(f"Failed to display table: {e}")
 
186
 
187
  if st.button("Generate Patent Insights"):
188
  with st.spinner('Processing...'):
@@ -216,11 +243,12 @@ if st.button("Generate Patent Insights"):
216
  st.write(analyst_output)
217
 
218
  # Generate visualizations if enabled
 
219
  if enable_advanced_analysis:
220
- create_visualizations(analyst_output)
221
 
222
  # Display tabular data
223
- display_table(analyst_output)
224
 
225
  # Display Token Usage and Execution Time
226
  st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
@@ -231,9 +259,9 @@ if st.button("Generate Patent Insights"):
231
 
232
  # Generate PDF Report
233
  if writer_output:
234
- report_path = generate_pdf_report(writer_output)
235
- with open(report_path, "rb") as report_file:
236
  st.download_button("Download Report", data=report_file, file_name="Patent_Report.pdf")
237
 
238
  except Exception as e:
239
- st.error(f"An error occurred during execution: {e}")
 
5
  from fpdf import FPDF
6
  import pandas as pd
7
  import plotly.express as px
8
+ import matplotlib.pyplot as plt
9
+ import tempfile
10
  import time
11
 
12
  # Title and Sidebar
 
131
  verbose=True
132
  )
133
 
134
+ def generate_pdf_report(result, charts=None, table_data=None):
135
  """Generate a professional PDF report from the Crew output."""
136
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
137
+ pdf = FPDF()
138
+ pdf.add_page()
139
+ pdf.set_font("Arial", size=12)
140
+ pdf.set_auto_page_break(auto=True, margin=15)
141
+
142
+ # Title
143
+ pdf.set_font("Arial", size=16, style="B")
144
+ pdf.cell(200, 10, txt="Patent Insights Report", ln=True, align="C")
145
+ pdf.ln(10)
146
+
147
+ # Content
148
+ pdf.set_font("Arial", size=12)
149
+ pdf.multi_cell(0, 10, txt=result)
150
+
151
+ # Add charts if provided
152
+ if charts:
153
+ for chart_path in charts:
154
+ pdf.add_page()
155
+ pdf.image(chart_path, x=10, y=20, w=180)
156
+
157
+ # Add tables if provided
158
+ if table_data:
159
+ pdf.add_page()
160
+ pdf.set_font("Arial", size=10)
161
+ for row in table_data:
162
+ pdf.cell(200, 10, txt=str(row), ln=True)
163
+
164
+ # Save PDF
165
+ pdf.output(temp_pdf.name)
166
+ return temp_pdf.name
167
 
168
  def create_visualizations(analyst_output):
169
  """Create visualizations for advanced insights."""
170
+ chart_paths = []
171
  if enable_custom_visualization and analyst_output:
172
  try:
173
  st.markdown("#### Debugging Analyst Output")
 
178
  data = pd.DataFrame(analyst_output)
179
  if 'Category' in data.columns and 'Values' in data.columns:
180
  st.markdown("### Advanced Visualization")
181
+
182
+ # Create a bar chart
183
  fig = px.bar(data, x="Category", y="Values", title="Patent Trends by Category")
184
  st.plotly_chart(fig)
185
+
186
+ # Save chart to a file for embedding in PDF
187
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
188
+ fig.write_image(temp_chart.name)
189
+ chart_paths.append(temp_chart.name)
190
  else:
191
  st.warning("Data does not have the required columns 'Category' and 'Values'.")
192
  else:
193
  st.warning("Analyst output is not in the correct format for visualization.")
194
  except Exception as e:
195
  st.error(f"Failed to create visualizations: {e}")
196
+ return chart_paths
197
 
198
  def display_table(analyst_output):
199
  """Display tabular data for detailed insights."""
200
+ table_data = []
201
  if analyst_output and isinstance(analyst_output, list):
202
  try:
203
  st.markdown("#### Debugging Analyst Output for Table")
 
206
  data = pd.DataFrame(analyst_output)
207
  st.markdown("### Data Table")
208
  st.dataframe(data)
209
+ table_data = data.to_dict(orient="records")
210
  except Exception as e:
211
  st.error(f"Failed to display table: {e}")
212
+ return table_data
213
 
214
  if st.button("Generate Patent Insights"):
215
  with st.spinner('Processing...'):
 
243
  st.write(analyst_output)
244
 
245
  # Generate visualizations if enabled
246
+ charts = []
247
  if enable_advanced_analysis:
248
+ charts = create_visualizations(analyst_output)
249
 
250
  # Display tabular data
251
+ table_data = display_table(analyst_output)
252
 
253
  # Display Token Usage and Execution Time
254
  st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
 
259
 
260
  # Generate PDF Report
261
  if writer_output:
262
+ pdf_path = generate_pdf_report(writer_output, charts=charts, table_data=table_data)
263
+ with open(pdf_path, "rb") as report_file:
264
  st.download_button("Download Report", data=report_file, file_name="Patent_Report.pdf")
265
 
266
  except Exception as e:
267
+ st.error(f"An error occurred during execution: {e}")