Download app.py from yazied49/nn: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/yazied49/nn/resolve/main/app.py
- Command line
-
hf download hf://spaces/yazied49/nn/app.py
-
curl -L -o app.py https://huggingface.co/spaces/yazied49/nn/resolve/main/app.py
2.08 kB
| 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() | |