Download app.py from yazied49/mo: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/yazied49/mo/resolve/main/app.py
- Command line
-
hf download hf://spaces/yazied49/mo/app.py
-
curl -L -o app.py https://huggingface.co/spaces/yazied49/mo/resolve/main/app.py
1.38 kB
| import gradio as gr | |
| import pandas as pd | |
| import torch | |
| from sentence_transformers import SentenceTransformer, util | |
| # تحميل الداتا | |
| df = pd.read_csv("combined_qa_data.csv", encoding='ISO-8859-1') | |
| questions = df['question'].tolist() | |
| answers = df['answer'].tolist() | |
| # تحميل موديل التمثيل العددي | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| embeddings = model.encode(questions, convert_to_tensor=True) | |
| # دالة البحث | |
| def get_answer(user_question, threshold=0.75): | |
| if not user_question.strip(): | |
| return "من فضلك أدخل سؤالًا." | |
| question_embedding = model.encode(user_question, convert_to_tensor=True) | |
| scores = util.pytorch_cos_sim(question_embedding, embeddings) | |
| top_idx = torch.argmax(scores).item() | |
| top_score = scores[0][top_idx].item() | |
| if top_score >= threshold: | |
| return answers[top_idx] | |
| else: | |
| return "Sorry, I couldn't find a suitable answer to your question. Please try rephrasing it." | |
| # Gradio واجهة | |
| iface = gr.Interface( | |
| fn=get_answer, | |
| inputs=gr.Textbox(lines=2, placeholder="اكتب سؤالك هنا..."), | |
| outputs="text", | |
| title="الرد على الأسئلة الشائعة", | |
| description="أدخل سؤالاً وسأبحث عن أقرب إجابة من قاعدة البيانات." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |