Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import json | |
| import difflib | |
| from deep_translator import GoogleTranslator | |
| # ========================= | |
| # تحميل البيانات | |
| # ========================= | |
| with open("artifacts (3).json", "r", encoding="utf-8") as f: | |
| artifacts = json.load(f) | |
| # ========================= | |
| # تحديد اللغة | |
| # ========================= | |
| def is_arabic(text): | |
| for ch in text: | |
| if '\u0600' <= ch <= '\u06FF': | |
| return True | |
| return False | |
| def translate_to_en(text): | |
| return GoogleTranslator(source='auto', target='en').translate(text) | |
| def translate_to_ar(text): | |
| return GoogleTranslator(source='auto', target='ar').translate(text) | |
| # ========================= | |
| # البحث | |
| # ========================= | |
| def search_artifact(user_input): | |
| user_input = user_input.lower() | |
| best_match = None | |
| best_score = 0 | |
| for artifact in artifacts: | |
| for keyword in artifact["keywords"]: | |
| score = difflib.SequenceMatcher(None, user_input, keyword.lower()).ratio() | |
| if score > best_score: | |
| best_score = score | |
| best_match = artifact | |
| return best_match | |
| # ========================= | |
| # الرد | |
| # ========================= | |
| def chatbot(user_input): | |
| is_ar = is_arabic(user_input) | |
| if is_ar: | |
| query = translate_to_en(user_input) | |
| else: | |
| query = user_input | |
| result = search_artifact(query) | |
| if result: | |
| response = result["description"] | |
| if is_ar: | |
| response = translate_to_ar(response) | |
| return response | |
| else: | |
| return "مش لاقي حاجة 😅" if is_ar else "Not found 😅" | |
| # ========================= | |
| # واجهة Gradio | |
| # ========================= | |
| demo = gr.Interface( | |
| fn=chatbot, | |
| inputs=gr.Textbox(label="Ask about any artifact / اسأل عن أي أثر"), | |
| outputs="text", | |
| title="🏺 Egyptian Museum Chatbot" | |
| ) | |
| demo.launch() |