File size: 2,793 Bytes
c90b40e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import numpy as np
from utils import DocumentProcessor
from rag_pipeline import ArabicRAGSystem

css = """
.rtl {direction: rtl; text-align: right;}
.header {background: #f0f2f6; padding: 20px; border-radius: 10px;}
.markdown-body {font-family: 'Amiri', serif; font-size: 18px;}
.highlight {background: #fff3cd; padding: 10px; border-radius: 5px;}
"""

with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
    rag = ArabicRAGSystem()
    
    with gr.Column(elem_classes="header"):
        gr.Markdown("""
        <div class='rtl'>
        <h1 style="text-align:center; color: #2B547E;">نظام التحليل اللاهوتي المدعوم بالذكاء الاصطناعي</h1>
        <p style="text-align:center">نظام لتحليل الكتب الدينية العربية وإجابة الأسئلة مع الإشارة إلى المصادر</p>
        </div>
        """)
    
    with gr.Row():
        with gr.Column(scale=1):
            file_upload = gr.File(label="تحميل الملفات", file_types=[".pdf", ".docx"], 
                               file_count="multiple", elem_classes="rtl")
            with gr.Accordion("إعدادات البحث", open=False):
                top_k = gr.Slider(3, 10, value=5, step=1, label="عدد المقاطع المستخدمة")
                temperature = gr.Slider(0.1, 1.0, value=0.7, label="درجة الإبداعية")
        
        with gr.Column(scale=2):
            question = gr.Textbox(label="اكتب سؤالك هنا", lines=3, elem_classes="rtl")
            answer = gr.Markdown(label="الإجابة", elem_classes=["markdown-body", "rtl"])
            sources = gr.DataFrame(label="المصادر المستخدمة", 
                                 headers=["النص", "المصدر", "الصفحة", "الثقة"],
                                 elem_classes="rtl")

    def process_query(files, question, top_k, temp):
        if not files or not question:
            return "", []
        
        processor = DocumentProcessor()
        documents = processor.process_documents(files)
        answer_text, sources_data = rag.generate_answer(
            question=question,
            documents=documents,
            top_k=top_k,
            temperature=temp
        )
        
        formatted_sources = []
        for src in sources_data:
            formatted_sources.append([
                src['text'],
                src['source'],
                src['page'],
                f"{src['score']:.2f}"
            ])
        
        return answer_text, formatted_sources

    question.submit(
        process_query,
        inputs=[file_upload, question, top_k, temperature],
        outputs=[answer, sources]
    )

if __name__ == "__main__":
    demo.launch()