Phanarin commited on
Commit
3f32be7
·
verified ·
1 Parent(s): f417650

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -82
app.py CHANGED
@@ -24,13 +24,15 @@ from reportlab.pdfbase.ttfonts import TTFont
24
  # 1) Configuration & Setup
25
  # ============================================
26
 
27
- # 🔑 API KEY: แนะนำให้ตั้งใน Settings > Secrets ของ HF Space ชื่อ "GOOGLE_API_KEY"
28
- # ถ้าไม่มีจะใช้ค่าว่าง (Chat จะไม่ทำงานสมบูรณ์)
29
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
30
  if GOOGLE_API_KEY:
31
- genai.configure(api_key=GOOGLE_API_KEY)
 
 
 
32
 
33
- # 📂 Model Path: อัปโหลดไฟล์ .pt ไปที่ Files ใน HF Space โดยตรง
34
  MODEL_PATH = "otu_multiclass_yolo11s_v2.pt"
35
 
36
  LOGO_KMUTNB_URL = "https://www.mou.kmutnb.ac.th/logo_kmutnb.png"
@@ -43,20 +45,28 @@ CLASS_NAMES = {
43
  }
44
 
45
  # ---------------------------------------------------------
46
- # 🛠️ AUTO-DOWNLOAD FONTS
47
  # ---------------------------------------------------------
48
  def force_download_font(url, filename):
49
  if not os.path.exists(filename):
50
  print(f"📥 Downloading {filename}...")
51
  try:
52
- r = requests.get(url, allow_redirects=True)
53
- with open(filename, 'wb') as f:
54
- f.write(r.content)
 
 
 
 
 
 
 
55
  except Exception as e:
56
  print(f"❌ Error downloading {filename}: {e}")
57
  return False
58
  return True
59
 
 
60
  font_urls = [
61
  ("https://github.com/nutjunkie/thaifonts_sipa/raw/master/sipa_fonts/THSarabunNew/THSarabunNew.ttf", "THSarabunNew.ttf"),
62
  ("https://github.com/nutjunkie/thaifonts_sipa/raw/master/sipa_fonts/THSarabunNew/THSarabunNew%20Bold.ttf", "THSarabunNew-Bold.ttf")
@@ -65,13 +75,20 @@ font_urls = [
65
  for url, fname in font_urls:
66
  force_download_font(url, fname)
67
 
 
 
 
 
68
  try:
69
- if os.path.exists("THSarabunNew.ttf"):
70
  pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))
71
- if os.path.exists("THSarabunNew-Bold.ttf"):
 
 
72
  pdfmetrics.registerFont(TTFont('THSarabun-Bold', 'THSarabunNew-Bold.ttf'))
 
73
  except Exception as e:
74
- print(f"⚠️ Font Registration Error: {e}")
75
 
76
  # ============================================
77
  # 2) Helper Functions
@@ -100,13 +117,10 @@ def create_medical_report(pt_name, pt_id, diagnosis, conf):
100
  filename = tempfile.mktemp(suffix=".pdf")
101
  c = canvas.Canvas(filename, pagesize=A4)
102
 
103
- # Use registered font if available, else standard
104
- font_name = 'THSarabun-Bold' if 'THSarabun-Bold' in pdfmetrics.getRegisteredFontNames() else 'Helvetica-Bold'
105
-
106
- c.setFont(font_name, 24)
107
  c.drawString(2*cm, 27*cm, "Medical Image Analysis Report")
108
 
109
- c.setFont(font_name, 16)
110
  c.drawString(2*cm, 25*cm, f"Patient Name: {pt_name}")
111
  c.drawString(2*cm, 24*cm, f"Patient ID: {pt_id}")
112
  c.drawString(2*cm, 22*cm, f"Diagnosis Result: {diagnosis}")
@@ -121,48 +135,39 @@ def create_medical_report(pt_name, pt_id, diagnosis, conf):
121
 
122
  # --- Chat Function ---
123
  def chat_fn(message, history, crop_img, info_text, diagnosis):
124
- # ถ้า history เป็น None ให้เริ่มเป็น List ว่าง
125
- if history is None:
126
- history = []
127
 
128
- # 1. ใส่ข้อความฝั่ง User เข้าไปก่อน
129
  history.append({"role": "user", "content": message})
130
 
131
- # เช็ค API Key
132
  if not GOOGLE_API_KEY:
