Phanarin commited on
Commit
ee5da3b
·
verified ·
1 Parent(s): 982dfdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -119,36 +119,50 @@ def create_medical_report(pt_name, pt_id, diagnosis, conf):
119
  print(f"PDF Error: {e}")
120
  return None
121
 
122
- # --- Chat Function (Added Missing Function) ---
123
  def chat_fn(message, history, crop_img, info_text, diagnosis):
124
- if history is None: history = []
 
 
125
 
 
 
 
 
126
  if not GOOGLE_API_KEY:
127
- response = "ระบบ Chat ไม่สามารถใช้งานได้เนื่องจากไม่มี API KEY (กรุณาตั้งค่า Secret 'GOOGLE_API_KEY')"
128
- history.append((message, response))
129
  return history
130
 
131
  try:
132
- # Construct Prompt with Context
 
 
 
133
  context_prompt = f"""
134
- คุณคือผู้ช่วยทางการแพทย์อัจฉริยะ (AI Medical Assistant) จากรามาธิบดี
135
- ข้อมูลผลการตรวจปัจจุบันของผู้ป่วย:
136
- - การินิจฉัหล: {diagnosis}
137
- - รายะเอียดทงเทคนิ: {info_text}
 
138
 
139
- คำถามจากผู้ใช้: {message}
140
 
141
- กรุณาตอบคำถมอย่สุภาพ ใหข้มูลที่เป็นประโยชน์ แต่เน้ำว่าผลการตรวจต้องยืนยันโดยแพทย์ผู้เชี่ยวชาญเสมอ
142
- ตอบเป็นภาษาไทย
143
  """
144
 
145
- model = genai.GenerativeModel('gemini-pro')
 
146
  response = model.generate_content(context_prompt)
147
  bot_reply = response.text
 
148
  except Exception as e:
149
- bot_reply = f"เกิดข้อผิดพลาดในการเชื่อมต่อ AI: {str(e)}"
 
150
 
151
- history.append((message, bot_reply))
 
 
152
  return history
153
 
154
  # ============================================
@@ -341,7 +355,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Ovarian Tumor AI") as dem
341
  with gr.Column(elem_id="floating_container"):
342
  with gr.Column(elem_id="chat_window"):
343
  gr.HTML(f"<div style='background:linear-gradient(90deg, #0072ff, #00c6ff); color:white; padding:15px; border-radius:15px 15px 0 0;'><b>💬 ปรึกษาน้องดูแล</b></div>")
344
- chatbot = gr.Chatbot(height=400, show_label=False, avatar_images=(None, LOGO_RAMA_URL))
345
  msg = gr.Textbox(placeholder="พิมพ์คำถามที่นี่...", show_label=False)
346
  btn_send = gr.Button("ส่งข้อความ", variant="primary")
347
 
 
119
  print(f"PDF Error: {e}")
120
  return None
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
  # ============================================
 
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