Files changed (1) hide show
  1. app.py +289 -5
app.py CHANGED
@@ -1,3 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # 1) Configuration & Setup
2
  # ============================================
3
 
@@ -6,6 +29,22 @@ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
6
  if GOOGLE_API_KEY:
7
  genai.configure(api_key=GOOGLE_API_KEY)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  if not os.path.exists(filename):
10
  print(f"📥 Downloading {filename}...")
11
  try:
@@ -29,27 +68,69 @@ font_urls = [
29
  ]
30
 
31
  for url, fname in font_urls:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  try:
33
- filename = tempfile.mktemp(suffix=".pdf")
34
- c = canvas.Canvas(filename, pagesize=A4)
 
 
 
35
 
 
 
 
 
 
 
 
 
 
 
36
 
 
 
 
 
37
  font_name = 'THSarabun-Bold' if 'THSarabun-Bold' in pdfmetrics.getRegisteredFontNames() else 'Helvetica-Bold'
38
 
39
  c.setFont(font_name, 24)
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  return None
41
 
42
  # ============================================
43
  # [FIXED] Chat Function (Dictionary Format)
44
  # ============================================
45
  def chat_fn(message, history, crop_img, info_text, diagnosis):
46
-
47
  if history is None: history = []
48
 
49
  # 1. API Key Check
50
  if not GOOGLE_API_KEY:
51
  response = "❌ ไม่พบ API KEY: กรุณาตรวจสอบการตั้งค่า GOOGLE_API_KEY ในไฟล์ app.py"
52
-
53
  history.append({"role": "user", "content": message})
54
  history.append({"role": "assistant", "content": response})
55
  return history, ""
@@ -59,6 +140,15 @@ def chat_fn(message, history, crop_img, info_text, diagnosis):
59
  context_prompt = f"""
60
  บทบาท: คุณคือผู้ช่วยทางการแพทย์อัจฉริยะ (AI Medical Assistant)
61
 
 
 
 
 
 
 
 
 
 
62
  - *สำคัญ*: ต้องลงท้ายเสมอว่า "ควรปรึกษาแพทย์ผู้เชี่ยวชาญเพื่อการวินิจฉัยที่แม่นยำที่สุด"
63
  """
64
 
@@ -67,6 +157,7 @@ def chat_fn(message, history, crop_img, info_text, diagnosis):
67
  response = model.generate_content(context_prompt)
68
  bot_reply = response.text
69
 
 
70
  bot_reply = f"เกิดข้อผิดพลาด (System Error): {str(e)}"
71
  print(f"DEBUG ERROR: {e}")
72
 
@@ -74,10 +165,183 @@ def chat_fn(message, history, crop_img, info_text, diagnosis):
74
  history.append({"role": "user", "content": message})
75
  history.append({"role": "assistant", "content": bot_reply})
76
 
77
-
78
  return history, ""
79
 
80
  # ============================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  with gr.Tab("3. Gallery History"):
82
  gallery_ui = gr.Gallery(columns=4, height=600)
83
 
@@ -92,8 +356,28 @@ def chat_fn(message, history, crop_img, info_text, diagnosis):
92
  msg = gr.Textbox(placeholder="พิมพ์คำถามที่นี่...", show_label=False)
93
  btn_send = gr.Button("ส่งข้อความ", variant="primary")
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  btn_pdf.click(pdf_wrapper, [inp_pt_name, inp_pt_id, state_diag, state_conf], out_pdf)
96
 
97
  # Chat interactions
98
  btn_send.click(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], [chatbot, msg])
99
  msg.submit(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], [chatbot, msg])
 
 
 
 
1
+ import os
2
+ import sys
3
+ import cv2
4
+ import numpy as np
5
+ import gradio as gr
6
+ import plotly.graph_objects as go
7
+ from ultralytics import YOLO
8
+ import google.generativeai as genai
9
+ from PIL import Image
10
+ from gtts import gTTS
11
+ import tempfile
12
+ import datetime
13
+ import requests
14
+ import shutil
15
+
16
+ # --- ReportLab Imports (PDF) ---
17
+ from reportlab.pdfgen import canvas
18
+ from reportlab.lib.pagesizes import A4
19
+ from reportlab.lib.units import cm, mm
20
+ from reportlab.pdfbase import pdfmetrics
21
+ from reportlab.pdfbase.ttfonts import TTFont
22
+
23
+ # ============================================
24
  # 1) Configuration & Setup
25
  # ============================================
26
 
 
29
  if GOOGLE_API_KEY:
30
  genai.configure(api_key=GOOGLE_API_KEY)
31
 
