Rename chatbot2.py to app.py
Browse files- chatbot2.py → app.py +55 -55
chatbot2.py → app.py
RENAMED
|
@@ -1,55 +1,55 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from sentence_transformers import SentenceTransformer, util
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
# تحميل الموديل من Hugging Face
|
| 7 |
-
model = SentenceTransformer("yazied49/NAdine3")
|
| 8 |
-
|
| 9 |
-
# تحميل البيانات
|
| 10 |
-
df = pd.read_csv("final_special_needs_qa.csv") # استخدم CSV بدلًا من Excel
|
| 11 |
-
questions = df["question"].tolist()
|
| 12 |
-
answers = df["answer"].tolist()
|
| 13 |
-
question_embeddings = model.encode(questions, convert_to_tensor=True)
|
| 14 |
-
|
| 15 |
-
# الردود الاجتماعية الجاهزة
|
| 16 |
-
greetings = {
|
| 17 |
-
"هاي": "أهلاً وسهلاً! 😊 إزاي أقدر أساعدك؟",
|
| 18 |
-
"ازيك": "أنا تمام! شكرًا لسؤالك. عندك أي سؤال متعلق بذوي الاحتياجات الخاصة؟",
|
| 19 |
-
"السلام عليكم": "وعليكم السلام ورحمة الله وبركاته!",
|
| 20 |
-
"شكرا": "العفو! أنا دايمًا هنا للمساعدة 😊",
|
| 21 |
-
"thanks": "You're welcome! 💙",
|
| 22 |
-
"hi": "Hi there! How can I help you?",
|
| 23 |
-
"hello": "Hello! Feel free to ask anything.",
|
| 24 |
-
"merci": "على الرحب والسعة!",
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
def get_answer(user_input):
|
| 28 |
-
user_input_lower = user_input.lower().strip()
|
| 29 |
-
|
| 30 |
-
# الردود الاجتماعية
|
| 31 |
-
for key in greetings:
|
| 32 |
-
if key in user_input_lower:
|
| 33 |
-
return greetings[key]
|
| 34 |
-
|
| 35 |
-
# التحقق من أقرب سؤال
|
| 36 |
-
input_embedding = model.encode(user_input, convert_to_tensor=True)
|
| 37 |
-
cos_scores = util.pytorch_cos_sim(input_embedding, question_embeddings)[0]
|
| 38 |
-
best_match_idx = torch.argmax(cos_scores).item()
|
| 39 |
-
best_score = cos_scores[best_match_idx].item()
|
| 40 |
-
|
| 41 |
-
if best_score < 0.4:
|
| 42 |
-
return "
|
| 43 |
-
|
| 44 |
-
return answers[best_match_idx]
|
| 45 |
-
|
| 46 |
-
# واجهة Gradio
|
| 47 |
-
iface = gr.Interface(
|
| 48 |
-
fn=get_answer,
|
| 49 |
-
inputs=gr.Textbox(lines=2, placeholder="
|
| 50 |
-
outputs="text",
|
| 51 |
-
title="🤖
|
| 52 |
-
description="
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# تحميل الموديل من Hugging Face
|
| 7 |
+
model = SentenceTransformer("yazied49/NAdine3")
|
| 8 |
+
|
| 9 |
+
# تحميل البيانات
|
| 10 |
+
df = pd.read_csv("final_special_needs_qa.csv") # استخدم CSV بدلًا من Excel
|
| 11 |
+
questions = df["question"].tolist()
|
| 12 |
+
answers = df["answer"].tolist()
|
| 13 |
+
question_embeddings = model.encode(questions, convert_to_tensor=True)
|
| 14 |
+
|
| 15 |
+
# الردود الاجتماعية الجاهزة
|
| 16 |
+
greetings = {
|
| 17 |
+
"هاي": "أهلاً وسهلاً! 😊 إزاي أقدر أساعدك؟",
|
| 18 |
+
"ازيك": "أنا تمام! شكرًا لسؤالك. عندك أي سؤال متعلق بذوي الاحتياجات الخاصة؟",
|
| 19 |
+
"السلام عليكم": "وعليكم السلام ورحمة الله وبركاته!",
|
| 20 |
+
"شكرا": "العفو! أنا دايمًا هنا للمساعدة 😊",
|
| 21 |
+
"thanks": "You're welcome! 💙",
|
| 22 |
+
"hi": "Hi there! How can I help you?",
|
| 23 |
+
"hello": "Hello! Feel free to ask anything.",
|
| 24 |
+
"merci": "على الرحب والسعة!",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def get_answer(user_input):
|
| 28 |
+
user_input_lower = user_input.lower().strip()
|
| 29 |
+
|
| 30 |
+
# الردود الاجتماعية
|
| 31 |
+
for key in greetings:
|
| 32 |
+
if key in user_input_lower:
|
| 33 |
+
return greetings[key]
|
| 34 |
+
|
| 35 |
+
# التحقق من أقرب سؤال
|
| 36 |
+
input_embedding = model.encode(user_input, convert_to_tensor=True)
|
| 37 |
+
cos_scores = util.pytorch_cos_sim(input_embedding, question_embeddings)[0]
|
| 38 |
+
best_match_idx = torch.argmax(cos_scores).item()
|
| 39 |
+
best_score = cos_scores[best_match_idx].item()
|
| 40 |
+
|
| 41 |
+
if best_score < 0.4:
|
| 42 |
+
return "Sorry, I didn't understand your question. Can you please rephrase? 🤔"
|
| 43 |
+
|
| 44 |
+
return answers[best_match_idx]
|
| 45 |
+
|
| 46 |
+
# واجهة Gradio
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=get_answer,
|
| 49 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your question here..."),
|
| 50 |
+
outputs="text",
|
| 51 |
+
title="🤖 Special Needs Medical Assistant",
|
| 52 |
+
description="Ask any question related to special needs and we'll try to help you"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
iface.launch()
|