File size: 2,189 Bytes
99354e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
import gradio as gr
from utils import extract_texts_from_files, clean_arabic, chunk_text
from rag_pipeline import ArabicRAGPipeline, save_to_doc

rag = ArabicRAGPipeline()

def process_files(files):
    all_chunks = []
    for file in files:
        text = extract_texts_from_files(file)
        if not text:
            continue
        clean_text = clean_arabic(text)
        chunks = chunk_text(clean_text, source=file.name)
        all_chunks.extend(chunks)
    rag.build_index(all_chunks)
    return "✅ تم تحميل وفهرسة الملفات بنجاح", None

def ask_question(question):
    passages = rag.retrieve(question)
    answer, cited_passages = rag.generate_answer(question, passages)
    citations = "\n\n".join(f"📌 {src}" for _, src in cited_passages)
    return answer, citations

def export_answer(answer, citations):
    return save_to_doc(answer, citations)

with gr.Blocks(theme=gr.themes.Base(), css="body { background-color: #111; color: #eee; font-family: 'Cairo', sans-serif; }") as demo:
    gr.Image("assets/logo.png", height=120)
    gr.Markdown("### 🤖 مساعد الإيمان - روبوت ذكي لتحليل كتب البابا شنودة الثالث")

    with gr.Row():
        file_input = gr.File(label="📚 تحميل ملفات PDF أو DOCX", file_types=[".pdf", ".docx"], file_count="multiple")
        file_status = gr.Textbox(label="📌 الحالة", interactive=False)

    file_input.change(fn=process_files, inputs=file_input, outputs=file_status)

    question_input = gr.Textbox(label="✍️ اكتب سؤالك هنا", placeholder="مثال: ما هو دور الإيمان في المعجزات؟")
    answer_output = gr.Textbox(label="🧠 الإجابة", lines=5)
    citations_output = gr.Textbox(label="🔖 المراجع المستخدمة", lines=10)
    export_btn = gr.Button("💾 حفظ الإجابة كمستند")
    output_file = gr.File(label="📥 تحميل الملف")

    question_input.submit(fn=ask_question, inputs=question_input, outputs=[answer_output, citations_output])
    export_btn.click(fn=export_answer, inputs=[answer_output, citations_output], outputs=output_file)

demo.launch()