Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,49 +28,71 @@ Important guidelines:
|
|
| 28 |
|
| 29 |
Please respond in Indonesian language unless the user specifically asks for English."""
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def dental_chat(message, history):
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
history.append({"role": "assistant", "content": "Mohon masukkan pertanyaan yang valid."})
|
| 54 |
yield "", history
|
| 55 |
return
|
| 56 |
|
| 57 |
-
if not
|
| 58 |
-
history.append({"role": "user", "content":
|
| 59 |
-
history.append({"role": "assistant", "content": "Maaf, saya hanya bisa membantu dengan pertanyaan seputar kesehatan
|
| 60 |
yield "", history
|
| 61 |
return
|
| 62 |
|
| 63 |
try:
|
| 64 |
mistral_messages = [ChatMessage(role="system", content=DENTAL_SYSTEM_PROMPT)]
|
|
|
|
| 65 |
for msg in history:
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
history.append({"role": "assistant", "content": ""})
|
| 72 |
|
| 73 |
-
# Streaming response
|
| 74 |
response_text = ""
|
| 75 |
for chunk in client.chat_stream(
|
| 76 |
model="mistral-tiny",
|
|
@@ -86,13 +108,29 @@ def dental_chat(message, history):
|
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
print(f"Error: {str(e)}")
|
| 89 |
-
history
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
yield "", history
|
| 92 |
|
| 93 |
css = """
|
| 94 |
footer { display: none !important; }
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
body, .gradio-container {
|
| 97 |
background-color: #0a0e27 !important;
|
| 98 |
}
|
|
|
|
| 28 |
|
| 29 |
Please respond in Indonesian language unless the user specifically asks for English."""
|
| 30 |
|
| 31 |
+
NON_MEDICAL_KEYWORDS = [
|
| 32 |
+
'masak', 'resep', 'film', 'musik', 'lagu', 'game', 'politik', 'bisnis',
|
| 33 |
+
'saham', 'crypto', 'olahraga', 'bola', 'basket', 'travel', 'wisata',
|
| 34 |
+
'fashion', 'cuaca', 'berita', 'gosip', 'artis', 'coding', 'programming',
|
| 35 |
+
'hukum', 'harga', 'beli', 'jual', 'toko', 'belanja'
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
def extract_text(content):
|
| 39 |
+
"""Ekstrak teks dari content yang bisa berupa string, list, atau dict"""
|
| 40 |
+
if isinstance(content, str):
|
| 41 |
+
return content
|
| 42 |
+
elif isinstance(content, list):
|
| 43 |
+
texts = []
|
| 44 |
+
for item in content:
|
| 45 |
+
if isinstance(item, dict):
|
| 46 |
+
texts.append(item.get('text', '') or item.get('content', ''))
|
| 47 |
+
elif isinstance(item, str):
|
| 48 |
+
texts.append(item)
|
| 49 |
+
return ' '.join(filter(None, texts))
|
| 50 |
+
elif isinstance(content, dict):
|
| 51 |
+
return content.get('text', '') or content.get('content', '') or str(content)
|
| 52 |
+
return str(content) if content else ''
|
| 53 |
+
|
| 54 |
+
def is_medical_related(message):
|
| 55 |
+
"""Izinkan semua topik medis, blokir hanya yang jelas non-medis"""
|
| 56 |
+
msg_lower = message.lower()
|
| 57 |
+
# Blokir jika mengandung keyword non-medis yang jelas
|
| 58 |
+
non_medical_count = sum(1 for kw in NON_MEDICAL_KEYWORDS if kw in msg_lower)
|
| 59 |
+
# Jika mengandung 2+ keyword non-medis dan tidak ada konteks medis, tolak
|
| 60 |
+
medical_hints = ['sakit', 'nyeri', 'obat', 'dokter', 'gejala', 'penyakit',
|
| 61 |
+
'kesehatan', 'medis', 'klinik', 'rumah sakit', 'operasi',
|
| 62 |
+
'terapi', 'diagnosis', 'treatment', 'health', 'pain', 'disease']
|
| 63 |
+
has_medical = any(kw in msg_lower for kw in medical_hints)
|
| 64 |
+
if non_medical_count >= 2 and not has_medical:
|
| 65 |
+
return False
|
| 66 |
+
return True
|
| 67 |
|
| 68 |
def dental_chat(message, history):
|
| 69 |
+
message_text = extract_text(message)
|
| 70 |
+
|
| 71 |
+
if not message_text.strip():
|
| 72 |
+
history.append({"role": "user", "content": message_text})
|
| 73 |
history.append({"role": "assistant", "content": "Mohon masukkan pertanyaan yang valid."})
|
| 74 |
yield "", history
|
| 75 |
return
|
| 76 |
|
| 77 |
+
if not is_medical_related(message_text):
|
| 78 |
+
history.append({"role": "user", "content": message_text})
|
| 79 |
+
history.append({"role": "assistant", "content": "Maaf, saya hanya bisa membantu dengan pertanyaan seputar kesehatan dan kedokteran gigi. Silakan ajukan pertanyaan medis."})
|
| 80 |
yield "", history
|
| 81 |
return
|
| 82 |
|
| 83 |
try:
|
| 84 |
mistral_messages = [ChatMessage(role="system", content=DENTAL_SYSTEM_PROMPT)]
|
| 85 |
+
|
| 86 |
for msg in history:
|
| 87 |
+
content_text = extract_text(msg["content"])
|
| 88 |
+
if content_text.strip():
|
| 89 |
+
mistral_messages.append(ChatMessage(role=msg["role"], content=content_text))
|
| 90 |
|
| 91 |
+
mistral_messages.append(ChatMessage(role="user", content=message_text))
|
| 92 |
+
|
| 93 |
+
history.append({"role": "user", "content": message_text})
|
| 94 |
history.append({"role": "assistant", "content": ""})
|
| 95 |
|
|
|
|
| 96 |
response_text = ""
|
| 97 |
for chunk in client.chat_stream(
|
| 98 |
model="mistral-tiny",
|
|
|
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
print(f"Error: {str(e)}")
|
| 111 |
+
if history and history[-1]["content"] == "":
|
| 112 |
+
history[-1]["content"] = f"Maaf, terjadi kesalahan: {str(e)}. Silakan coba lagi."
|
| 113 |
+
else:
|
| 114 |
+
history.append({"role": "assistant", "content": f"Maaf, terjadi kesalahan: {str(e)}. Silakan coba lagi."})
|
| 115 |
yield "", history
|
| 116 |
|
| 117 |
css = """
|
| 118 |
footer { display: none !important; }
|
| 119 |
|
| 120 |
+
/* Ganti loading dots dengan teks Thinking... */
|
| 121 |
+
.progress-text span,
|
| 122 |
+
.eta-bar,
|
| 123 |
+
.generating span {
|
| 124 |
+
display: none !important;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
.generating::after {
|
| 128 |
+
content: 'Thinking...' !important;
|
| 129 |
+
color: #94a3b8 !important;
|
| 130 |
+
font-style: italic !important;
|
| 131 |
+
font-size: 0.9rem !important;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
body, .gradio-container {
|
| 135 |
background-color: #0a0e27 !important;
|
| 136 |
}
|