File size: 2,083 Bytes
bfed17f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from sentence_transformers import SentenceTransformer, util
import pandas as pd
import torch

# تحميل الموديل من Hugging Face
model = SentenceTransformer("yazied49/NAdine3")

# تحميل البيانات
df = pd.read_csv("final_special_needs_qa.csv")  # استخدم CSV بدلًا من Excel
questions = df["question"].tolist()
answers = df["answer"].tolist()
question_embeddings = model.encode(questions, convert_to_tensor=True)

# الردود الاجتماعية الجاهزة
greetings = {
    "هاي": "أهلاً وسهلاً! 😊 إزاي أقدر أساعدك؟",
    "ازيك": "أنا تمام! شكرًا لسؤالك. عندك أي سؤال متعلق بذوي الاحتياجات الخاصة؟",
    "السلام عليكم": "وعليكم السلام ورحمة الله وبركاته!",
    "شكرا": "العفو! أنا دايمًا هنا للمساعدة 😊",
    "thanks": "You're welcome! 💙",
    "hi": "Hi there! How can I help you?",
    "hello": "Hello! Feel free to ask anything.",
    "merci": "على الرحب والسعة!",
}

def get_answer(user_input):
    user_input_lower = user_input.lower().strip()

    # الردود الاجتماعية
    for key in greetings:
        if key in user_input_lower:
            return greetings[key]

    # التحقق من أقرب سؤال
    input_embedding = model.encode(user_input, convert_to_tensor=True)
    cos_scores = util.pytorch_cos_sim(input_embedding, question_embeddings)[0]
    best_match_idx = torch.argmax(cos_scores).item()
    best_score = cos_scores[best_match_idx].item()

    if best_score < 0.4:
        return "Sorry, I didn't understand your question. Can you please rephrase? 🤔"

    return answers[best_match_idx]

# واجهة Gradio
iface = gr.Interface(
    fn=get_answer,
    inputs=gr.Textbox(lines=2, placeholder="Type your question here..."),
    outputs="text",
    title="🤖 Special Needs Medical Assistant",
    description="Ask any question related to special needs and we'll try to help you"
)

iface.launch()