133
- error_msg = "❌ ไม่พบ API KEY: กรุณาไปที่ Settings > Secrets แล้วตั้งค่า 'GOOGLE_API_KEY'"
134
  history.append({"role": "assistant", "content": error_msg})
135
  return history
136
 
137
  try:
138
- # เตรียม Prompt
139
  diag_str = diagnosis if diagnosis else "ยังไม่มีการวินิจฉัย"
140
  info_str = info_text if info_text else "ไม่มีรายละเอียด"
141
 
142
  context_prompt = f"""
143
  บทบาท: คุณคือผู้ช่วยทางการแพทย์อัจฉริยะ (AI Medical Assistant)
144
-
145
- ข้อมูลผู้ป่วยปัจจุบัน:
146
  - ผลการวินิจฉัย: {diag_str}
147
  - รายละเอียด: {info_str}
148
 
149
  คำถาม: {message}
150
-
151
- ตอบเป็นภาษาไทย กระชับ สุภาพ และต้องลงท้ายว่า "ผลการวินิจฉัยต้องยืนยันโดยแพทย์ผู้เชี่ยวชาญ"
152
  """
153
 
154
- # เรียก Gemini
155
  model = genai.GenerativeModel('gemini-1.5-flash')
156
  response = model.generate_content(context_prompt)
157
  bot_reply = response.text
158
 
159
  except Exception as e:
160
- bot_reply = f"เกิดข้อผิดพลาด: {str(e)}"
161
- print(f"Error: {e}")
162
 
163
- # 2. ใส่ข้อความฝั่ง AI (Assistant) เข้าไป
164
  history.append({"role": "assistant", "content": bot_reply})
165
-
166
  return history
167
 
168
  # ============================================
@@ -178,10 +183,9 @@ def analyze_image(image, history_list):
178
  error_msg = f"⚠️ Model file not found at {MODEL_PATH}. Please upload the .pt file."
179
  return image, image, image, go.Figure(), error_msg, "", None, image, "Error", 0, history_list, history_list, image, image, image
180
 
181
- # Add to history
182
  history_list.append(image)
183
 
184
- # --- [STEP 1: Image Enhancement] ---
185
  lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
186
  l, a, b = cv2.split(lab)
187
  clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
@@ -189,17 +193,9 @@ def analyze_image(image, history_list):
189
  enhanced_img = cv2.merge((cl,a,b))
190
  enhanced_img = cv2.cvtColor(enhanced_img, cv2.COLOR_LAB2RGB)
191
 
192
- # --- [STEP 2: Inference] ---
193
  try:
194
  model = YOLO(MODEL_PATH)
195
- results = model.predict(
196
- enhanced_img,
197
- imgsz=640,
198
- conf=0.25,
199
- iou=0.45,
200
- augment=True,
201
- verbose=False
202
- )[0]
203
  except Exception as e:
204
  return image, image, image, go.Figure(), f"Inference Error: {e}", "", None, image, "Error", 0, history_list, history_list, image, image, image
205
 
@@ -211,14 +207,11 @@ def analyze_image(image, history_list):
211
  primary_diag = "Normal / Not Found"
212
  fig = go.Figure()
213
 
214
- # --- [STEP 3: Process Results] ---
215
  if results.boxes and len(results.boxes) > 0:
216
  boxes = results.boxes.data.cpu().numpy()
217
-
218
  for i, box in enumerate(boxes):
219
  x1, y1, x2, y2, conf, cls_id = box
220
  cls_name = CLASS_NAMES.get(int(cls_id), "Unknown")
221
-
222
  if conf > max_conf:
223
  max_conf = conf
224
  primary_diag = cls_name
@@ -229,48 +222,37 @@ def analyze_image(image, history_list):
229
  if "Normal" in cls_name: color = (0, 255, 0)
230
 
231
  cv2.rectangle(orig, (int(x1), int(y1)), (int(x2), int(y2)), color, 3)