32
+ # 📂 Model Path
33
+ MODEL_PATH = "otu_multiclass_yolo11s_v2.pt"
34
+
35
+ LOGO_KMUTNB_URL = "https://www.mou.kmutnb.ac.th/logo_kmutnb.png"
36
+ LOGO_RAMA_URL ="https://www.rama.mahidol.ac.th/nursing/sites/default/files/public/Rama_Logo.png"
37
+ INTRO_SOUND_URL = "https://cdn.pixabay.com/download/audio/2022/03/24/audio_c8c8a73467.mp3?filename=cinematic-atmosphere-score-2-22136.mp3"
38
+
39
+ CLASS_NAMES = {
40
+ 0: "Chocolate cyst", 1: "Serous cystadenoma", 2: "Teratoma", 3: "Theca cell tumor",
41
+ 4: "Simple cyst", 5: "Normal ovary", 6: "Mucinous cystadenoma", 7: "High grade serous"
42
+ }
43
+
44
+ # ---------------------------------------------------------
45
+ # 🛠️ AUTO-DOWNLOAD FONTS
46
+ # ---------------------------------------------------------
47
+ def force_download_font(url, filename):
48
  if not os.path.exists(filename):
49
  print(f"📥 Downloading {filename}...")
50
  try:
 
68
  ]
69
 
70
  for url, fname in font_urls:
71
+ force_download_font(url, fname)
72
+
73
+ try:
74
+ if os.path.exists("THSarabunNew.ttf"):
75
+ pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))
76
+ if os.path.exists("THSarabunNew-Bold.ttf"):
77
+ pdfmetrics.registerFont(TTFont('THSarabun-Bold', 'THSarabunNew-Bold.ttf'))
78
+ except Exception as e:
79
+ print(f"⚠️ Font Registration Error: {e}")
80
+
81
+ # ============================================
82
+ # 2) Helper Functions
83
+ # ============================================
84
+ def text_to_speech(text):
85
  try:
86
+ tts = gTTS(text, lang='th')
87
+ f = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
88
+ tts.save(f.name)
89
+ return f.name
90
+ except: return None
91
 
92
+ def generate_led_html(score, diagnosis):
93
+ color = "#ff4444" if "High grade" in diagnosis else "#00C851" if "Normal" in diagnosis else "#ffbb33"
94
+ return f"""
95
+ <div style='background-color: #2b2b2b; border: 3px solid {color}; border-radius: 12px; padding: 15px; text-align: center; box-shadow: 0 4px 8px rgba(0,0,0,0.2);'>
96
+ <span style='color: #e0e0e0; font-size: 14px; text-transform: uppercase;'>Primary Diagnosis</span><br>
97
+ <span style='color: {color}; font-size: 24px; font-weight: 800;'>{diagnosis}</span><br>
98
+ <hr style='border-color: #444; margin: 10px 0;'>
99
+ <span style='color: white; font-size: 32px; font-weight: bold;'>{score}%</span>
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
  font_name = 'THSarabun-Bold' if 'THSarabun-Bold' in pdfmetrics.getRegisteredFontNames() else 'Helvetica-Bold'
108
 
109
  c.setFont(font_name, 24)
110
+ c.drawString(2*cm, 27*cm, "Medical Image Analysis Report")
111
+
112
+ c.setFont(font_name, 16)
113
+ c.drawString(2*cm, 25*cm, f"Patient Name: {pt_name}")
114
+ c.drawString(2*cm, 24*cm, f"Patient ID: {pt_id}")
115
+ c.drawString(2*cm, 22*cm, f"Diagnosis Result: {diagnosis}")
116
+ c.drawString(2*cm, 21*cm, f"Confidence Score: {conf}%")
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:
122
+ print(f"PDF Error: {e}")
123
  return None
124
 
125
  # ============================================
126
  # [FIXED] Chat Function (Dictionary Format)
127
  # ============================================
128
  def chat_fn(message, history, crop_img, info_text, diagnosis):
 
129
  if history is None: history = []
130
 
131
  # 1. API Key Check
132
  if not GOOGLE_API_KEY:
133
  response = "❌ ไม่พบ API KEY: กรุณาตรวจสอบการตั้งค่า GOOGLE_API_KEY ในไฟล์ app.py"
 
134
  history.append({"role": "user", "content": message})
135
  history.append({"role": "assistant", "content": response})
136
  return history, ""
 
140
  context_prompt = f"""
141
  บทบาท: คุณคือผู้ช่วยทางการแพทย์อัจฉริยะ (AI Medical Assistant)
142
 
