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("""
نظام التحليل اللاهوتي المدعوم بالذكاء الاصطناعي
نظام لتحليل الكتب الدينية العربية وإجابة الأسئلة مع الإشارة إلى المصادر
""")
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()