Ali2206 commited on
Commit
e221614
·
verified ·
1 Parent(s): 7fd42f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -26
app.py CHANGED
@@ -231,10 +231,11 @@ def generate_pdf_report_with_charts(summary: str, report_path: str, detailed_bat
231
  chart_dir = os.path.join(os.path.dirname(report_path), "charts")
232
  os.makedirs(chart_dir, exist_ok=True)
233
 
234
- # Bar chart
235
- bar_chart_path = os.path.join(chart_dir, "summary_chart.png")
236
  categories = ['Diagnostics', 'Medications', 'Missed', 'Inconsistencies', 'Follow-up']
237
  values = [4, 2, 3, 1, 5]
 
 
238
  plt.figure(figsize=(6, 4))
239
  plt.bar(categories, values)
240
  plt.title('Clinical Issues Overview')
@@ -242,55 +243,81 @@ def generate_pdf_report_with_charts(summary: str, report_path: str, detailed_bat
242
  plt.savefig(bar_chart_path)
243
  plt.close()
244
 
245
- # Pie chart
246
- pie_chart_path = os.path.join(chart_dir, "summary_pie_chart.png")
247
  plt.figure(figsize=(6, 6))
248
- plt.pie(values, labels=categories, autopct='%1.1f%%', startangle=140)
249
- plt.title('Distribution of Clinical Issues')
250
  plt.tight_layout()
251
  plt.savefig(pie_chart_path)
252
  plt.close()
253
 
254
- # Line graph
255
- line_chart_path = os.path.join(chart_dir, "trend_chart.png")
256
  plt.figure(figsize=(6, 4))
257
  plt.plot(categories, values, marker='o')
258
- plt.title('Clinical Concern Trend')
259
- plt.xlabel('Category')
260
- plt.ylabel('Concern Level')
261
  plt.tight_layout()
262
- plt.savefig(line_chart_path)
263
  plt.close()
264
 
265
- # PDF Creation
266
  pdf_path = report_path.replace('.md', '.pdf')
267
  pdf = FPDF()
 
 
 
268
  pdf.add_page()
269
- pdf.set_font("Arial", size=12)
270
- pdf.multi_cell(0, 10, txt="Final Medical Report", align="C")
 
 
 
 
 
 
 
 
 
 
 
 
271
  pdf.ln(5)
 
272
  for line in summary.split("\n"):
273
- clean_line = remove_non_ascii(line)
274
- pdf.multi_cell(0, 10, txt=clean_line)
275
 
276
- pdf.ln(10)
277
- pdf.image(bar_chart_path, w=150)
 
 
 
278
  pdf.ln(5)
 
 
 
 
 
 
279
  pdf.image(pie_chart_path, w=150)
280
  pdf.ln(5)
281
- pdf.image(line_chart_path, w=150)
282
 
283
- # Detailed batch results
 
 
 
284
  if detailed_batches:
285
  pdf.add_page()
286
- pdf.set_font("Arial", size=12)
287
- pdf.multi_cell(0, 10, txt="Detailed Analysis Per Tool", align="L")
 
288
  pdf.ln(5)
 
289
  for idx, detail in enumerate(detailed_batches):
290
- pdf.multi_cell(0, 10, txt=f"Tool Result #{idx + 1}", align="L")
 
 
291
  for line in remove_non_ascii(detail).split("\n"):
292
- pdf.multi_cell(0, 10, txt=line)
293
- pdf.ln(5)
294
 
295
  pdf.output(pdf_path)
296
  return pdf_path
 
231
  chart_dir = os.path.join(os.path.dirname(report_path), "charts")
232
  os.makedirs(chart_dir, exist_ok=True)
233
 
234
+ # Generate 3 charts
 
235
  categories = ['Diagnostics', 'Medications', 'Missed', 'Inconsistencies', 'Follow-up']
236
  values = [4, 2, 3, 1, 5]
237
+
238
+ bar_chart_path = os.path.join(chart_dir, "bar_chart.png")
239
  plt.figure(figsize=(6, 4))
240
  plt.bar(categories, values)
241
  plt.title('Clinical Issues Overview')
 
243
  plt.savefig(bar_chart_path)
244
  plt.close()
245
 
246
+ pie_chart_path = os.path.join(chart_dir, "pie_chart.png")
 
247
  plt.figure(figsize=(6, 6))
248
+ plt.pie(values, labels=categories, autopct='%1.1f%%')
249
+ plt.title('Issue Distribution')
250
  plt.tight_layout()
251
  plt.savefig(pie_chart_path)
252
  plt.close()
253
 
254
+ trend_chart_path = os.path.join(chart_dir, "trend_chart.png")
 
255
  plt.figure(figsize=(6, 4))
256
  plt.plot(categories, values, marker='o')
257
+ plt.title('Trend Analysis')
 
 
258
  plt.tight_layout()
259
+ plt.savefig(trend_chart_path)
260
  plt.close()
261
 
 
262
  pdf_path = report_path.replace('.md', '.pdf')
263
  pdf = FPDF()
264
+ pdf.set_auto_page_break(auto=True, margin=15)
265
+
266
+ # Title Page
267
  pdf.add_page()
268
+ pdf.set_font("Arial", 'B', 24)
269
+ pdf.cell(0, 20, "🩺 Final Medical Report", ln=True, align='C')
270
+ pdf.set_font("Arial", '', 14)
271
+ pdf.cell(0, 10, datetime.now().strftime("Generated on %B %d, %Y at %H:%M"), ln=True, align='C')
272
+ pdf.ln(20)
273
+ pdf.set_font("Arial", 'I', 12)
274
+ pdf.multi_cell(0, 10, "This report contains a professional summary of clinical observations, potential inconsistencies, and follow-up recommendations based on the uploaded medical document.", align="C")
275
+
276
+ # Summary Page
277
+ pdf.add_page()
278
+ pdf.set_font("Arial", 'B', 16)
279
+ pdf.cell(0, 10, "📄 Final Summary", ln=True)
280
+ pdf.set_draw_color(200, 200, 200)
281
+ pdf.line(10, pdf.get_y(), 200, pdf.get_y())
282
  pdf.ln(5)
283
+ pdf.set_font("Arial", '', 12)
284
  for line in summary.split("\n"):
285
+ clean_line = remove_non_ascii(line.strip())
286
+ pdf.multi_cell(0, 8, txt=clean_line)
287
 
288
+ # Charts Page
289
+ pdf.add_page()
290
+ pdf.set_font("Arial", 'B', 16)
291
+ pdf.cell(0, 10, "📊 Statistical Overview", ln=True)
292
+ pdf.line(10, pdf.get_y(), 200, pdf.get_y())
293
  pdf.ln(5)
294
+ pdf.set_font("Arial", 'B', 12)
295
+ pdf.cell(0, 10, "1. Clinical Issues Overview", ln=True)
296
+ pdf.image(bar_chart_path, w=180)
297
+ pdf.ln(5)
298
+
299
+ pdf.cell(0, 10, "2. Issue Distribution", ln=True)
300
  pdf.image(pie_chart_path, w=150)
301
  pdf.ln(5)
 
302
 
303
+ pdf.cell(0, 10, "3. Trend Analysis", ln=True)
304
+ pdf.image(trend_chart_path, w=180)
305
+
306
+ # Detailed Batches
307
  if detailed_batches:
308
  pdf.add_page()
309
+ pdf.set_font("Arial", 'B', 16)
310
+ pdf.cell(0, 10, "🔎 Detailed Tool Insights", ln=True)
311
+ pdf.line(10, pdf.get_y(), 200, pdf.get_y())
312
  pdf.ln(5)
313
+
314
  for idx, detail in enumerate(detailed_batches):
315
+ pdf.set_font("Arial", 'B', 13)
316
+ pdf.cell(0, 10, f"Tool Output #{idx + 1}", ln=True)
317
+ pdf.set_font("Arial", '', 11)
318
  for line in remove_non_ascii(detail).split("\n"):
319
+ pdf.multi_cell(0, 8, txt=line.strip())
320
+ pdf.ln(3)
321
 
322
  pdf.output(pdf_path)
323
  return pdf_path