| import json |
| import gradio as gr |
| from difflib import SequenceMatcher |
| from pathlib import Path |
|
|
|
|
| |
| 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 (အတည်ပြု + အဖြေထုတ်) |
| """ |
| |
| |
| print(f"[Step 1 - Perception] User: {user_message}") |
| |
| |
| best_match = None |
| best_score = 0 |
| |
| for resp in responses: |
| score = 0 |
| for kw in resp["keywords"]: |
| if kw in user_message: |
| |
| score += SequenceMatcher(None, user_message, kw).ratio() |
| |
| |
| if resp["keywords"]: |
| score = score / len(resp["keywords"]) |
| |
| if score > best_score: |
| best_score = score |
| best_match = resp |
| |
| |
| if best_match and best_score > 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 = "ကျေးဇူးပြု၍ ပိုရှင်းလင်းအောင် မေးပေးပါ။" |
| |
| |
| 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 |
|
|
|
|
| |
| demo = gr.ChatInterface( |
| fn=three_step_reasoning, |
| title="🧠 Amkyaw Core L3 - ၃ ဆင့်တွေးခေါ်မှု System", |
| description="3-Step Reasoning System with keyword matching", |
| theme=gr.themes.Soft(), |
| ) |
|
|
| |
| demo.launch() |