232
- label = f"{cls_name} {conf*100:.1f}%"
233
- cv2.putText(orig, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
234
-
235
  info_log += f"Found #{i+1}: {cls_name} ({conf*100:.1f}%)\n"
236
 
237
- # --- [STEP 4: Segmentation] ---
238
  if results.masks:
239
  mask_combined = np.zeros(image.shape[:2], dtype=np.float32)
240
  for m_raw in results.masks.data.cpu().numpy():
241
  m_resized = cv2.resize(m_raw, (image.shape[1], image.shape[0]))
242
  mask_combined = np.maximum(mask_combined, m_resized)
243
-
244
  mask_bool = mask_combined > 0.5
245
- mask_uint8 = (mask_bool * 255).astype(np.uint8)
246
-
247
  colored_mask = np.zeros_like(seg_overlay)
248
  colored_mask[mask_bool] = (0, 255, 0)
249
  seg_overlay = cv2.addWeighted(seg_overlay, 1.0, colored_mask, 0.4, 0)
250
-
251
- contours, _ = cv2.findContours(mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
252
- cv2.drawContours(seg_overlay, contours, -1, (255, 255, 255), 2)
253
-
254
- # 3D Plot Data
255
- dist_map = cv2.distanceTransform(mask_uint8, cv2.DIST_L2, 5)
256
  y_idx, x_idx = np.where(mask_bool)
257
  if len(x_idx) > 0:
258
- step = max(1, len(x_idx) // 1000)
259
- fig.add_trace(go.Scatter3d(
260
- x=x_idx[::step], y=image.shape[0]-y_idx[::step], z=dist_map[y_idx, x_idx][::step],
261
- mode='markers', marker=dict(size=2, color=dist_map[y_idx, x_idx][::step], colorscale='Hot', opacity=0.8)
262
  ))
263
  else:
264
- info_log = "ไม่พบความผิดปกติในภาพนี้ (No Lesion Detected)"
265
- crop_img = image # Show full image if no crop
266
-
267
- 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)
268
 
 
 
269
  score_percent = int(max_conf * 100)
270
  led_html = generate_led_html(score_percent, primary_diag)
271
- audio_path = text_to_speech(f"วิเคราะห์เสร็จสิ้น ตรวจพบ {primary_diag} ความมั่นใจ {score_percent} เปอร์เซ็นต์")
272
 
273
- # Returning 15 outputs to match the UI State slots
274
  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
275
 
276
  # ============================================
@@ -298,22 +280,19 @@ css = """
298
  #chat_btn img { width: 65px; height: 65px; object-fit: contain; border-radius: 50%; }
299
  """
300
 
301
- with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Ovarian Tumor AI") as demo:
302
- # --- Intro Overlay ---
303
  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>""")
304
 
305
- # --- Header ---
306
  with gr.Row(variant="panel"):
307
  with gr.Column(scale=3):
308
  gr.Markdown("# 🏥 Ovarian Tumor Diagnosis System")
309
  gr.Markdown("AI System for Ovarian Tumor Detection & Diagnosis")
310
- gr.Markdown("จัดทำโดย นายภานรินทร์ เปียกบุตร & นางสาวภาพิมล ไพจิตโรจนา")
311
  with gr.Column(scale=2):
312
  with gr.Row(elem_classes="logo-container"):
313
  gr.Image(LOGO_KMUTNB_URL, show_label=False, container=False, height=65)
314
  gr.Image(LOGO_RAMA_URL, show_label=False, container=False, height=65)
315
 
316
- # State Variables
317
  state_crop = gr.State(None)
318
  state_info = gr.State("")
319
  state_diag = gr.State("")
@@ -322,9 +301,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Ovarian Tumor AI") as dem
322
  state_img_orig = gr.State(None)
323
  state_img_det = gr.State(None)
324
  state_img_seg = gr.State(None)
325
- state_fig = gr.State(None) # Added state for Figure
326
 
327
- # --- Main UI ---
328
  with gr.Tabs():
329
  with gr.Tab("1. Detection Analysis"):
330
  with gr.Row():
@@ -351,11 +329,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Ovarian Tumor AI") as dem
351
  with gr.Tab("3. Gallery History"):
352
  gallery_ui = gr.Gallery(columns=4, height=600)
353
 
354
- # --- Floating Chatbot ---
355
  with gr.Column(elem_id="floating_container"):
356
  with gr.Column(elem_id="chat_window"):
357
  gr.HTML(f"<div style='background:linear-gradient(90deg, #0072ff, #00c6ff); color:white; padding:15px; border-radius:15px 15px 0 0;'><b>💬 ปรึกษาน้องดูแล</b></div>")
 
 
358
  chatbot = gr.Chatbot(height=400, show_label=False, avatar_images=(None, LOGO_RAMA_URL), type="messages")
 
359
  msg = gr.Textbox(placeholder="พิมพ์คำถามที่นี่...", show_label=False)
360
  btn_send = gr.Button("ส่งข้อความ", variant="primary")
361
 
@@ -365,23 +346,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Ovarian Tumor AI") as dem
365
  </div>
366
  """)
367
 
368
- # --- Interactions ---
369
  btn_analyze.click(
370
  analyze_image,
371
  [img_in, state_gallery],
372
  [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]
373
  )
374
 
375
- # Function to generate PDF Wrapper
376
  def pdf_wrapper(name, pid, diag, conf):
377
  if not diag: return None
378
  return create_medical_report(name, pid, diag, conf)
379
 
380
  btn_pdf.click(pdf_wrapper, [inp_pt_name, inp_pt_id, state_diag, state_conf], out_pdf)
381
-
382
- # Chat interaction
383
  btn_send.click(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], chatbot)
