import json import gradio as gr from difflib import SequenceMatcher from pathlib import Path # JSONL ဖိုင်ကို တစ်ခါဖတ်ပြီး memory ထဲထား DATA_PATH = Path(__file__).parent / "data" / "responses.jsonl" with open(DATA_PATH, "r", encoding="utf-8") as f: responses = [json.loads(line) for line in f] def three_step_reasoning(user_message, history): """ သုံးဆင့်တွေးခေါ်မှု System Step 1: Perception (အခြေအနေ ပိုင်းခြား) Step 2: Logical Sequence (ယုတ္တိအစဉ်) Step 3: Verification & Output (အတည်ပြု + အဖြေထုတ်) """ # ----- အဆင့် ၁: Perception (ဖမ်းယူခြင်း) ----- print(f"[Step 1 - Perception] User: {user_message}") # JSONL ထဲက အကောင်းဆုံး match ကို ရှာ best_match = None best_score = 0 for resp in responses: score = 0 for kw in resp["keywords"]: if kw in user_message: # keyword တစ်လုံးချင်းစီအတွက် အချိုးကျပုံ score += SequenceMatcher(None, user_message, kw).ratio() # စုစုပေါင်း score ကို keyword အရေအတွက်နဲ့ စား if resp["keywords"]: score = score / len(resp["keywords"]) if score > best_score: best_score = score best_match = resp # ----- အဆင့် ၂: Logical Sequence (ယုတ္တိအစဉ်တန်း) ----- if best_match and best_score > 0.3: # threshold 0.3 print(f"[Step 2 - Logical Sequence] Matched: {best_match['keywords']} (score: {best_score:.2f})") reasoning = best_match["step2"] else: print(f"[Step 2 - Logical Sequence] No good match found") reasoning = "ကျေးဇူးပြု၍ ပိုရှင်းလင်းအောင် မေးပေးပါ။" # ----- အဆင့် ၃: Verification & Output (အတည်ပြု + အဖြေထုတ်) ----- if best_match and best_score > 0.3: print(f"[Step 3 - Verification] {best_match['step3']}") print(f"[Step 3 - Output] {best_match['answer']}") final_output = f"""🧠 **အဆင့် ၁ - Perception (ခံယူချက်)** {best_match['step1']} ⚙️ **အဆင့် ၂ - Logical Sequence (ယုတ္တိအစဉ်)** {reasoning} ✅ **အဆင့် ၃ - Verification & Output (အတည်ပြုခြင်းနှင့် အဖြေ)** {best_match['step3']} --- ✨ **အဖြေ:** {best_match['answer']}""" else: final_output = f"""🧠 **အဆင့် ၁ - Perception (ခံယူချက်)** မေးခွန်းကို နားလည်ရန် ကြိုးစားနေပါတယ်။ ⚙️ **အဆင့် ၂ - Logical Sequence (ယုတ္တိအစဉ်)** {reasoning} ✅ **အဆင့် ၃ - Verification & Output (အတည်ပြုခြင်းနှင့် အဖြေ)** ကျေးဇူးပြု၍ ထပ်မေးပေးပါ။""" return final_output # Gradio Interface demo = gr.ChatInterface( fn=three_step_reasoning, title="🧠 Amkyaw Core L3 - ၃ ဆင့်တွေးခေါ်မှု System", description="3-Step Reasoning System with keyword matching", theme=gr.themes.Soft(), ) # Launch demo.launch()