nnn / app.py
yazied49's picture
Update app.py
ed6a445 verified
import gradio as gr
import pandas as pd
import torch
from sentence_transformers import SentenceTransformer, util
# تحميل الداتا
df = pd.read_csv("cleaned_questions.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()