fatma812 commited on
Commit
4ef8bcb
·
verified ·
1 Parent(s): 51651ca

Update chatbot_updated.py

Browse files
Files changed (1) hide show
  1. chatbot_updated.py +61 -162
chatbot_updated.py CHANGED
@@ -1,162 +1,61 @@
1
- # ==============================
2
- # chatbot_gradio_fixed.py
3
- # ==============================
4
- import os
5
- import gradio as gr
6
- import json
7
- import numpy as np
8
- from deep_translator import GoogleTranslator
9
- from ultralytics import YOLO
10
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
11
-
12
- # ==============================
13
- # 🔹 Paths (نسبيّة مع مكان المشروع)
14
- # ==============================
15
- BASE_DIR = os.path.dirname(__file__) # مكان هذا الملف
16
- yolo_model_path = os.path.join(BASE_DIR, "best_egypt.pt") # ضع موديل YOLO هنا
17
- artifacts_json_path = os.path.join(BASE_DIR, "artifacts.json") # ملف JSON للأثار
18
-
19
- # ==============================
20
- # Load YOLO Model
21
- # ==============================
22
- yolo_model = YOLO(yolo_model_path)
23
-
24
- # ==============================
25
- # Load FLAN-T5 Model
26
- # ==============================
27
- model_name = "google/flan-t5-base"
28
- tokenizer = AutoTokenizer.from_pretrained(model_name)
29
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
30
-
31
- # ==============================
32
- # Load Artifacts JSON
33
- # ==============================
34
- with open(artifacts_json_path, "r", encoding="utf-8") as f:
35
- data = json.load(f)
36
-
37
- # ==============================
38
- # Helper Functions
39
- # ==============================
40
- def is_arabic(text):
41
- return any('\u0600' <= ch <= '\u06FF' for ch in text)
42
-
43
- def translate_to_en(text):
44
- return GoogleTranslator(source='auto', target='en').translate(text)
45
-
46
- def translate_to_ar(text):
47
- return GoogleTranslator(source='auto', target='ar').translate(text)
48
-
49
- def detect_artifact(image_path):
50
- results = yolo_model(image_path)
51
- result = results[0]
52
-
53
- if result.boxes is None or len(result.boxes) == 0:
54
- return None
55
-
56
- scores = result.boxes.conf.cpu().numpy()
57
- best_index = np.argmax(scores)
58
-
59
- class_id = int(result.boxes.cls[best_index])
60
- return result.names[class_id]
61
-
62
- def get_artifact(artifact_name):
63
- artifact_name = artifact_name.lower().strip()
64
-
65
- # 1️⃣ Exact match
66
- for doc in data:
67
- if artifact_name == doc['name'].lower().strip():
68
- return doc
69
-
70
- # 2️⃣ Partial match
71
- for doc in data:
72
- if artifact_name in doc['name'].lower():
73
- return doc
74
-
75
- # 3️⃣ Match keywords
76
- for doc in data:
77
- if 'keywords' in doc:
78
- for k in doc['keywords']:
79
- if k.lower() in artifact_name:
80
- return doc
81
- return None
82
-
83
- # ==============================
84
- # ✅ Chatbot Function
85
- # ==============================
86
- def chatbot_gradio(image, question):
87
- user_lang = "ar" if is_arabic(question) else "en"
88
- question_en = translate_to_en(question) if user_lang == "ar" else question
89
-
90
- artifact_name = detect_artifact(image.name)
91
- if artifact_name is None:
92
- return "لم يتم التعرف على الأثر في الصورة" if user_lang == "ar" else "Artifact not detected."
93
-
94
- artifact = get_artifact(artifact_name)
95
- if artifact is None:
96
- return "لا توجد بيانات عن هذا الأثر" if user_lang == "ar" else "No data found for this artifact."
97
-
98
- q = question_en.lower()
99
- if any(word in q for word in ["who built", "creator", "made by"]):
100
- answer = artifact['creator']
101
- elif any(word in q for word in ["where", "location", "found"]):
102
- answer = artifact['location_found']
103
- elif any(word in q for word in ["material", "made of"]):
104
- answer = artifact['material']
105
- elif any(word in q for word in ["describe", "appearance", "look like", "shape", "what do you know about it", "tell me about it"]):
106
- answer = artifact['description'] + " " + artifact['importance']
107
- else:
108
- answer = artifact['description'] + " " + artifact['importance']
109
-
110
- # إعداد الـ Prompt للموديل
111
- instruction = "Provide a detailed and well-explained answer using ONLY the artifact information."
112
- prompt = f"""
113
- You are an expert assistant specialized in Egyptian artifacts.
114
- IMPORTANT RULES:
115
- - Use ONLY the artifact data provided below.
116
- - Do NOT use any external knowledge.
117
- - Do NOT guess or hallucinate.
118
- - If the answer cannot be found in the data, respond exactly:
119
- "I don't know based on the given artifact data."
120
- - Always provide detailed and well-explained answers.
121
- Instruction:
122
- {instruction}
123
- Artifact Information:
124
- Name: {artifact['name']}
125
- Creator: {artifact['creator']}
126
- Built Year: {artifact['built_year']}
127
- Type: {artifact['type']}
128
- Era: {artifact['era']}
129
- Material: {artifact['material']}
130
- Description: {artifact['description']}
131
- Importance: {artifact['importance']}
132
- Location Found: {artifact['location_found']}
133
- Current Location: {artifact['current_location']}
134
- Question:
135
- {question_en}
136
- Answer:
137
- """
138
-
139
- inputs = tokenizer(prompt, return_tensors="pt")
140
- outputs = model.generate(**inputs, max_new_tokens=400, do_sample=False)
141
- response_en = tokenizer.decode(outputs[0], skip_special_tokens=True)
142
-
143
- if "Answer:" in response_en:
144
- response_en = response_en.split("Answer:")[-1].strip()
145
-
146
- return translate_to_ar(response_en) if user_lang == "ar" else response_en
147
-
148
- # ==============================
149
- # ✅ Gradio Interface
150
- # ==============================
151
- iface = gr.Interface(
152
- fn=chatbot_gradio,
153
- inputs=[
154
- gr.Image(type="file", label="Upload Artifact Image"),
155
- gr.Textbox(lines=2, placeholder="Ask a question about the artifact...", label="Question")
156
- ],
157
- outputs=gr.Textbox(label="Answer"),
158
- title="Egyptian Artifacts Chatbot",
159
- description="Upload an image of an Egyptian artifact and ask any question about it."
160
- )
161
-
162
- iface.launch()
 