143
+ ข้���มูลบริบททางการแพทย์ของผู้ป่วยรายนี้:
144
+ - ผลการวินิจฉัยหลัก (Diagnosis): {diagnosis if diagnosis else "ยังไม่มีการวินิจฉัย"}
145
+ - ข้อมูลเพิ่มเติม: {info_text if info_text else "ไม่มี"}
146
+
147
+ คำถามจากผู้ใช้: {message}
148
+
149
+ คำแนะนำในการตอบ:
150
+ - ตอบเป็นภาษาไทย ให้กระชับ เข้าใจง่าย
151
+ - ถ้าเกี่ยวกับเรื่องซีสต์หรือเนื้องอก ให้ข้อมูลตามหลักการแพทย์
152
  - *สำคัญ*: ต้องลงท้ายเสมอว่า "ควรปรึกษาแพทย์ผู้เชี่ยวชาญเพื่อการวินิจฉัยที่แม่นยำที่สุด"
153
  """
154
 
 
157
  response = model.generate_content(context_prompt)
158
  bot_reply = response.text
159
 
160
+ except Exception as e:
161
  bot_reply = f"เกิดข้อผิดพลาด (System Error): {str(e)}"
162
  print(f"DEBUG ERROR: {e}")
163
 
 
165
  history.append({"role": "user", "content": message})
166
  history.append({"role": "assistant", "content": bot_reply})
167
 
 
168
  return history, ""
169
 
170
  # ============================================
171
+ # 3) Main Inference Logic
172
+ # ============================================
173
+ def analyze_image(image, history_list):
174
+ if history_list is None: history_list = []
175
+ if image is None:
176
+ return [None]*15
177
+
178
+ if not os.path.exists(MODEL_PATH):
179
+ error_msg = f"⚠️ Model file not found at {MODEL_PATH}. Please upload the .pt file."
180
+ return image, image, image, go.Figure(), error_msg, "", None, image, "Error", 0, history_list, history_list, image, image, image
181
+
182
+ history_list.append(image)
183
+
184
+ lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
185
+ l, a, b = cv2.split(lab)
186
+ clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
187
+ cl = clahe.apply(l)
188
+ enhanced_img = cv2.merge((cl,a,b))
189
+ enhanced_img = cv2.cvtColor(enhanced_img, cv2.COLOR_LAB2RGB)
190
+
191
+ try:
192
+ model = YOLO(MODEL_PATH)
193
+ results = model.predict(
194
+ enhanced_img,
195
+ imgsz=640,
196
+ conf=0.25,
197
+ iou=0.45,
198
+ augment=True,
199
+ verbose=False
200
+ )[0]
201
+ except Exception as e:
202
+ return image, image, image, go.Figure(), f"Inference Error: {e}", "", None, image, "Error", 0, history_list, history_list, image, image, image
203
+
204
+ orig = image.copy()
205
+ seg_overlay = image.copy()
206
+ crop_img = np.zeros_like(image)
207
+ info_log = "Analysis Results:\n" + "-"*20 + "\n"
208
+ max_conf = 0
209
+ primary_diag = "Normal / Not Found"
210
+ fig = go.Figure()
211
+
212
+ if results.boxes and len(results.boxes) > 0:
213
+ boxes = results.boxes.data.cpu().numpy()
214
+
215
+ for i, box in enumerate(boxes):
216
+ x1, y1, x2, y2, conf, cls_id = box
217
+ cls_name = CLASS_NAMES.get(int(cls_id), "Unknown")
218
+
219
+ if conf > max_conf:
220
+ max_conf = conf
221
+ primary_diag = cls_name
222
+ crop_img = image[int(y1):int(y2), int(x1):int(x2)]
223
+
224
+ color = (0, 165, 255)
225
+ if "High grade" in cls_name: color = (255, 0, 0)
226
+ if "Normal" in cls_name: color = (0, 255, 0)
227
+
228
+ cv2.rectangle(orig, (int(x1), int(y1)), (int(x2), int(y2)), color, 3)
229
+ label = f"{cls_name} {conf*100:.1f}%"
230
+ cv2.putText(orig, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
231
+
232
+ info_log += f"Found #{i+1}: {cls_name} ({conf*100:.1f}%)\n"
233
+
234
+ if results.masks:
235
+ mask_combined = np.zeros(image.shape[:2], dtype=np.float32)
236
+ for m_raw in results.masks.data.cpu().numpy():
237
+ m_resized = cv2.resize(m_raw, (image.shape[1], image.shape[0]))
238
+ mask_combined = np.maximum(mask_combined, m_resized)
239
+
240
+ mask_bool = mask_combined > 0.5
241
+ mask_uint8 = (mask_bool * 255).astype(np.uint8)
242
+
243
+ colored_mask = np.zeros_like(seg_overlay)
244
+ colored_mask[mask_bool] = (0, 255, 0)
245
+ seg_overlay = cv2.addWeighted(seg_overlay, 1.0, colored_mask, 0.4, 0)
246
+
247
+ contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
248
+ cv2.drawContours(seg_overlay, contours, -1, (255, 255, 255), 2)
249
+
250
+ dist_map = cv2.distanceTransform(mask_uint8, cv2.DIST_L2, 5)
251
+ y_idx, x_idx = np.where(mask_bool)
252
+ if len(x_idx) > 0:
253
+ step = max(1, len(x_idx) // 1000)
254
+ fig.add_trace(go.Scatter3d(
255
+ x=x_idx[::step], y=image.shape[0]-y_idx[::step], z=dist_map[y_idx, x_idx][::step],
256
+ mode='markers', marker=dict(size=2, color=dist_map[y_idx, x_idx][::step], colorscale='Hot', opacity=0.8)
257
+ ))
258
+ else:
259
+ info_log = "ไม่พบความผิดปกติในภาพนี้ (No Lesion Detected)"
260
+ crop_img = image
261
+
262
+ fig.update_layout(scene=dict(xaxis_title='Width', yaxis_title='Height', zaxis_title='Density'), margin=dict(l=0,r=0,b=0,t=0), height=300)
263
+
264
+ score_percent = int(max_conf * 100)
265
+ led_html = generate_led_html(score_percent, primary_diag)
266
+ audio_path = text_to_speech(f"วิเคราะห์เสร็จสิ้น ตรวจพบ {primary_diag} ความมั่นใจ {score_percent} เปอร์เซ็นต์")
267
+
268
+ return orig, seg_overlay, crop_img, fig, info_log, led_html, audio_path, crop_img, primary_diag, score_percent, history_list, history_list, image, orig, seg_overlay
269
+
270
+ # ============================================
271
+ # 4) Gradio UI
272
+ # ============================================
273
+ css = """
274
+ @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700;900&display=swap');
275
+ .logo-container { display: flex; justify-content: flex-end; align-items: center; gap: 20px; }
276
+ #intro-overlay { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background-color: #000; z-index: 99999; display: flex; flex-direction: column; justify-content: center; align-items: center; animation: fadeOutOverlay 1s ease-in-out 4.5s forwards; pointer-events: none; }
277
+ .intro-content { display: flex; gap: 40px; align-items: center; animation: zoomInLogos 3s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards; }
278
+ .intro-logo { height: 120px; width: auto; filter: drop-shadow(0 0 10px rgba(255,255,255,0.3)); }
279
+ .intro-text-container { margin-top: 40px; text-align: center; opacity: 0; animation: textSlideUp 1.5s ease-out 1.2s forwards; }
280
+ .intro-title { color: #ffffff; font-family: 'Montserrat', sans-serif; font-size: 2.5rem; font-weight: 900; text-transform: uppercase; letter-spacing: 2px; text-shadow: 0 0 20px rgba(255, 255, 255, 0.6); line-height: 1.2; margin-bottom: 10px; }
281
+ .intro-subtitle { color: #b3b3b3; font-family: 'Montserrat', sans-serif; font-size: 1.2rem; font-weight: 400; letter-spacing: 4px; }
282
+ @keyframes zoomInLogos { 0% { transform: scale(0.8); opacity: 0; } 50% { transform: scale(1.05); opacity: 1; } 100% { transform: scale(1.0); opacity: 1; } }
283
+ @keyframes textSlideUp { 0% { transform: translateY(30px); opacity: 0; } 100% { transform: translateY(0); opacity: 1; } }
284
+ @keyframes fadeOutOverlay { to { opacity: 0; visibility: hidden; z-index: -1; } }
285
+
286
+ /* Floating Chatbot CSS */
287
+ #floating_container { position: fixed; bottom: 25px; left: 25px; z-index: 9999; display: flex; flex-direction: column; align-items: flex-start; }
288
+ #chat_window { width: 380px; height: 550px; background: white; border-radius: 20px; box-shadow: 0 15px 50px rgba(0,0,0,0.25); margin-bottom: 15px; display: none; flex-direction: column; border: 1px solid #eee; overflow: hidden; }
289
+ .show-chat #chat_window { display: flex !important; }
290
+ #chat_btn { width: 80px; height: 80px; background: white; border-radius: 50%; cursor: pointer; display: flex; justify-content: center; align-items: center; box-shadow: 0 8px 30px rgba(0,0,0,0.2); transition: 0.3s; border: 2px solid #0072ff; }
291
+ #chat_btn:hover { transform: scale(1.1); }
292
+ #chat_btn img { width: 65px; height: 65px; object-fit: contain; border-radius: 50%; }
293
+ """
294
+
295
+ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Ovarian Tumor AI") as demo:
296
+ # --- Intro Overlay ---
297
+ gr.HTML(f"""<div id="intro-overlay"><audio autoplay><source src="{INTRO_SOUND_URL}" type="audio/mpeg"></audio><div class="intro-content"><img src="{LOGO_KMUTNB_URL}" class="intro-logo"><img src="{LOGO_RAMA_URL}" class="intro-logo"></div><div class="intro-text-container"><div class="intro-title">Deep Learning for<br>Ovarian Tumor Detection</div><div class="intro-title" style="font-size: 1.8rem; color: #E50914;">in Ultrasound Images</div><div class="intro-subtitle">AI MEDICAL DIAGNOSIS SYSTEM</div></div></div>""")
298
+
299
+ # --- Header ---
300
+ with gr.Row(variant="panel"):
301
+ with gr.Column(scale=3):
302
+ gr.Markdown("# 🏥 Ovarian Tumor Diagnosis System")
303
+ gr.Markdown("AI System for Ovarian Tumor Detection & Diagnosis")
304
+ gr.Markdown("จัดทำโดย นายภานรินทร์ เปียกบุตร & นางสาวภาพิมล ไพจิตโรจนา")
305
+ with gr.Column(scale=2):
306
+ with gr.Row(elem_classes="logo-container"):
307
+ gr.Image(LOGO_KMUTNB_URL, show_label=False, container=False, height=65)
308
+ gr.Image(LOGO_RAMA_URL, show_label=False, container=False, height=65)
309
+
310
+ # State Variables
311
+ state_crop = gr.State(None)
312
+ state_info = gr.State("")
313
+ state_diag = gr.State("")
314
+ state_conf = gr.State(0)
315
+ state_gallery = gr.State([])
316
+ state_img_orig = gr.State(None)
317
+ state_img_det = gr.State(None)
318
+ state_img_seg = gr.State(None)
319
+ state_fig = gr.State(None)
320
+
321
+ # --- Main UI ---
322
+ with gr.Tabs():
323
+ with gr.Tab("1. Detection Analysis"):
324
+ with gr.Row():
325
+ with gr.Column(scale=2):
326
+ img_in = gr.Image(label="Upload Ultrasound Image", type="numpy", height=400)
327
+ btn_analyze = gr.Button("🔍 Analyze Image", variant="primary")
328
+ with gr.Column(scale=1):
329
+ html_led = gr.HTML()
330
+ aud = gr.Audio(label="Voice Assistant", autoplay=True)
331
+ txt_log = gr.Textbox(label="Detailed Findings", lines=8)
332
+
333
+ with gr.Row():
334
+ img_det = gr.Image(label="AI Detection", interactive=False)
335
+ img_seg = gr.Image(label="Segmentation", interactive=False)
336
+ img_crop = gr.Image(label="Focused Lesion", interactive=False)
337
+
338
+ with gr.Tab("2. Medical Report"):
339
+ with gr.Row():
340
+ inp_pt_name = gr.Textbox(label="Patient Name")
341
+ inp_pt_id = gr.Textbox(label="Patient ID (HN)")
342
+ btn_pdf = gr.Button("🖨️ Generate PDF Report", variant="primary")
343
+ out_pdf = gr.File(label="Download Report")
344
+
345
  with gr.Tab("3. Gallery History"):
346
  gallery_ui = gr.Gallery(columns=4, height=600)
347
 
 
356
  msg = gr.Textbox(placeholder="พิมพ์คำถามที่นี่...", show_label=False)
357
  btn_send = gr.Button("ส่งข้อความ", variant="primary")
358
 
359
+ gr.HTML(f"""
360
+ <div id="chat_btn" onclick="document.getElementById('floating_container').classList.toggle('show-chat')">
361
+ <img src="{LOGO_RAMA_URL}" />
362
+ </div>
363
+ """)
364
+
365
+ # --- Interactions ---
366
+ btn_analyze.click(
367
+ analyze_image,
368
+ [img_in, state_gallery],
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(pdf_wrapper, [inp_pt_name, inp_pt_id, state_diag, state_conf], out_pdf)
377
 
378
  # Chat interactions
379
  btn_send.click(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], [chatbot, msg])
380
  msg.submit(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], [chatbot, msg])
381
+
382
+ if __name__ == "__main__":
383
+ demo.launch()