384
  msg.submit(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], chatbot)
385
 
 
386
  if __name__ == "__main__":
387
- demo.launch()
 
24
  # 1) Configuration & Setup
25
  # ============================================
26
 
27
+ # 🔑 API KEY
 
28
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
29
  if GOOGLE_API_KEY:
30
+ try:
31
+ genai.configure(api_key=GOOGLE_API_KEY)
32
+ except:
33
+ print("⚠️ Failed to configure Google AI")
34
 
35
+ # 📂 Model Path
36
  MODEL_PATH = "otu_multiclass_yolo11s_v2.pt"
37
 
38
  LOGO_KMUTNB_URL = "https://www.mou.kmutnb.ac.th/logo_kmutnb.png"
 
45
  }
46
 
47
  # ---------------------------------------------------------
48
+ # 🛠️ ROBUST FONT DOWNLOADER (แก้ปัญหา Font Error)
49
  # ---------------------------------------------------------
50
  def force_download_font(url, filename):
51
  if not os.path.exists(filename):
52
  print(f"📥 Downloading {filename}...")
53
  try:
54
+ # เพิ่ม User-Agent เพื่อป้องกัน GitHub ปฏิเสธการโหลด
55
+ headers = {'User-Agent': 'Mozilla/5.0'}
56
+ r = requests.get(url, headers=headers, allow_redirects=True)
57
+
58
+ if r.status_code == 200:
59
+ with open(filename, 'wb') as f:
60
+ f.write(r.content)
61
+ else:
62
+ print(f"❌ Failed to download {filename} (Status: {r.status_code})")
63
+ return False
64
  except Exception as e:
65
  print(f"❌ Error downloading {filename}: {e}")
66
  return False
67
  return True
68
 
69
+ # URL สำรองสำหรับฟอนต์
70
  font_urls = [
71
  ("https://github.com/nutjunkie/thaifonts_sipa/raw/master/sipa_fonts/THSarabunNew/THSarabunNew.ttf", "THSarabunNew.ttf"),
72
  ("https://github.com/nutjunkie/thaifonts_sipa/raw/master/sipa_fonts/THSarabunNew/THSarabunNew%20Bold.ttf", "THSarabunNew-Bold.ttf")
 
75
  for url, fname in font_urls:
76
  force_download_font(url, fname)
77
 
78
+ # Register Font (ใส่ Try/Except เพื่อไม่ให้แอพพังถ้าฟอนต์เสีย)
79
+ FONT_REGULAR = 'Helvetica'
80
+ FONT_BOLD = 'Helvetica-Bold'
81
+
82
  try:
83
+ if os.path.exists("THSarabunNew.ttf") and os.path.getsize("THSarabunNew.ttf") > 1000:
84
  pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))
85
+ FONT_REGULAR = 'THSarabun'
86
+
87
+ if os.path.exists("THSarabunNew-Bold.ttf") and os.path.getsize("THSarabunNew-Bold.ttf") > 1000:
88
  pdfmetrics.registerFont(TTFont('THSarabun-Bold', 'THSarabunNew-Bold.ttf'))
89
+ FONT_BOLD = 'THSarabun-Bold'
90
  except Exception as e:
91
+ print(f"⚠️ Font Registration Error: {e} - Using default Helvetica")
92
 
93
  # ============================================
94
  # 2) Helper Functions
 
117
  filename = tempfile.mktemp(suffix=".pdf")
118
  c = canvas.Canvas(filename, pagesize=A4)
119
 
120
+ c.setFont(FONT_BOLD, 24)
 
 
 
121
  c.drawString(2*cm, 27*cm, "Medical Image Analysis Report")
122
 
