trapezius60 commited on
Commit
6433cdc
·
verified ·
1 Parent(s): 86bfcb6

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +49 -18
app.py CHANGED
@@ -1,32 +1,63 @@
1
  # app.py
 
2
  import gradio as gr
3
 
4
- # Define Kratom knowledge base
 
 
 
5
  kratom_contexts = {
6
- "urination": "Kratom may affect urination by interacting with the autonomic nervous system, leading to increased frequency or discomfort in some cases.",
7
- "skin": "Long-term Kratom use has been associated with skin darkening and dryness, possibly linked to hormonal or liver-related effects.",
8
- "behavior": "Depending on the dosage, Kratom can cause stimulant-like or sedative behaviors—including euphoria, drowsiness, agitation, and even dependency with prolonged use.",
9
- "heart": "Kratom may influence heart rate and blood pressure. Some users report palpitations or arrhythmia, especially at high doses.",
10
- "eyes": "Miosis (pupil constriction) and blurred vision have occasionally been observed with Kratom use due to its opioid-like activity.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  }
12
 
13
- # Basic Q&A logic with keyword matching
14
  def answer_question(user_input):
15
- user_input = user_input.lower()
16
- for keyword in kratom_contexts:
17
- if keyword in user_input:
18
- return kratom_contexts[keyword]
19
- return "🧪 I couldn’t find matching context. Try asking about specific body systems like 'urination', 'skin', or 'behavior'."
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # ✅ Build Gradio interface
22
  iface = gr.Interface(
23
  fn=answer_question,
24
- inputs=gr.Textbox(label="Ask about Kratom effects (English only for now)"),
25
- outputs=gr.Textbox(label="Medical Tutor Response"),
26
- title="Kratom Context Tutor",
27
- description="Keyword-driven answers about Kratom effects. Built for health education—expandable with Thai, visuals, or AI integration."
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()