Spaces:
Runtime error
Runtime error
Update chatbot_updated.py
Browse files- chatbot_updated.py +61 -162
chatbot_updated.py
CHANGED
|
@@ -1,162 +1,61 @@
|
|
| 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 |
-
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|