File size: 1,375 Bytes
5565640
 
 
 
 
 
26fb581
5565640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()