Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 123 |
def chat_fn(message, history, crop_img, info_text, diagnosis):
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
if not GOOGLE_API_KEY:
|
| 127 |
-
|
| 128 |
-
history.append(
|
| 129 |
return history
|
| 130 |
|
| 131 |
try:
|
| 132 |
-
#
|
|
|
|
|
|
|
|
|
|
| 133 |
context_prompt = f"""
|
| 134 |
-
คุณคือผู้ช่วยทางการแพทย์อัจฉริยะ (AI Medical Assistant)
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
-
|
|
|
|
| 138 |
|
| 139 |
-
คำถาม
|
| 140 |
|
| 141 |
-
|
| 142 |
-
ตอบเป็นภาษาไทย
|
| 143 |
"""
|
| 144 |
|
| 145 |
-
|
|
|
|
| 146 |
response = model.generate_content(context_prompt)
|
| 147 |
bot_reply = response.text
|
|
|
|
| 148 |
except Exception as e:
|
| 149 |
-
bot_reply = f"เกิดข้อผิดพลาด
|
|
|
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
| 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 |
|