File size: 1,793 Bytes
4028152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 time
import gradio as gr
from rag_pipeline import RAGPipeline
from utils import process_documents

rag = RAGPipeline()

def process_files(docs):
    status_messages = []
    start_time = time.time()
    status_messages.append("📁 جاري قراءة الملفات...")
    passages = process_documents(docs)
    status_messages.append(f"✅ تم استخراج {len(passages)} مقطع.")
    build_start = time.time()
    rag.build_index(passages)
    build_duration = time.time() - build_start
    status_messages.append(f"✅ تم بناء الفهرس في {build_duration:.2f} ثانية.")
    total_time = time.time() - start_time
    status_messages.append(f"⏱️ المدة الإجمالية: {total_time:.2f} ثانية.")
    return "تم تجهيز النظام للإجابة.", "\n".join(status_messages)

def ask_question(question):
    answer, cited = rag.generate_answer(question)
    return answer, "\n".join(cited)

with gr.Blocks() as demo:
    gr.Markdown("# 🤖 مساعد ذكي يعتمد على كتبك الخاصة")
    with gr.Row():
        file_input = gr.File(file_types=[".pdf", ".docx"], file_count="multiple", label="📚 ارفع ملفاتك")
        upload_btn = gr.Button("🔍 معالجة الملفات")
    status_output = gr.Textbox(label="📋 الحالة", lines=10)
    upload_btn.click(process_files, inputs=[file_input], outputs=["textbox", status_output])

    with gr.Row():
        question_input = gr.Textbox(label="✍️ اكتب سؤالك")
        ask_btn = gr.Button("🧠 اسأل")
    answer_output = gr.Textbox(label="📢 الإجابة")
    sources_output = gr.Textbox(label="🔗 المراجع")
    ask_btn.click(ask_question, inputs=[question_input], outputs=[answer_output, sources_output])

demo.launch()