|
|
import gradio as gr |
|
|
from sentence_transformers import SentenceTransformer, util |
|
|
import pandas as pd |
|
|
import torch |
|
|
|
|
|
|
|
|
model = SentenceTransformer("yazied49/NAdine3") |
|
|
|
|
|
|
|
|
df = pd.read_csv("final_special_needs_qa.csv") |
|
|
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] |
|
|
|
|
|
|
|
|
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() |
|
|
|