Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
-
from rag_pipeline import RAGPipeline
|
3 |
import time
|
|
|
|
|
4 |
|
5 |
rag = RAGPipeline()
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
start_time = time.time()
|
9 |
-
|
|
|
|
|
10 |
end_time = time.time()
|
11 |
-
log
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
with gr.Row():
|
18 |
-
|
19 |
-
|
20 |
-
upload_btn = gr.Button("⬆️ رفع ومعالجة")
|
21 |
-
upload_log = gr.Textbox(label="🔍 سجل المعالجة", lines=10)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
answer = gr.Textbox(label="📜 الإجابة", lines=5)
|
27 |
-
sources = gr.Textbox(label="🧭 المراجع", lines=10)
|
28 |
|
29 |
-
|
30 |
-
submit_btn.click(fn=submit_question, inputs=[question], outputs=[answer, sources])
|
31 |
|
32 |
-
demo.launch()
|
|
|
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 log(message):
|
11 |
+
log_messages.append(message)
|
12 |
+
return "\n".join(log_messages)
|
13 |
+
|
14 |
+
def process_files(files):
|
15 |
+
global log_messages
|
16 |
+
log_messages = []
|
17 |
+
log("🚀 [RAG] جاري تحميل الملفات...")
|
18 |
start_time = time.time()
|
19 |
+
all_chunks = process_documents(files, log)
|
20 |
+
log(f"📚 [RAG] تم استخراج {len(all_chunks)} مقاطع نصية.")
|
21 |
+
rag.build_index(all_chunks, log)
|
22 |
end_time = time.time()
|
23 |
+
log(f"✅ [RAG] تم بناء الفهرس في {end_time - start_time:.2f} ثانية.")
|
24 |
+
return "\n".join(log_messages)
|
25 |
+
|
26 |
+
def answer_question(question):
|
27 |
+
if not question.strip():
|
28 |
+
return "❗ الرجاء إدخال سؤال."
|
29 |
+
log(f"❓ [RAG] السؤال: {question}")
|
30 |
+
start_time = time.time()
|
31 |
+
answer, passages = rag.answer_question(question, log)
|
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"], file_count="multiple", label="📂 رفع الكتب")
|
43 |
+
upload_btn = gr.Button("🔄 بناء الفهرس")
|
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(label="📝 اطرح سؤالك هنا")
|
51 |
+
ask_btn = gr.Button("📤 إرسال السؤال")
|
|
|
|
|
52 |
|
53 |
+
answer_output = gr.Textbox(label="📢 الإجابة", lines=5)
|
54 |
+
sources_output = gr.Textbox(label="📌 مصدر الإجابة", lines=10)
|
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()
|