Spaces:
Sleeping
Sleeping
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() | |