File size: 1,946 Bytes
4028152
 
72b0fdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4028152
 
72b0fdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
import gradio as gr
from rag_pipeline import RAGPipeline

logs = []

def logger(message):
    logs.append(message)
    if len(logs) > 50:
        logs.pop(0)

# تهيئة RAGPipeline مع ال logger
rag = RAGPipeline(logger=logger)

def process_documents(texts):
    chunks = [t.strip() for t in texts.split("\n") if t.strip()]
    msg = rag.build_index(chunks)
    return msg, "\n".join(logs)

def answer_question(question):
    answer, sources = rag.generate_answer(question)
    return answer, "\n".join(sources), "\n".join(logs)

with gr.Blocks() as demo:
    gr.Markdown("## نظام RAG بالعربية مع عرض سجلات التشغيل")

    with gr.Tab("رفع المستندات وبناء الفهرس"):
        docs_input = gr.Textbox(label="أدخل نصوص المستندات (سطر لكل مستند)", lines=10)
        build_btn = gr.Button("بناء الفهرس")
        build_status = gr.Textbox(label="حالة بناء الفهرس", lines=3, interactive=False)
        logs_box = gr.Textbox(label="سجلات التشغيل (Logs)", lines=10, interactive=False)

        build_btn.click(fn=process_documents, inputs=docs_input, outputs=[build_status, logs_box])

    with gr.Tab("طرح الأسئلة"):
        question_input = gr.Textbox(label="السؤال", lines=2)
        answer_output = gr.Textbox(label="الإجابة", lines=5, interactive=False)
        sources_output = gr.Textbox(label="المراجع المستخدمة", lines=5, interactive=False)
        logs_box2 = gr.Textbox(label="سجلات التشغيل (Logs)", lines=10, interactive=False)

        question_input.submit(fn=answer_question, inputs=question_input, outputs=[answer_output, sources_output, logs_box2])
        # أو زر سؤال
        # ask_btn = gr.Button("اطرح السؤال")
        # ask_btn.click(fn=answer_question, inputs=question_input, outputs=[answer_output, sources_output, logs_box2])

demo.launch()