Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -1,32 +1,63 @@
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
| 5 |
kratom_contexts = {
|
| 6 |
-
"urination":
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
-
#
|
| 14 |
def answer_question(user_input):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
iface = gr.Interface(
|
| 23 |
fn=answer_question,
|
| 24 |
-
inputs=gr.Textbox(label="
|
| 25 |
-
outputs=gr.Textbox(label="
|
| 26 |
-
title="Kratom
|
| 27 |
-
description="
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# ✅ Launch the app
|
| 31 |
if __name__ == "__main__":
|
| 32 |
iface.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load semantic similarity model
|
| 6 |
+
model = SentenceTransformer('distiluse-base-multilingual-cased-v2') # Supports Thai + English
|
| 7 |
+
|
| 8 |
+
# Define bilingual Kratom knowledge base
|
| 9 |
kratom_contexts = {
|
| 10 |
+
"urination": {
|
| 11 |
+
"keywords": ["urination", "pee", "ปัสสาวะ", "ฉี่"],
|
| 12 |
+
"response": {
|
| 13 |
+
"en": "Kratom may affect urination due to its impact on the autonomic nervous system.",
|
| 14 |
+
"th": "กระท่อมอาจส่งผลต่อการปัสสาวะเนื่องจากมีผลต่อระบบประสาทอัตโนมัติ"
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
"skin": {
|
| 18 |
+
"keywords": ["skin", "ผิวหนัง", "darkening", "dry skin", "ผิวแห้ง"],
|
| 19 |
+
"response": {
|
| 20 |
+
"en": "Long-term Kratom use may cause skin darkening and dryness.",
|
| 21 |
+
"th": "การใช้กระท่อมระยะยาวอาจทำให้ผิวคล้ำและแห้ง"
|
| 22 |
+
}
|
| 23 |
+
},
|
| 24 |
+
"behavior": {
|
| 25 |
+
"keywords": ["behavior", "พฤติกรรม", "agitation", "sedation", "กระสับกระส่าย", "ง่วง"],
|
| 26 |
+
"response": {
|
| 27 |
+
"en": "Kratom can cause stimulant or sedative behaviors depending on dosage.",
|
| 28 |
+
"th": "กระท่อมสามารถกระตุ้นหรือกดประสาทได้ขึ้นอยู่กับขนาดที่ใช้"
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
}
|
| 32 |
|
| 33 |
+
# Semantic matching function
|
| 34 |
def answer_question(user_input):
|
| 35 |
+
user_embedding = model.encode(user_input, convert_to_tensor=True)
|
| 36 |
+
best_match = None
|
| 37 |
+
best_score = 0.0
|
| 38 |
+
|
| 39 |
+
for topic, data in kratom_contexts.items():
|
| 40 |
+
for keyword in data["keywords"]:
|
| 41 |
+
keyword_embedding = model.encode(keyword, convert_to_tensor=True)
|
| 42 |
+
score = util.cos_sim(user_embedding, keyword_embedding).item()
|
| 43 |
+
if score > best_score:
|
| 44 |
+
best_score = score
|
| 45 |
+
best_match = topic
|
| 46 |
+
|
| 47 |
+
if best_score > 0.5 and best_match:
|
| 48 |
+
response = kratom_contexts[best_match]["response"]
|
| 49 |
+
return f"🇬🇧 {response['en']}\n🇹🇭 {response['th']}"
|
| 50 |
+
else:
|
| 51 |
+
return "❓ Sorry, I couldn’t match your question. Try asking about urination, skin, or behavior (in Thai or English)."
|
| 52 |
|
| 53 |
+
# Gradio UI
|
| 54 |
iface = gr.Interface(
|
| 55 |
fn=answer_question,
|
| 56 |
+
inputs=gr.Textbox(label="ถามเกี่ยวกับผลของกระท่อม (Thai or English)"),
|
| 57 |
+
outputs=gr.Textbox(label="คำตอบจากผู้ช่วยทางการแพทย์"),
|
| 58 |
+
title="Kratom Semantic Tutor",
|
| 59 |
+
description="Ask about Kratom effects in Thai or English. Uses semantic similarity to match flexible phrasing."
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
iface.launch()
|