Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import time
|
| 3 |
-
from rag_pipeline import RAGPipeline
|
| 4 |
from utils import process_documents
|
|
|
|
| 5 |
|
| 6 |
rag = RAGPipeline()
|
| 7 |
|
| 8 |
log_messages = []
|
| 9 |
|
| 10 |
-
def
|
| 11 |
log_messages.append(message)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
def answer_question(question):
|
| 27 |
if not question.strip():
|
| 28 |
-
return "
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
answer
|
| 32 |
-
end_time = time.time()
|
| 33 |
-
log(f"🕒 تم توليد الإجابة في {end_time - start_time:.2f} ثانية.")
|
| 34 |
-
log(f"✅ الإجابة: {answer}")
|
| 35 |
-
sources = "\n\n".join([f"📄 من الفقرة:\n---\n{p}" for p in passages])
|
| 36 |
-
return answer, sources, "\n".join(log_messages)
|
| 37 |
-
|
| 38 |
-
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("# 🤖 مساعد بحث بالذكاء الاصطناعي للكتب العربية")
|
| 40 |
|
|
|
|
|
|
|
| 41 |
with gr.Row():
|
| 42 |
-
file_input = gr.File(file_types=[".pdf", ".docx", ".txt"],
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
log_output = gr.Textbox(label="🧭 سجل التشغيل", lines=15)
|
| 46 |
-
|
| 47 |
-
upload_btn.click(fn=process_files, inputs=[file_input], outputs=[log_output])
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
-
question_input = gr.Textbox(
|
| 51 |
-
|
| 52 |
|
| 53 |
-
answer_output = gr.Textbox(label="
|
| 54 |
-
|
| 55 |
-
log_box = gr.Textbox(label="🔍 سجل التشغيل الكامل", lines=10)
|
| 56 |
-
|
| 57 |
-
ask_btn.click(fn=answer_question, inputs=[question_input], outputs=[answer_output, sources_output, log_box])
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from utils import process_documents
|
| 3 |
+
from rag_pipeline import RAGPipeline
|
| 4 |
|
| 5 |
rag = RAGPipeline()
|
| 6 |
|
| 7 |
log_messages = []
|
| 8 |
|
| 9 |
+
def log_callback(message):
|
| 10 |
log_messages.append(message)
|
| 11 |
+
print(message)
|
| 12 |
+
|
| 13 |
+
def upload_and_index(files):
|
| 14 |
+
log_messages.clear()
|
| 15 |
+
if not files:
|
| 16 |
+
return "⚠️ يرجى رفع ملفات.", "", ""
|
| 17 |
+
|
| 18 |
+
chunks = []
|
| 19 |
+
for file in files:
|
| 20 |
+
content_chunks = process_documents(file.name)
|
| 21 |
+
chunks.extend(content_chunks)
|
| 22 |
+
|
| 23 |
+
index_status = rag.build_index(chunks, log_callback=log_callback)
|
| 24 |
+
return index_status, "\n".join(log_messages), ""
|
| 25 |
|
| 26 |
def answer_question(question):
|
| 27 |
if not question.strip():
|
| 28 |
+
return "⚠️ يرجى إدخال سؤال."
|
| 29 |
+
log_messages.clear()
|
| 30 |
+
answer = rag.answer_question(question, log_callback=log_callback)
|
| 31 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
with gr.Blocks(title="مساعد عربي للكتب") as demo:
|
| 34 |
+
gr.Markdown("## 📚 مساعد ذكي للإجابة من كتبك باللغة العربية")
|
| 35 |
with gr.Row():
|
| 36 |
+
file_input = gr.File(file_types=[".pdf", ".docx", ".txt"], label="📁 حمّل ملفاتك", file_count="multiple")
|
| 37 |
+
upload_button = gr.Button("🔍 رفع وبناء الفهرس")
|
| 38 |
+
upload_output = gr.Textbox(label="🔔 الحالة", lines=2)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with gr.Row():
|
| 41 |
+
question_input = gr.Textbox(placeholder="اكتب سؤالك هنا...", label="❓ السؤال")
|
| 42 |
+
submit_button = gr.Button("🚀 أرسل السؤال")
|
| 43 |
|
| 44 |
+
answer_output = gr.Textbox(label="🧠 الإجابة", lines=6)
|
| 45 |
+
logs_output = gr.Textbox(label="📜 سجل العمليات", lines=12)
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
upload_button.click(fn=upload_and_index, inputs=[file_input], outputs=[upload_output, logs_output, answer_output])
|
| 48 |
+
submit_button.click(fn=answer_question, inputs=[question_input], outputs=[answer_output])
|
| 49 |
+
|
| 50 |
demo.launch()
|