File size: 1,994 Bytes
b26d7a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
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()