123
+ c.setFont(FONT_BOLD, 16)
124
  c.drawString(2*cm, 25*cm, f"Patient Name: {pt_name}")
125
  c.drawString(2*cm, 24*cm, f"Patient ID: {pt_id}")
126
  c.drawString(2*cm, 22*cm, f"Diagnosis Result: {diagnosis}")
 
135
 
136
  # --- Chat Function ---
137
  def chat_fn(message, history, crop_img, info_text, diagnosis):
138
+ if history is None: history = []
 
 
139
 
140
+ # User Message
141
  history.append({"role": "user", "content": message})
142
 
 
143
  if not GOOGLE_API_KEY:
144
+ error_msg = "❌ ไม่พบ API KEY: กรุณาไปที่ Settings > Secrets แล้วตั้งค่า 'GOOGLE_API_KEY' และ Restart Space"
145
  history.append({"role": "assistant", "content": error_msg})
146
  return history
147
 
148
  try:
 
149
  diag_str = diagnosis if diagnosis else "ยังไม่มีการวินิจฉัย"
150
  info_str = info_text if info_text else "ไม่มีรายละเอียด"
151
 
152
  context_prompt = f"""
153
  บทบาท: คุณคือผู้ช่วยทางการแพทย์อัจฉริยะ (AI Medical Assistant)
154
+ ข้อมูลผู้ป่วย:
 
155
  - ผลการวินิจฉัย: {diag_str}
156
  - รายละเอียด: {info_str}
157
 
158
  คำถาม: {message}
159
+ คำตอบ: (ตอบสั้นกระชับ ภาษาไทย และลงท้ายว่าต้องยืนยันโดยแพทย์)
 
160
  """
161
 
 
162
  model = genai.GenerativeModel('gemini-1.5-flash')
163
  response = model.generate_content(context_prompt)
164
  bot_reply = response.text
165
 
166
  except Exception as e:
167
+ bot_reply = f"System Error: {str(e)}"
 
168
 
169
+ # Bot Message
170
  history.append({"role": "assistant", "content": bot_reply})
 
171
  return history
172
 
173
  # ============================================
 
183
  error_msg = f"⚠️ Model file not found at {MODEL_PATH}. Please upload the .pt file."
184
  return image, image, image, go.Figure(), error_msg, "", None, image, "Error", 0, history_list, history_list, image, image, image
185
 
 
186
  history_list.append(image)
187
 
188
+ # Enhance
189
  lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
190
  l, a, b = cv2.split(lab)
191
  clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
 
193
  enhanced_img = cv2.merge((cl,a,b))
194
  enhanced_img = cv2.cvtColor(enhanced_img, cv2.COLOR_LAB2RGB)
195
 
 
196
  try:
197
  model = YOLO(MODEL_PATH)
198
+ results = model.predict(enhanced_img, imgsz=640, conf=0.25, iou=0.45, augment=True, verbose=False)[0]
 
 
 
 
 
 
 
199
  except Exception as e:
200
  return image, image, image, go.Figure(), f"Inference Error: {e}", "", None, image, "Error", 0, history_list, history_list, image, image, image
201
 
 
207
  primary_diag = "Normal / Not Found"
208
  fig = go.Figure()
209
 
 
210
  if results.boxes and len(results.boxes) > 0:
211
  boxes = results.boxes.data.cpu().numpy()
 
212
  for i, box in enumerate(boxes):
213
  x1, y1, x2, y2, conf, cls_id = box
214
  cls_name = CLASS_NAMES.get(int(cls_id), "Unknown")
 
215
  if conf > max_conf:
216
  max_conf = conf
217
  primary_diag = cls_name
 
222
  if "Normal" in cls_name: color = (0, 255, 0)
223
 
224
  cv2.rectangle(orig, (int(x1), int(y1)), (int(x2), int(y2)), color, 3)
 
 
 
225
  info_log += f"Found #{i+1}: {cls_name} ({conf*100:.1f}%)\n"
226
 
 
227
  if results.masks:
228
  mask_combined = np.zeros(image.shape[:2], dtype=np.float32)
229
  for m_raw in results.masks.data.cpu().numpy():
230
  m_resized = cv2.resize(m_raw, (image.shape[1], image.shape[0]))
231
  mask_combined = np.maximum(mask_combined, m_resized)
232
+
233
  mask_bool = mask_combined > 0.5
 
 
234
  colored_mask = np.zeros_like(seg_overlay)
