Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,311 +1,116 @@
|
|
| 1 |
-
|
| 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 |
-
#
|
| 13 |
WEBSITE_DATASET = [
|
| 14 |
-
{"category": "သတင်း", "name": "BBC မြန်မာ", "url": "https://www.bbc.com/burmese", "description": "နိုင်ငံတကာ သတင်းများ
|
| 15 |
-
{"category": "သတင်း", "name": "
|
| 16 |
-
{"category": "
|
| 17 |
-
{"category": "
|
| 18 |
-
{"category": "
|
| 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 |
-
#
|
|
|
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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"
|
| 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 |
-
#
|
| 71 |
def search_websites(query):
|
| 72 |
-
|
| 73 |
-
|
| 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
|
| 93 |
-
"""Fetch content from a website"""
|
| 94 |
try:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
soup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 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 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 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 |
-
|
| 279 |
-
gr.
|
| 280 |
-
|
| 281 |
-
gr.Markdown("### ⚠️ Groq API Key not set")
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 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()
|
|
|
|
|
|
| 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 |
+
from groq import Groq
|
| 8 |
|
| 9 |
+
# API Key ကို Hugging Face Settings -> Secrets ထဲမှာ GROQ_API_KEY ဆိုပြီး ထည့်ထားရပါမယ်
|
|
|
|
| 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 |
+
# --- ၁။ Website Database ---
|
| 14 |
WEBSITE_DATASET = [
|
| 15 |
+
{"category": "သတင်း", "name": "BBC မြန်မာ", "url": "https://www.bbc.com/burmese", "description": "နိုင်ငံတကာနှင့် ပြည်တွင်းသတင်းများ။"},
|
| 16 |
+
{"category": "သတင်း", "name": "DVB", "url": "https://burmese.dvb.no/", "description": "ဒီဗွီဘီ မြန်မာသတင်းများ။"},
|
| 17 |
+
{"category": "ပညာရေး", "name": "BurmeseTutor", "url": "https://burmesetutor.com/", "description": "မြန်မာစာ သင်ယူနည်း လေ့လာရန်။"},
|
| 18 |
+
{"category": "နည်းပညာ", "name": "Unicode Myanmar", "url": "https://www.unicode.org/faq/myanmar.html", "description": "မြန်မာ ယူနီကုဒ် အကြောင်း။"},
|
| 19 |
+
{"category": "ကျန်းမာရေး", "name": "WHO Myanmar", "url": "https://www.who.int/myanmar", "description": "ကမ္ဘာ့ကျန်းမာရေးအဖွဲ့ မြန်မာဌာန။"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
+
# --- ၂။ Dataset Loading ---
|
| 23 |
+
chat_pairs = []
|
| 24 |
try:
|
| 25 |
+
# သင့် Dataset မှ Chat Pairs များကို ဆွဲယူခြင်း
|
| 26 |
+
dataset = load_dataset("amkyawdev/AmkyawDev-Dataset", split="train", trust_remote_code=True)
|
| 27 |
+
for item in dataset:
|
| 28 |
+
if "input" in item and "output" in item:
|
| 29 |
+
chat_pairs.append((item["input"], item["output"]))
|
| 30 |
+
print(f"Loaded {len(chat_pairs)} pairs from dataset.")
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"Dataset loading error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# --- ၃။ Helper Functions (Search & Scrape) ---
|
| 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 |
+
def fetch_content(url):
|
|
|
|
| 40 |
try:
|
| 41 |
+
res = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0"})
|
| 42 |
+
if res.status_code == 200:
|
| 43 |
+
soup = BeautifulSoup(res.text, 'html.parser')
|
| 44 |
+
for s in soup(["script", "style"]): s.decompose()
|
| 45 |
+
return soup.get_text()[:2000] # စာလုံးရေ ၂၀၀၀ ခန့်သာ ယူမည်
|
| 46 |
+
return "ဝက်ဘ်ဆိုက်ကို ဖတ်၍မရပါ။"
|
| 47 |
+
except: return "ချိတ်ဆက်မှု အမှားအယွင်းရှိပါသည်။"
|
| 48 |
+
|
| 49 |
+
# --- ၄။ Core AI Logic ---
|
| 50 |
+
def chat_with_ai(user_input, history):
|
| 51 |
+
if not user_input.strip():
|
| 52 |
+
return "စာသားတစ်ခုခု ရေးသားပေးပါ။"
|
| 53 |
+
|
| 54 |
+
# A. ဝက်ဘ်ဆိုက် ရှာဖွေမှု ရှိမရှိ စစ်ဆေးခြင်း
|
| 55 |
+
web_keywords = ["website", "ဝက်ဘ်ဆိုင်း", "ရှာပေး", "link"]
|
| 56 |
+
if any(k in user_input.lower() for k in web_keywords):
|
| 57 |
+
results = search_websites(user_input)
|
| 58 |
+
if results:
|
| 59 |
+
site = results[0]
|
| 60 |
+
content = fetch_content(site['url'])
|
| 61 |
+
return f"🌐 {site['name']} မှ အချက်အလက်များ:\n\n{content}"
|
| 62 |
+
|
| 63 |
+
# B. Dataset ထဲတွင် တိုက်ရိုက်တူညီမှု ရှိမရှိ စစ်ဆေးခြင်း (Similarity Threshold: 0.8)
|
| 64 |
+
for pattern, response in chat_pairs:
|
| 65 |
+
if SequenceMatcher(None, user_input, pattern).ratio() > 0.8:
|
| 66 |
+
return response
|
| 67 |
+
|
| 68 |
+
# C. Groq AI (Llama 3.1) ကို ခေါ်ယူခြင်း
|
| 69 |
+
if client:
|
| 70 |
+
try:
|
| 71 |
+
system_msg = "You are AmkyawDev NLP AI. Respond clearly in Myanmar Unicode. If you don't know, say so. Avoid nonsense."
|
| 72 |
+
messages = [{"role": "system", "content": system_msg}]
|
| 73 |
|
| 74 |
+
# History ထည့်သွင်းခြင်း (နောက်ဆုံး ၃ ကြိမ်စာ)
|
| 75 |
+
for h_user, h_bot in history[-3:]:
|
| 76 |
+
messages.append({"role": "user", "content": h_user})
|
| 77 |
+
messages.append({"role": "assistant", "content": h_bot})
|
| 78 |
|
| 79 |
+
messages.append({"role": "user", "content": user_input})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
chat_completion = client.chat.completions.create(
|
| 82 |
+
messages=messages,
|
| 83 |
+
model="llama-3.1-8b-instant",
|
| 84 |
+
temperature=0.6, # Logic ပိုမှန်စေရန် 0.6 ခန့်ထားပါ
|
| 85 |
+
)
|
| 86 |
+
return chat_completion.choices[0].message.content
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return f"AI Error: {str(e)}"
|
| 89 |
+
|
| 90 |
+
return "တောင်းပန်ပါတယ်၊ အဖြေရှာမတွေ့ပါဘူး။"
|
| 91 |
+
|
| 92 |
+
# --- ၅။ UI Design (Gradio Blocks) ---
|
| 93 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AmkyawDev NLP") as demo:
|
| 94 |
+
gr.Markdown("# 🇲🇲 AmkyawDev NLP Assistant")
|
| 95 |
+
gr.Markdown("Dataset + Web Search + Llama-3.1-8B Hybrid AI System")
|
| 96 |
|
| 97 |
+
chatbot = gr.Chatbot(label="Chat History", bubble_full_width=False)
|
| 98 |
+
msg = gr.Textbox(label="မေးခွန်းရေးရန်", placeholder="မင်္ဂလာပါ...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
with gr.Row():
|
| 101 |
+
submit = gr.Button("Send", variant="primary")
|
| 102 |
+
clear = gr.Button("Clear")
|
|
|
|
| 103 |
|
| 104 |
+
def user_msg(user_message, history):
|
| 105 |
+
# အဖြေထုတ်ပေးခြင်း
|
| 106 |
+
bot_response = chat_with_ai(user_message, history)
|
| 107 |
+
history.append((user_message, bot_response))
|
| 108 |
+
return "", history
|
| 109 |
|
| 110 |
+
msg.submit(user_msg, [msg, chatbot], [msg, chatbot])
|
| 111 |
+
submit.click(user_msg, [msg, chatbot], [msg, chatbot])
|
| 112 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
demo.launch()
|
| 116 |
+
|