1
+ # ==============================
2
+ # 🔹 Main Entry Point
3
+ # ==============================
4
+ def chatbot_updated(question, image=None):
5
+ global LAST_ARTIFACT
6
+
7
+ if not question or question.strip() == "":
8
+ return "أهلاً بك! كيف يمكنني مساعدتك في التعرف على الآثار؟"
9
+
10
+ user_lang = "ar" if is_arabic(question) else "en"
11
+ question_en = translate_to_en(question) if user_lang == "ar" else question
12
+
13
+ artifact_name = None
14
+
15
+ # 1️⃣ التعرف من الصورة
16
+ if image is not None:
17
+ detected, _ = detect_artifact(image)
18
+ if detected and get_artifact(detected):
19
+ artifact_name = detected
20
+ LAST_ARTIFACT = detected
21
+
22
+ # 2️⃣ التعرف من النص
23
+ if artifact_name is None:
24
+ for doc in data:
25
+ if doc['name'].lower() in question_en.lower():
26
+ artifact_name = doc['name']
27
+ LAST_ARTIFACT = doc['name']
28
+ break
29
+
30
+ # 3️⃣ لو ما لقيناش أثر نهائي
31
+ if artifact_name is None or not get_artifact(artifact_name):
32
+ return "لا توجد معلومات عن هذا الأثر." if user_lang == "ar" else "No data found for this artifact."
33
+
34
+ artifact = get_artifact(artifact_name)
35
+
36
+ # معالجة الأسئلة المتعددة
37
+ raw_parts = split_questions(question_en)
38
+ if not raw_parts:
39
+ raw_parts = [question_en]
40
+
41
+ all_answers = []
42
+ for part in raw_parts:
43
+ # حل الضمائر it → اسم الأثر
44
+ if " it " in f" {part.lower()} ":
45
+ part = part.replace(" it ", f" {artifact_name} ")
46
+ answer = get_best_response(part, artifact)
47
+ if answer:
48
+ all_answers.append(answer)
49
+
50
+ # دمج الإجابات بجملة واحدة سلسة
51
+ if not all_answers:
52
+ return "لا يمكنني العثور على إجابة محددة."
53
+
54
+ final_answer_en = " ".join(all_answers)
55
+
56
+ # الترجمة النهائية
57
+ if user_lang == "ar":
58
+ ar_answer = translate_to_ar(final_answer_en)
59
+ return ar_answer.replace(" ", " ").strip()
60
+
61
+ return final_answer_en