Update app.py
Browse files
app.py
CHANGED
|
@@ -225,16 +225,17 @@ Avoid repeating the same points multiple times.
|
|
| 225 |
return final_response
|
| 226 |
|
| 227 |
def remove_non_ascii(text):
|
| 228 |
-
return
|
| 229 |
|
| 230 |
def generate_pdf_report_with_charts(summary: str, report_path: str, detailed_batches: List[str] = None):
|
| 231 |
chart_dir = os.path.join(os.path.dirname(report_path), "charts")
|
| 232 |
os.makedirs(chart_dir, exist_ok=True)
|
| 233 |
|
| 234 |
-
#
|
| 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)
|
|
@@ -243,6 +244,7 @@ def generate_pdf_report_with_charts(summary: str, report_path: str, detailed_bat
|
|
| 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%%')
|
|
@@ -251,6 +253,7 @@ def generate_pdf_report_with_charts(summary: str, report_path: str, detailed_bat
|
|
| 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')
|
|
@@ -259,61 +262,66 @@ def generate_pdf_report_with_charts(summary: str, report_path: str, detailed_bat
|
|
| 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, "
|
| 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,
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
# Summary
|
| 277 |
pdf.add_page()
|
| 278 |
pdf.set_font("Arial", 'B', 16)
|
| 279 |
-
pdf.cell(0, 10, "
|
| 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 |
-
|
|
|
|
| 287 |
|
| 288 |
-
# Charts
|
| 289 |
pdf.add_page()
|
| 290 |
pdf.set_font("Arial", 'B', 16)
|
| 291 |
-
pdf.cell(0, 10, "
|
| 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
|
| 307 |
if detailed_batches:
|
| 308 |
pdf.add_page()
|
| 309 |
pdf.set_font("Arial", 'B', 16)
|
| 310 |
-
pdf.cell(0, 10, "
|
| 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())
|
|
|
|
| 225 |
return final_response
|
| 226 |
|
| 227 |
def remove_non_ascii(text):
|
| 228 |
+
return ''.join(c for c in text if ord(c) < 256)
|
| 229 |
|
| 230 |
def generate_pdf_report_with_charts(summary: str, report_path: str, detailed_batches: List[str] = None):
|
| 231 |
chart_dir = os.path.join(os.path.dirname(report_path), "charts")
|
| 232 |
os.makedirs(chart_dir, exist_ok=True)
|
| 233 |
|
| 234 |
+
# Prepare data
|
| 235 |
categories = ['Diagnostics', 'Medications', 'Missed', 'Inconsistencies', 'Follow-up']
|
| 236 |
values = [4, 2, 3, 1, 5]
|
| 237 |
|
| 238 |
+
# Chart 1: Bar
|
| 239 |
bar_chart_path = os.path.join(chart_dir, "bar_chart.png")
|
| 240 |
plt.figure(figsize=(6, 4))
|
| 241 |
plt.bar(categories, values)
|
|
|
|
| 244 |
plt.savefig(bar_chart_path)
|
| 245 |
plt.close()
|
| 246 |
|
| 247 |
+
# Chart 2: Pie
|
| 248 |
pie_chart_path = os.path.join(chart_dir, "pie_chart.png")
|
| 249 |
plt.figure(figsize=(6, 6))
|
| 250 |
plt.pie(values, labels=categories, autopct='%1.1f%%')
|
|
|
|
| 253 |
plt.savefig(pie_chart_path)
|
| 254 |
plt.close()
|
| 255 |
|
| 256 |
+
# Chart 3: Line
|
| 257 |
trend_chart_path = os.path.join(chart_dir, "trend_chart.png")
|
| 258 |
plt.figure(figsize=(6, 4))
|
| 259 |
plt.plot(categories, values, marker='o')
|
|
|
|
| 262 |
plt.savefig(trend_chart_path)
|
| 263 |
plt.close()
|
| 264 |
|
| 265 |
+
# PDF init
|
| 266 |
pdf_path = report_path.replace('.md', '.pdf')
|
| 267 |
pdf = FPDF()
|
| 268 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 269 |
|
| 270 |
+
# === Title Page ===
|
| 271 |
pdf.add_page()
|
| 272 |
pdf.set_font("Arial", 'B', 24)
|
| 273 |
+
pdf.cell(0, 20, remove_non_ascii("Final Medical Report"), ln=True, align='C')
|
| 274 |
pdf.set_font("Arial", '', 14)
|
| 275 |
pdf.cell(0, 10, datetime.now().strftime("Generated on %B %d, %Y at %H:%M"), ln=True, align='C')
|
| 276 |
pdf.ln(20)
|
| 277 |
pdf.set_font("Arial", 'I', 12)
|
| 278 |
+
pdf.multi_cell(0, 10, remove_non_ascii(
|
| 279 |
+
"This report contains a professional summary of clinical observations, potential inconsistencies, and follow-up recommendations based on the uploaded medical document."
|
| 280 |
+
), align="C")
|
| 281 |
|
| 282 |
+
# === Summary Section ===
|
| 283 |
pdf.add_page()
|
| 284 |
pdf.set_font("Arial", 'B', 16)
|
| 285 |
+
pdf.cell(0, 10, remove_non_ascii("Final Summary"), ln=True)
|
| 286 |
pdf.set_draw_color(200, 200, 200)
|
| 287 |
pdf.line(10, pdf.get_y(), 200, pdf.get_y())
|
| 288 |
pdf.ln(5)
|
| 289 |
pdf.set_font("Arial", '', 12)
|
| 290 |
for line in summary.split("\n"):
|
| 291 |
clean_line = remove_non_ascii(line.strip())
|
| 292 |
+
if clean_line:
|
| 293 |
+
pdf.multi_cell(0, 8, txt=clean_line)
|
| 294 |
|
| 295 |
+
# === Charts Section ===
|
| 296 |
pdf.add_page()
|
| 297 |
pdf.set_font("Arial", 'B', 16)
|
| 298 |
+
pdf.cell(0, 10, remove_non_ascii("Statistical Overview"), ln=True)
|
| 299 |
pdf.line(10, pdf.get_y(), 200, pdf.get_y())
|
| 300 |
pdf.ln(5)
|
| 301 |
+
|
| 302 |
pdf.set_font("Arial", 'B', 12)
|
| 303 |
+
pdf.cell(0, 10, remove_non_ascii("1. Clinical Issues Overview"), ln=True)
|
| 304 |
pdf.image(bar_chart_path, w=180)
|
| 305 |
pdf.ln(5)
|
| 306 |
|
| 307 |
+
pdf.cell(0, 10, remove_non_ascii("2. Issue Distribution"), ln=True)
|
| 308 |
pdf.image(pie_chart_path, w=150)
|
| 309 |
pdf.ln(5)
|
| 310 |
|
| 311 |
+
pdf.cell(0, 10, remove_non_ascii("3. Trend Analysis"), ln=True)
|
| 312 |
pdf.image(trend_chart_path, w=180)
|
| 313 |
|
| 314 |
+
# === Detailed Tool Outputs ===
|
| 315 |
if detailed_batches:
|
| 316 |
pdf.add_page()
|
| 317 |
pdf.set_font("Arial", 'B', 16)
|
| 318 |
+
pdf.cell(0, 10, remove_non_ascii("Detailed Tool Insights"), ln=True)
|
| 319 |
pdf.line(10, pdf.get_y(), 200, pdf.get_y())
|
| 320 |
pdf.ln(5)
|
| 321 |
|
| 322 |
for idx, detail in enumerate(detailed_batches):
|
| 323 |
pdf.set_font("Arial", 'B', 13)
|
| 324 |
+
pdf.cell(0, 10, remove_non_ascii(f"Tool Output #{idx + 1}"), ln=True)
|
| 325 |
pdf.set_font("Arial", '', 11)
|
| 326 |
for line in remove_non_ascii(detail).split("\n"):
|
| 327 |
pdf.multi_cell(0, 8, txt=line.strip())
|