Spaces:
Runtime error
Runtime error
| 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() |