235
  colored_mask[mask_bool] = (0, 255, 0)
236
  seg_overlay = cv2.addWeighted(seg_overlay, 1.0, colored_mask, 0.4, 0)
237
+
238
+ # 3D Plot
239
+ step = 10
 
 
 
240
  y_idx, x_idx = np.where(mask_bool)
241
  if len(x_idx) > 0:
242
+ fig.add_trace(go.Scatter3d(
243
+ x=x_idx[::step], y=image.shape[0]-y_idx[::step], z=np.ones_like(x_idx[::step]),
244
+ mode='markers', marker=dict(size=2, color='red', opacity=0.5)
 
245
  ))
246
  else:
247
+ info_log = "ไม่พบความผิดปกติในภาพนี้"
248
+ crop_img = image
 
 
249
 
250
+ fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'), height=300, margin=dict(l=0,r=0,b=0,t=0))
251
+
252
  score_percent = int(max_conf * 100)
253
  led_html = generate_led_html(score_percent, primary_diag)
254
+ audio_path = text_to_speech(f"ตรวจพบ {primary_diag}")
255
 
 
256
  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
257
 
258
  # ============================================
 
280
  #chat_btn img { width: 65px; height: 65px; object-fit: contain; border-radius: 50%; }
281
  """
282
 
283
+ # แก้ไข: เอา css ออกจาก Blocks() แล้วไปใส่ใน launch()
284
+ with gr.Blocks(title="Ovarian Tumor AI") as demo:
285
  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>""")
286
 
 
287
  with gr.Row(variant="panel"):
288
  with gr.Column(scale=3):
289
  gr.Markdown("# 🏥 Ovarian Tumor Diagnosis System")
290
  gr.Markdown("AI System for Ovarian Tumor Detection & Diagnosis")
 
291
  with gr.Column(scale=2):
292
  with gr.Row(elem_classes="logo-container"):
293
  gr.Image(LOGO_KMUTNB_URL, show_label=False, container=False, height=65)
294
  gr.Image(LOGO_RAMA_URL, show_label=False, container=False, height=65)
295
 
 
296
  state_crop = gr.State(None)
297
  state_info = gr.State("")
298
  state_diag = gr.State("")
 
301
  state_img_orig = gr.State(None)
302
  state_img_det = gr.State(None)
303
  state_img_seg = gr.State(None)
304
+ state_fig = gr.State(None)
305
 
 
306
  with gr.Tabs():
307
  with gr.Tab("1. Detection Analysis"):
308
  with gr.Row():
 
329
  with gr.Tab("3. Gallery History"):
330
  gallery_ui = gr.Gallery(columns=4, height=600)
331
 
332
+ # Floating Chatbot (type="messages")
333
  with gr.Column(elem_id="floating_container"):
334
  with gr.Column(elem_id="chat_window"):
335
  gr.HTML(f"<div style='background:linear-gradient(90deg, #0072ff, #00c6ff); color:white; padding:15px; border-radius:15px 15px 0 0;'><b>💬 ปรึกษาน้องดูแล</b></div>")
336
+
337
+ # แก้ไข: type="messages" จะทำงานได้เมื่อ gradio >= 5.0 ใน requirements.txt
338
  chatbot = gr.Chatbot(height=400, show_label=False, avatar_images=(None, LOGO_RAMA_URL), type="messages")
339
+
340
  msg = gr.Textbox(placeholder="พิมพ์คำถามที่นี่...", show_label=False)
341
  btn_send = gr.Button("ส่งข้อความ", variant="primary")
342
 
 
346
  </div>
347
  """)
348
 
 
349
  btn_analyze.click(
350
  analyze_image,
351
  [img_in, state_gallery],
352
  [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]
353
  )
354
 
 
355
  def pdf_wrapper(name, pid, diag, conf):
356
  if not diag: return None
357
  return create_medical_report(name, pid, diag, conf)
358
 
359
  btn_pdf.click(pdf_wrapper, [inp_pt_name, inp_pt_id, state_diag, state_conf], out_pdf)
 
 
360
  btn_send.click(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], chatbot)
361
  msg.submit(chat_fn, [msg, chatbot, state_crop, state_info, state_diag], chatbot)
362
 
363
+ # แก้ไข: ใส่ css และ theme ที่นี่แทน
364
  if __name__ == "__main__":
365
+ demo.launch(theme=gr.themes.Soft(), css=css)