Spaces:
Runtime error
Runtime error
File size: 2,962 Bytes
fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 5e80ca8 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 fc53b72 560e8f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 |
import gradio as gr
import yaml
import sqlite3
import difflib
import re
# ✅ Emotion detection (সাধারণ রুল-বেসড)
def detect_emotion(text):
text = text.lower()
if any(word in text for word in ["ভালো", "সুন্দর", "ধন্যবাদ", "আনন্দ"]):
return "আনন্দ"
elif any(word in text for word in ["দুঃখ", "কষ্ট", "হারিয়েছি", "চিন্তা"]):
return "দুঃখ"
elif any(word in text for word in ["রাগ", "বিরক্ত", "না", "খারাপ"]):
return "রাগ"
elif any(word in text for word in ["কি", "কেন", "কিভাবে", "বলো"]):
return "কৌতূহল"
else:
return "নিরপেক্ষ"
# ✅ YAML থেকে QA লোড করুন
def load_qa_yaml(path="bonolota_ai_dataset/qa.yaml"):
with open(path, "r", encoding="utf-8") as f:
return yaml.safe_load(f)
qa_data = load_qa_yaml()
# ✅ প্রশ্নের fuzzy match খুঁজে বের করুন
def fuzzy_match(query, qa_data, threshold=0.6):
questions = [item["question"] for item in qa_data]
matches = difflib.get_close_matches(query, questions, n=1, cutoff=threshold)
if matches:
for item in qa_data:
if item["question"] == matches[0]:
return item
return None
def load_qa_yaml(path="bonolota_ai_dataset/qa.yaml"):
try:
with open(path, "r", encoding="utf-8") as f:
return yaml.safe_load(f)
except FileNotFoundError:
print("⚠️ QA YAML ফাইল পাওয়া যায়নি।")
return []
# ✅ চ্যাট রেসপন্স ফাংশন
def respond(message, history):
emotion = detect_emotion(message)
matched = fuzzy_match(message, qa_data)
if matched:
response = f"📖 উত্তর: {matched['answer']}\n😌 আবেগ: {matched['emotion']}"
if "voice_path" in matched:
response += f"\n🔊 ভয়েস: {matched['voice_path']}"
if "photo_path" in matched:
response += f"\n🖼️ ছবি: {matched['photo_path']}"
else:
response = f"😔 দুঃখিত, আমি এই প্রশ্নের উত্তর খুঁজে পেলাম না।\n🧠 আবেগ শনাক্ত: {emotion}"
history.append((message, response))
return "", history
# ✅ Gradio UI
chatbot = gr.ChatInterface(
respond,
title="🌿 Bonolota_AI (লোকাল বাংলা চ্যাটবট)",
theme="soft",
examples=["তোমার নাম কী?", "বাংলাদেশের রাজধানী কী?", "আমি খুব দুঃখিত", "তুমি কিভাবে কাজ করো?"],
cache_examples=False,
)
# ✅ Launch
if __name__ == "__main__":
chatbot.launch() |