chat22GV2 / app.py
ramysaidagieb's picture
Update app.py
021d4f9 verified
raw
history blame
2.28 kB
import gradio as gr
import time
from rag_pipeline import RAGPipeline
from utils import process_documents
rag = RAGPipeline()
log_messages = []
def log(message):
log_messages.append(message)
return "\n".join(log_messages)
def process_files(files):
global log_messages
log_messages = []
log("🚀 [RAG] جاري تحميل الملفات...")
start_time = time.time()
all_chunks = process_documents(files, log)
log(f"📚 [RAG] تم استخراج {len(all_chunks)} مقاطع نصية.")
rag.build_index(all_chunks, log)
end_time = time.time()
log(f"✅ [RAG] تم بناء الفهرس في {end_time - start_time:.2f} ثانية.")
return "\n".join(log_messages)
def answer_question(question):
if not question.strip():
return "❗ الرجاء إدخال سؤال."
log(f"❓ [RAG] السؤال: {question}")
start_time = time.time()
answer, passages = rag.answer_question(question, log)
end_time = time.time()
log(f"🕒 تم توليد الإجابة في {end_time - start_time:.2f} ثانية.")
log(f"✅ الإجابة: {answer}")
sources = "\n\n".join([f"📄 من الفقرة:\n---\n{p}" for p in passages])
return answer, sources, "\n".join(log_messages)
with gr.Blocks() as demo:
gr.Markdown("# 🤖 مساعد بحث بالذكاء الاصطناعي للكتب العربية")
with gr.Row():
file_input = gr.File(file_types=[".pdf", ".docx", ".txt"], file_count="multiple", label="📂 رفع الكتب")
upload_btn = gr.Button("🔄 بناء الفهرس")
log_output = gr.Textbox(label="🧭 سجل التشغيل", lines=15)
upload_btn.click(fn=process_files, inputs=[file_input], outputs=[log_output])
with gr.Row():
question_input = gr.Textbox(label="📝 اطرح سؤالك هنا")
ask_btn = gr.Button("📤 إرسال السؤال")
answer_output = gr.Textbox(label="📢 الإجابة", lines=5)
sources_output = gr.Textbox(label="📌 مصدر الإجابة", lines=10)
log_box = gr.Textbox(label="🔍 سجل التشغيل الكامل", lines=10)
ask_btn.click(fn=answer_question, inputs=[question_input], outputs=[answer_output, sources_output, log_box])
demo.launch()