Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,311 @@
|
|
| 1 |
-
|
| 2 |
from datasets import load_dataset
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
from difflib import SequenceMatcher
|
| 7 |
-
from groq import Groq
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 11 |
-
client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
|
| 12 |
|
| 13 |
-
#
|
| 14 |
WEBSITE_DATASET = [
|
| 15 |
-
{"category": "သတင်း", "name": "BBC မြန်မာ", "url": "https://www.bbc.com/burmese", "description": "နိုင်ငံတကာ
|
| 16 |
-
{"category": "သတင်း", "name": "
|
| 17 |
-
{"category": "
|
| 18 |
-
{"category": "
|
| 19 |
-
{"category": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
chat_pairs = []
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
print(f"
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
def search_websites(query):
|
| 36 |
-
query = query.lower()
|
| 37 |
-
return [site for site in WEBSITE_DATASET if query in site['name'].lower() or query in site['category']]
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
for
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
messages.append({"role": "user", "content": h_user})
|
| 77 |
-
messages.append({"role": "assistant", "content": h_bot})
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
gr.Markdown("Dataset + Web Search + Llama-3.1-8B Hybrid AI System")
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
demo.launch()
|
|
|
|
| 1 |
+
Import gradio as gr
|
| 2 |
from datasets import load_dataset
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
from difflib import SequenceMatcher
|
|
|
|
| 7 |
|
| 8 |
+
|
| 9 |
+
# Load GROQ API Key from secrets
|
| 10 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
|
|
|
| 11 |
|
| 12 |
+
# Myanmar websites database
|
| 13 |
WEBSITE_DATASET = [
|
| 14 |
+
{"category": "သတင်း", "name": "BBC မြန်မာ", "url": "https://www.bbc.com/burmese", "description": "နိုင်ငံတကာ သတင်းများကို မြန်မာလို ဖြေဆိုပါတယ်။"},
|
| 15 |
+
{"category": "သတင်း", "name": "Radio Free Asia", "url": "https://www.rfa.org/burmese/", "description": "RFA သည် မြန်မာပါဝင်ပပါတယ်။"},
|
| 16 |
+
{"category": "သတင်း", "name": "Democratic Voice of Burma", "url": "https://burmese.dvb.no/", "description": "DVB သည် မြန်မာသတင်း ဝက်ဘ်ဆိုင်း ဖြစ်ပါတယ်။"},
|
| 17 |
+
{"category": "သတင်း", "name": "Myanmar Times", "url": "https://myanmar-times.com/", "description": "Myanmar Times သည် မြန်မာနိုင်ငံရဲ့ သတင်းစာပါ။"},
|
| 18 |
+
{"category": "ပညာရေး", "name": "SEAsite Burmese", "url": "https://seasite.niu.edu/Burmese/", "description": "Northern Illinois University မှာရှိတဲ့ မြန်မာစာ၊ စာပေ နှင့် ယဉ်ကျေးမှု သင်တဲ့ ဌာန ဖြစ်ပါတယ်။"},
|
| 19 |
+
{"category": "ပညာရေး", "name": "Omniglot Burmese", "url": "https://www.omniglot.com/writing/burmese.htm", "description": "Omniglot သည် မြန်မာအက္ခရာနှင့် စာပေအောက်ငါးပါးသင်တဲ့ ဝက်ဘ်ဆိုင်း ဖြစ်ပါတယ်။"},
|
| 20 |
+
{"category": "ပညာရေး", "name": "BurmeseTutor", "url": "https://burmesetutor.com/", "description": "BurmeseTutor သည် မြန်မာစာ သင်ယူနည်း ပြည့်စုံသော ဝက်ဘ်ဆိုင်း ဖြစ်ပါတယ်။"},
|
| 21 |
+
{"category": "ယဉ်ကျေးမှု", "name": "Burmese Classic", "url": "https://burmeseclassic.com/", "description": "Burmese Classic သည် မြန်မာ ရုပ်ရှင်း၊ သီချင်း၊ မဂ်ဖိုလ်၊ နက္ခတ်များ ပါဝင်ပပါတယ်။"},
|
| 22 |
+
{"category": "ယဉ်ကျေးမှု", "name": "Buddhist Myanmar", "url": "https://www.buddhistmyanmar.org/", "description": "Buddhist Myanmar သည် ဗုဒ္ဓဘာသာ မြန်မာ ဝက်ဘ်ဆိုင်း ဖြစ်ပါတယ်။"},
|
| 23 |
+
{"category": "နည်းပညာ", "name": "Unicode Myanmar FAQ", "url": "https://www.unicode.org/faq/myanmar.html", "description": "Unicode Myanmar FAQ သည် မြန်မာ Unicode နဲ့ ပါတ်သတ်သည့် မေးလောက်များ ဖြေဆိုထားပါတယ်။"},
|
| 24 |
+
{"category": "ဆေးပညာ", "name": "WHO Myanmar", "url": "https://www.who.int/myanmar", "description": "WHO Myanmar သည် ကမ္ဘာ့ကျန်းမာရေးအဖွဲ့ မြန်မာဌာန ဖြစ်ပါတယ်။"},
|
| 25 |
+
{"category": "ဆေးပညာ", "name": "Myanmar Health Journal", "url": "https://www.myanmarhealthjournal.org/", "description": "Myanmar Health Journal သည် မြန်မာနိုင်ငံရဲ့ ကျန်းမာရေး သတင်းများ ပါဝင်ပပါတယ်။"}
|
| 26 |
]
|
| 27 |
|
| 28 |
+
# Load datasets from Hugging Face
|
|
|
|
| 29 |
try:
|
| 30 |
+
dataset = load_dataset("amkyawdev/AmkyawDev-Dataset")
|
| 31 |
+
train_data = dataset["train"]
|
| 32 |
+
all_tags = set()
|
| 33 |
+
for item in train_data:
|
| 34 |
+
if "tags" in item:
|
| 35 |
+
all_tags.add(item["tags"])
|
| 36 |
+
if "category" in item:
|
| 37 |
+
all_tags.add(item["category"])
|
| 38 |
+
label_names = list(all_tags) if all_tags else ["greeting", "coding", "translation", "conversation", "general", "math"]
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"Error loading dataset: {e}")
|
| 41 |
+
label_names = ["greeting", "coding", "translation", "conversation", "general", "math"]
|
| 42 |
|
| 43 |
+
LABELS = label_names
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Load chat data
|
| 46 |
+
chat_pairs = []
|
| 47 |
+
chat_tags = []
|
| 48 |
+
|
| 49 |
+
def load_chat_data():
|
| 50 |
+
global chat_pairs, chat_tags
|
| 51 |
+
chat_pairs = []
|
| 52 |
+
chat_tags = []
|
| 53 |
try:
|
| 54 |
+
for split in ["train.jsonl", "validation.jsonl", "test.jsonl"]:
|
| 55 |
+
chat_dataset = load_dataset("amkyawdev/AmkyawDev-Dataset", data_files=split)
|
| 56 |
+
chat_data = list(chat_dataset["train"])
|
| 57 |
+
for item in chat_data:
|
| 58 |
+
if "input" in item and "output" in item:
|
| 59 |
+
user_msg = item.get("input", "")
|
| 60 |
+
assistant_msg = item.get("output", "")
|
| 61 |
+
if user_msg and assistant_msg:
|
| 62 |
+
chat_pairs.append((user_msg, assistant_msg))
|
| 63 |
+
chat_tags.append(item.get("category", "other"))
|
| 64 |
+
print(f"Loaded {len(chat_pairs)} chat pairs from dataset")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Error loading chat data: {e}")
|
| 67 |
+
|
| 68 |
+
load_chat_data()
|
| 69 |
+
|
| 70 |
+
# Website search function
|
| 71 |
+
def search_websites(query):
|
| 72 |
+
results = []
|
| 73 |
+
query_lower = query.lower()
|
| 74 |
+
|
| 75 |
+
for site in WEBSITE_DATASET:
|
| 76 |
+
if (query_lower in site["category"].lower() or
|
| 77 |
+
query_lower in site["name"].lower() or
|
| 78 |
+
query_lower in site["description"].lower()):
|
| 79 |
+
results.append(site)
|
| 80 |
+
|
| 81 |
+
if not results:
|
| 82 |
+
keywords = query_lower.split()
|
| 83 |
+
for site in WEBSITE_DATASET:
|
| 84 |
+
for keyword in keywords:
|
| 85 |
+
if (keyword in site["category"].lower() or
|
| 86 |
+
keyword in site["name"].lower()):
|
| 87 |
+
if site not in results:
|
| 88 |
+
results.append(site)
|
| 89 |
+
|
| 90 |
+
return results
|
| 91 |
|
| 92 |
+
def fetch_website_content(url):
|
| 93 |
+
"""Fetch content from a website"""
|
| 94 |
+
try:
|
| 95 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 96 |
+
response = requests.get(url, headers=headers, timeout=15)
|
| 97 |
+
|
| 98 |
+
if response.status_code == 200:
|
| 99 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 100 |
|
| 101 |
+
for script in soup(["script", "style", "nav", "footer", "header"]):
|
| 102 |
+
script.decompose()
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
title = soup.title.string if soup.title else "ခေါင်းစဉ်မရှိပါ။"
|
| 105 |
+
text = soup.get_text()
|
| 106 |
+
lines = (line.strip() for line in text.splitlines())
|
| 107 |
+
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
|
| 108 |
+
text = ' '.join(chunk for chunk in chunks if chunk)
|
| 109 |
|
| 110 |
+
return f"📌 {title}\n\n{text[:3000]}"
|
| 111 |
+
|
| 112 |
+
return "ဝက်ဘ်ဆိုင်းကို ဖတ်လို့မရပါ။"
|
| 113 |
+
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return f"အမှားဖြစ်ပါတယ်: {str(e)}"
|
| 116 |
+
|
| 117 |
+
def get_website_info(query):
|
| 118 |
+
results = search_websites(query)
|
| 119 |
+
|
| 120 |
+
if not results:
|
| 121 |
+
return None
|
| 122 |
+
|
| 123 |
+
response = "📱 ရှာဖွေလိုက်တဲ့ ဝက်ဘ်ဆိုင်းများ:\n\n"
|
|
|
|
| 124 |
|
| 125 |
+
for i, site in enumerate(results, 1):
|
| 126 |
+
response += f"{i}. 🌐 {site['name']}\n"
|
| 127 |
+
response += f" 📂 အမျိုးအစား: {site['category']}\n"
|
| 128 |
+
response += f" 🔗 URL: {site['url']}\n"
|
| 129 |
+
response += f" 📝 ဖော်ပြချက်: {site['description']}\n\n"
|
| 130 |
|
| 131 |
+
# Fetch content from first website
|
| 132 |
+
if results:
|
| 133 |
+
response += "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
|
| 134 |
+
response += f"📖 {results[0]['name']} မှ ဖတ်လိုက်တဲ့ အရာ:\n\n"
|
| 135 |
+
content = fetch_website_content(results[0]['url'])
|
| 136 |
+
response += content
|
| 137 |
+
|
| 138 |
+
return response
|
| 139 |
+
|
| 140 |
+
# System prompt for Groq API
|
| 141 |
+
SYSTEM_PROMPT = """သင်သည် AmkyawDev မှ ဖန်တီးထားသော မြန်မာ AI Assistant တစ်ဦးဖြစ်ပါသည်။ အောက်ပါ လမ်းညွှန်ချက်များအတိုင်း တိကျစွာ အလုပ်လုပ်ပေးပါ။
|
| 142 |
+
|
| 143 |
+
၁။ နုတ်ဆက်ခြင်းနှင့် လေသံ (Identity & Tone):
|
| 144 |
+
- ယဉ်ကျေးပျူငှာပြီး နွေးထွေးဖော်ရွေသော စကားပြော လေသံကို သုံးပါ။
|
| 145 |
+
- 'AmkyawDev' ၏ ကိုယ်စားလှယ်အဖြစ် မိမိကိုယ်ကို သိမှတ်ပြီး အကူအညီပေးပါ။
|
| 146 |
+
|
| 147 |
+
၂။ အချက်အလက် ရှာဖွေခြင်း (Data Retrieval):
|
| 148 |
+
- အကယ်၍ ဝက်ဆိုက် (သို့မဟုတ်) Dataset အချက်အလက်များ ပါဝင်လာပါက ထိုအချက်အလက်များကို ဦးစားပေး ဖတ်ရှုပြီး ဖြေကြားပါ။
|
| 149 |
+
- Dataset တွင် မပါရှိသော အကြောင်းအရာများကို မေးမြန်းပါက သင်၏ ကိုယ်ပိ���င်အသိဉာဏ် (General Knowledge) ကို အသုံးပြု၍ ဖြေကြားပေးပါ၊ သို့သော် Dataset တွင် မပါဝင်ကြောင်းကို သိမ်မွေ့စွာ အသိပေးပါ။
|
| 150 |
+
|
| 151 |
+
၃။ ဘာသာပြန်ခြင်း (Translation):
|
| 152 |
+
- အခြားဘာသာစကားမှ မြန်မာဘာသာသို့ ပြန်ဆိုရာတွင် တိုက်ရိုက်အဓိပ္ပာယ်ထက် မြန်မာစကားအသုံးအနှုန်း ဆီလျော်မှုရှိစေရန် (Contextual Translation) ကို အလေးထားပါ။
|
| 153 |
+
|
| 154 |
+
၄။ စာရေးသားမှု စံနှုန်း (Formatting & Spelling):
|
| 155 |
+
- မြန်မာစာလုံးပေါင်း သတ်ပုံကို အမှန်ကန်ဆုံးဖြစ်အောင် ဂရုစိုက်ပါ။
|
| 156 |
+
- အဖြေများကို ဖတ်ရလွယ်ကူစေရန် Bullet points သို့မဟုတ် နံပါတ်စဉ်များဖြင့် စနစ်တကျ စီစဉ်ပေးပါ။
|
| 157 |
+
|
| 158 |
+
၅။ ကန့်သတ်ချက် (Constraint):
|
| 159 |
+
- မေးခွန်းနှင့် မသက်ဆိုင်သော အပိုစာသားများကို ရှောင်ကြဉ်ပြီး မေးခွန်း၏ လိုရင်းကိုသာ အဓိကထား ဖြေကြားပါ။"""
|
| 160 |
+
|
| 161 |
+
# GROQ API integration (direct requests)
|
| 162 |
+
def call_groq(prompt):
|
| 163 |
+
"""Call GROQ API directly"""
|
| 164 |
+
if not GROQ_API_KEY:
|
| 165 |
+
return None
|
| 166 |
+
|
| 167 |
+
try:
|
| 168 |
+
import requests
|
| 169 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 170 |
+
headers = {
|
| 171 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 172 |
+
"Content-Type": "application/json"
|
| 173 |
+
}
|
| 174 |
+
data = {
|
| 175 |
+
"model": "llama-3.1-8b-instant",
|
| 176 |
+
"messages": [
|
| 177 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 178 |
+
{"role": "user", "content": prompt}
|
| 179 |
+
],
|
| 180 |
+
"temperature": 0.7,
|
| 181 |
+
"max_tokens": 500
|
| 182 |
+
}
|
| 183 |
+
response = requests.post(url, headers=headers, json=data, timeout=30)
|
| 184 |
+
if response.status_code == 200:
|
| 185 |
+
result = response.json()
|
| 186 |
+
return result["choices"][0]["message"]["content"]
|
| 187 |
+
else:
|
| 188 |
+
print(f"GROQ API error: {response.status_code}")
|
| 189 |
+
return None
|
| 190 |
+
except Exception as e:
|
| 191 |
+
print(f"Error calling GROQ: {e}")
|
| 192 |
+
return None
|
| 193 |
+
|
| 194 |
+
# Fallback responses
|
| 195 |
+
fallback_responses = {
|
| 196 |
+
"သတင်း": "သတင်းသည်သင်တန်းစာသင်ပါး။ နိုင်ငံတော်သမိုင်းနဲ့ရေးသားပါ။",
|
| 197 |
+
"ကဗျာ": "မြန်မာကဗျာ ရေးလိုက်ပါ။ အိပ်မက်မှာ ပါ။ အကျယ်ပြန့်စွာ ရှိပါ။",
|
| 198 |
+
"ဆေး": "ဆေးပပါး ဘာသာပါ။ ဆရာဝန်နဲ့ သွားကြည့်ပါ။",
|
| 199 |
+
"ပညာ": "ပညာ သင်လိုက်ပါ။ သင်ယူခွင့် ပါ။ ကျောင်းသားများ ပါ။",
|
| 200 |
+
"ဘာသာ": "ဘာသာစာသင်၊ ပါ။ သတင်း ပါ။",
|
| 201 |
+
"စကား": "စကား ခွဲပါ။ ဖြေချက် ပါ။",
|
| 202 |
+
"မိတ်ဆွေ": "မိတ်ဆွေများနဲ့ စကား ပြောပါ။",
|
| 203 |
+
"နေကောင်း": "နေကောင်းပါတယ်ခင်ဗျာ။ ဘာများကူညီပေးရမလဲ။",
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
def chat_response(user_input):
|
| 207 |
+
"""Chat response using chat pairs (Dataset), then GROQ API, then fallback"""
|
| 208 |
+
if not user_input or len(user_input.strip()) == 0:
|
| 209 |
+
return "ပါသည်ကို ရေးပါ။"
|
| 210 |
+
|
| 211 |
+
user_input_lower = user_input.lower()
|
| 212 |
+
|
| 213 |
+
# Check if user is asking about websites
|
| 214 |
+
website_keywords = ["website", "ဝက်ဘ်ဆိုင်း", "web site", "url", "link", "search", "find", "ရှာ", "ရှာပ", "ဖတ်"]
|
| 215 |
+
is_website_query = any(keyword in user_input_lower for keyword in website_keywords)
|
| 216 |
+
|
| 217 |
+
if is_website_query:
|
| 218 |
+
website_info = get_website_info(user_input)
|
| 219 |
+
if website_info:
|
| 220 |
+
return website_info
|
| 221 |
+
|
| 222 |
+
# ၁။ Dataset မှာ ရှာပါ။
|
| 223 |
+
best_match = None
|
| 224 |
+
best_score = 0
|
| 225 |
+
threshold = 0.3
|
| 226 |
+
|
| 227 |
+
for pattern, response in chat_pairs:
|
| 228 |
+
score = SequenceMatcher(None, user_input, pattern).ratio()
|
| 229 |
+
if score > best_score:
|
| 230 |
+
best_score = score
|
| 231 |
+
best_match = response
|
| 232 |
+
|
| 233 |
+
if best_match and best_score >= threshold:
|
| 234 |
+
return best_match
|
| 235 |
+
|
| 236 |
+
# ၂။ GROQ API သုံးပါ။
|
| 237 |
+
if GROQ_API_KEY:
|
| 238 |
+
groq_response = call_groq(user_input)
|
| 239 |
+
if groq_response:
|
| 240 |
+
return groq_response
|
| 241 |
+
|
| 242 |
+
# ၃။ Fallback
|
| 243 |
+
for key, response in fallback_responses.items():
|
| 244 |
+
if key in user_input_lower:
|
| 245 |
+
return response
|
| 246 |
+
|
| 247 |
+
return "နောက်မှာ ပြန်လာပါ။"
|
| 248 |
+
|
| 249 |
+
def simple_classify(text):
|
| 250 |
+
text_lower = text.lower()
|
| 251 |
+
keywords = {
|
| 252 |
+
"greeting": ["နေကောင်း", "ဟောက်", "မင်္ဂလာ", "မှား", "ဟိုင်း"],
|
| 253 |
+
"coding": ["python", "javascript", "html", "css", "java", "code", "ကုဒ်"],
|
| 254 |
+
"translation": ["ပြန်", "ဘာသာပြန်"],
|
| 255 |
+
"conversation": ["စကား", "ပြော"],
|
| 256 |
+
"general": ["သတင်း", "ဘာလဲ", "ရှိလဲ"],
|
| 257 |
+
"math": ["ပါ", "နှုန်း", "သင်္ချာ"]
|
| 258 |
+
}
|
| 259 |
+
scores = {}
|
| 260 |
+
for label, kws in keywords.items():
|
| 261 |
+
score = sum(1 for kw in kws if kw in text_lower)
|
| 262 |
+
scores[label] = score
|
| 263 |
+
predicted = max(scores, key=scores.get) if max(scores.values()) > 0 else "other"
|
| 264 |
+
return predicted
|
| 265 |
+
|
| 266 |
+
def predict(text):
|
| 267 |
+
if not text or len(text.strip()) == 0:
|
| 268 |
+
return {"error": "Please enter some text"}
|
| 269 |
+
label = simple_classify(text)
|
| 270 |
+
return {"text": text[:100] + "..." if len(text) > 100 else text, "label": label, "confidence": 0.85}
|
| 271 |
+
|
| 272 |
+
# Gradio UI
|
| 273 |
+
with gr.Blocks(title="AmkyawDev NLP") as demo:
|
| 274 |
+
gr.Markdown("## 🇲🇲 AmkyawDev NLP - Myanmar Language AI")
|
| 275 |
+
gr.Markdown(f"### Categories: {', '.join(LABELS)}")
|
| 276 |
+
gr.Markdown(f"### Chat pairs: {len(chat_pairs)} | Websites: {len(WEBSITE_DATASET)}")
|
| 277 |
+
|
| 278 |
+
if GROQ_API_KEY:
|
| 279 |
+
gr.Markdown("### 🔥 Groq AI - Enabled (Llama 3.1 8B)")
|
| 280 |
+
else:
|
| 281 |
+
gr.Markdown("### ⚠️ Groq API Key not set")
|
| 282 |
+
|
| 283 |
+
with gr.Tab("📊 Classification"):
|
| 284 |
+
input_text = gr.Textbox(label="Enter Myanmar Text", lines=5)
|
| 285 |
+
output = gr.JSON(label="Prediction Result")
|
| 286 |
+
submit_btn = gr.Button("Predict")
|
| 287 |
+
submit_btn.click(predict, input_text, output)
|
| 288 |
+
|
| 289 |
+
with gr.Tab("💬 Chat"):
|
| 290 |
+
gr.Markdown("### 💬 Chat - ဝက်ဘ်ဆိုင်းအကြောင်း မေးလိုက်ပါက ရှာပြီး ဖတ်ပါတယ်။")
|
| 291 |
+
chatbot = gr.Chatbot()
|
| 292 |
+
msg = gr.Textbox(label="Your Message", lines=2)
|
| 293 |
+
send_btn = gr.Button("Send")
|
| 294 |
+
clear_btn = gr.Button("Clear")
|
| 295 |
+
|
| 296 |
+
def respond(message, history):
|
| 297 |
+
if not message:
|
| 298 |
+
return "", history or []
|
| 299 |
+
response = chat_response(message)
|
| 300 |
+
if history is None:
|
| 301 |
+
history = []
|
| 302 |
+
history.append({"role": "user", "content": [{"text": message, "type": "text"}]})
|
| 303 |
+
history.append({"role": "assistant", "content": [{"text": response, "type": "text"}]})
|
| 304 |
+
return "", history
|
| 305 |
+
|
| 306 |
+
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 307 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 308 |
+
clear_btn.click(lambda: [], None, chatbot)
|
| 309 |
|
| 310 |
if __name__ == "__main__":
|
| 311 |
demo.launch()
|