Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -100,22 +100,65 @@ def generate_led_html(score, diagnosis):
|
|
| 100 |
</div>
|
| 101 |
"""
|
| 102 |
|
| 103 |
-
def create_medical_report(pt_name, pt_id, diagnosis, conf):
|
| 104 |
try:
|
| 105 |
filename = tempfile.mktemp(suffix=".pdf")
|
| 106 |
c = canvas.Canvas(filename, pagesize=A4)
|
| 107 |
-
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
c.
|
| 114 |
-
c.drawString(2*cm,
|
| 115 |
-
c.
|
| 116 |
-
c.
|
| 117 |
-
c.drawString(2*cm, 20*cm, f"Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}")
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
c.save()
|
| 120 |
return filename
|
| 121 |
except Exception as e:
|
|
@@ -369,11 +412,15 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Ovarian Tumor AI") as dem
|
|
| 369 |
[img_det, img_seg, img_crop, state_fig, txt_log, html_led, aud, state_crop, state_diag, state_conf, gallery_ui, state_gallery, state_img_orig, state_img_det, state_img_seg]
|
| 370 |
)
|
| 371 |
|
| 372 |
-
def pdf_wrapper(name, pid, diag, conf):
|
| 373 |
if not diag: return None
|
| 374 |
-
return create_medical_report(name, pid, diag, conf)
|
| 375 |
|
| 376 |
-
btn_pdf.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
# Chat interactions
|
| 379 |
btn_send.click(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], [chatbot, msg])
|
|
|
|
| 100 |
</div>
|
| 101 |
"""
|
| 102 |
|
| 103 |
+
def create_medical_report(pt_name, pt_id, diagnosis, conf, img_det, img_seg, img_crop):
|
| 104 |
try:
|
| 105 |
filename = tempfile.mktemp(suffix=".pdf")
|
| 106 |
c = canvas.Canvas(filename, pagesize=A4)
|
| 107 |
+
width, height = A4
|
| 108 |
|
| 109 |
+
# Font settings
|
| 110 |
+
font_regular = 'THSarabun' if 'THSarabun' in pdfmetrics.getRegisteredFontNames() else 'Helvetica'
|
| 111 |
+
font_bold = 'THSarabun-Bold' if 'THSarabun-Bold' in pdfmetrics.getRegisteredFontNames() else 'Helvetica-Bold'
|
| 112 |
|
| 113 |
+
# --- Header Section ---
|
| 114 |
+
c.setFont(font_bold, 24)
|
| 115 |
+
c.drawString(2*cm, height - 3*cm, "Medical Image Analysis Report")
|
| 116 |
+
c.setLineWidth(2)
|
| 117 |
+
c.line(2*cm, height - 3.2*cm, 19*cm, height - 3.2*cm)
|
|
|
|
| 118 |
|
| 119 |
+
# --- Patient Info ---
|
| 120 |
+
c.setFont(font_bold, 16)
|
| 121 |
+
c.drawString(2*cm, height - 5*cm, f"Patient Name: {pt_name}")
|
| 122 |
+
c.drawString(11*cm, height - 5*cm, f"Patient ID: {pt_id}")
|
| 123 |
+
c.drawString(2*cm, height - 6*cm, f"Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}")
|
| 124 |
+
|
| 125 |
+
# --- Diagnosis Result ---
|
| 126 |
+
c.setFillColorRGB(0.9, 0.9, 0.95)
|
| 127 |
+
c.rect(1.5*cm, height - 9*cm, 18*cm, 2*cm, fill=1, stroke=0)
|
| 128 |
+
c.setFillColorRGB(0, 0, 0)
|
| 129 |
+
c.drawString(2*cm, height - 8*cm, f"Diagnosis: {diagnosis}")
|
| 130 |
+
c.drawString(11*cm, height - 8*cm, f"Confidence: {conf}%")
|
| 131 |
+
|
| 132 |
+
# --- Image Helper ---
|
| 133 |
+
def draw_temp_image(img_array, x, y, w, h, title):
|
| 134 |
+
if img_array is not None:
|
| 135 |
+
try:
|
| 136 |
+
# Convert RGB (Gradio) to BGR (OpenCV) for saving
|
| 137 |
+
img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
| 138 |
+
tmp_img_path = tempfile.mktemp(suffix=".jpg")
|
| 139 |
+
cv2.imwrite(tmp_img_path, img_bgr)
|
| 140 |
+
|
| 141 |
+
# Draw Image
|
| 142 |
+
c.drawImage(tmp_img_path, x, y, width=w, height=h, preserveAspectRatio=True)
|
| 143 |
+
|
| 144 |
+
# Draw Title
|
| 145 |
+
c.setFont(font_bold, 14)
|
| 146 |
+
c.drawCentredString(x + w/2, y - 0.5*cm, title)
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print(f"Error drawing image: {e}")
|
| 149 |
+
|
| 150 |
+
# --- Draw Images ---
|
| 151 |
+
# Row 1: Detection & Segmentation
|
| 152 |
+
draw_temp_image(img_det, 2*cm, height - 16*cm, 8*cm, 6*cm, "AI Detection")
|
| 153 |
+
draw_temp_image(img_seg, 11*cm, height - 16*cm, 8*cm, 6*cm, "Segmentation Mask")
|
| 154 |
+
|
| 155 |
+
# Row 2: Focused Lesion
|
| 156 |
+
draw_temp_image(img_crop, 6.5*cm, height - 23*cm, 8*cm, 6*cm, "Focused Lesion")
|
| 157 |
+
|
| 158 |
+
# --- Footer ---
|
| 159 |
+
c.setFont(font_regular, 12)
|
| 160 |
+
c.drawCentredString(width/2, 2*cm, "Report generated by AI Ovarian Tumor Diagnosis System (KMUTNB & Ramathibodi)")
|
| 161 |
+
|
| 162 |
c.save()
|
| 163 |
return filename
|
| 164 |
except Exception as e:
|
|
|
|
| 412 |
[img_det, img_seg, img_crop, state_fig, txt_log, html_led, aud, state_crop, state_diag, state_conf, gallery_ui, state_gallery, state_img_orig, state_img_det, state_img_seg]
|
| 413 |
)
|
| 414 |
|
| 415 |
+
def pdf_wrapper(name, pid, diag, conf, det_img, seg_img, crop_img):
|
| 416 |
if not diag: return None
|
| 417 |
+
return create_medical_report(name, pid, diag, conf, det_img, seg_img, crop_img)
|
| 418 |
|
| 419 |
+
btn_pdf.click(
|
| 420 |
+
pdf_wrapper,
|
| 421 |
+
[inp_pt_name, inp_pt_id, state_diag, state_conf, state_img_det, state_img_seg, state_crop],
|
| 422 |
+
out_pdf
|
| 423 |
+
)
|
| 424 |
|
| 425 |
# Chat interactions
|
| 426 |
btn_send.click(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], [chatbot, msg])
|