Spaces:
Sleeping
Sleeping
import time | |
import gradio as gr | |
from rag_pipeline import RAGPipeline | |
from utils import process_documents | |
rag = RAGPipeline() | |
def process_files(docs): | |
status_messages = [] | |
start_time = time.time() | |
status_messages.append("📁 جاري قراءة الملفات...") | |
passages = process_documents(docs) | |
status_messages.append(f"✅ تم استخراج {len(passages)} مقطع.") | |
build_start = time.time() | |
rag.build_index(passages) | |
build_duration = time.time() - build_start | |
status_messages.append(f"✅ تم بناء الفهرس في {build_duration:.2f} ثانية.") | |
total_time = time.time() - start_time | |
status_messages.append(f"⏱️ المدة الإجمالية: {total_time:.2f} ثانية.") | |
return "تم تجهيز النظام للإجابة.", "\n".join(status_messages) | |
def ask_question(question): | |
answer, cited = rag.generate_answer(question) | |
return answer, "\n".join(cited) | |
with gr.Blocks() as demo: | |
gr.Markdown("# 🤖 مساعد ذكي يعتمد على كتبك الخاصة") | |
with gr.Row(): | |
file_input = gr.File(file_types=[".pdf", ".docx"], file_count="multiple", label="📚 ارفع ملفاتك") | |
upload_btn = gr.Button("🔍 معالجة الملفات") | |
status_output = gr.Textbox(label="📋 الحالة", lines=10) | |
upload_btn.click(process_files, inputs=[file_input], outputs=["textbox", status_output]) | |
with gr.Row(): | |
question_input = gr.Textbox(label="✍️ اكتب سؤالك") | |
ask_btn = gr.Button("🧠 اسأل") | |
answer_output = gr.Textbox(label="📢 الإجابة") | |
sources_output = gr.Textbox(label="🔗 المراجع") | |
ask_btn.click(ask_question, inputs=[question_input], outputs=[answer_output, sources_output]) | |
demo.launch() |