fatma812 commited on
Commit
b26d7a2
·
verified ·
1 Parent(s): ba721cc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import difflib
4
+ from deep_translator import GoogleTranslator
5
+
6
+ # =========================
7
+ # تحميل البيانات
8
+ # =========================
9
+ with open("artifacts (3).json", "r", encoding="utf-8") as f:
10
+ artifacts = json.load(f)
11
+
12
+ # =========================
13
+ # تحديد اللغة
14
+ # =========================
15
+ def is_arabic(text):
16
+ for ch in text:
17
+ if '\u0600' <= ch <= '\u06FF':
18
+ return True
19
+ return False
20
+
21
+ def translate_to_en(text):
22
+ return GoogleTranslator(source='auto', target='en').translate(text)
23
+
24
+ def translate_to_ar(text):
25
+ return GoogleTranslator(source='auto', target='ar').translate(text)
26
+
27
+ # =========================
28
+ # البحث
29
+ # =========================
30
+ def search_artifact(user_input):
31
+ user_input = user_input.lower()
32
+
33
+ best_match = None
34
+ best_score = 0
35
+
36
+ for artifact in artifacts:
37
+ for keyword in artifact["keywords"]:
38
+ score = difflib.SequenceMatcher(None, user_input, keyword.lower()).ratio()
39
+ if score > best_score:
40
+ best_score = score
41
+ best_match = artifact
42
+
43
+ return best_match
44
+
45
+ # =========================
46
+ # الرد
47
+ # =========================
48
+ def chatbot(user_input):
49
+ is_ar = is_arabic(user_input)
50
+
51
+ if is_ar:
52
+ query = translate_to_en(user_input)
53
+ else:
54
+ query = user_input
55
+
56
+ result = search_artifact(query)
57
+
58
+ if result:
59
+ response = result["description"]
60
+
61
+ if is_ar:
62
+ response = translate_to_ar(response)
63
+
64
+ return response
65
+ else:
66
+ return "مش لاقي حاجة 😅" if is_ar else "Not found 😅"
67
+
68
+ # =========================
69
+ # واجهة Gradio
70
+ # =========================
71
+ demo = gr.Interface(
72
+ fn=chatbot,
73
+ inputs=gr.Textbox(label="Ask about any artifact / اسأل عن أي أثر"),
74
+ outputs="text",
75
+ title="🏺 Egyptian Museum Chatbot"
76
+ )
77
+
78
+ demo.launch()