Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,6 @@ def log_message(msg, logs):
|
|
9 |
return logs
|
10 |
|
11 |
def upload_and_index(files, logs):
|
12 |
-
"""
|
13 |
-
1) رفع وبناء الفهرس من المقاطع
|
14 |
-
2) يقوم تلقائيًا بتلخيص كل مقطع بعد الانتهاء من بناء الفهرس
|
15 |
-
"""
|
16 |
logs = log_message("[RAG] بدء معالجة الملفات...", logs)
|
17 |
all_chunks = []
|
18 |
for file in files:
|
@@ -21,36 +17,30 @@ def upload_and_index(files, logs):
|
|
21 |
all_chunks.extend(chunks)
|
22 |
logs = log_message(f"[RAG] تم استخراج {len(chunks)} مقطع من {file.name}", logs)
|
23 |
|
24 |
-
# بناء الفهرس
|
25 |
rag.build_index(all_chunks)
|
26 |
logs = log_message("[RAG] تم بناء الفهرس.", logs)
|
27 |
|
28 |
-
#
|
29 |
-
logs = log_message("[RAG] بدأ التلخيص التلقائي
|
30 |
-
rag.summarize_all_chunks()
|
31 |
-
logs = log_message("[RAG] تم التلخيص لجميع
|
32 |
|
33 |
-
# إظهار زر الإجابة بعد اكتمال التلخيص
|
34 |
return logs, gr.update(visible=True), gr.update(visible=True)
|
35 |
|
36 |
def answer_question(question, logs):
|
37 |
-
"""
|
38 |
-
توليد الإجابة بناءً على الملخصات المحفوظة
|
39 |
-
"""
|
40 |
logs = log_message(f"[RAG] استلام السؤال: {question}", logs)
|
41 |
|
42 |
-
# استدعاء إجابة باستخدام التلخيص المحفوظ
|
43 |
answer, sources, combined_summary = rag.answer(question)
|
44 |
|
45 |
if not combined_summary.strip():
|
46 |
-
logs = log_message("[RAG] لم يتم إنشاء ملخص صالح.
|
47 |
return "", logs
|
48 |
|
49 |
if not answer.strip():
|
50 |
-
logs = log_message("[RAG]
|
51 |
else:
|
52 |
-
logs = log_message("[RAG] تم توليد
|
53 |
-
logs = log_message(f"[RAG]
|
54 |
|
55 |
return answer, combined_summary, logs
|
56 |
|
@@ -59,32 +49,17 @@ with gr.Blocks() as demo:
|
|
59 |
gr.Markdown("# 🕌 نظام استرجاع المعرفة باللغة العربية")
|
60 |
|
61 |
with gr.Row():
|
62 |
-
files_input = gr.File(
|
63 |
-
|
64 |
-
file_count="multiple",
|
65 |
-
label="📂 رفع الملفات"
|
66 |
-
)
|
67 |
-
upload_btn = gr.Button("🔄 رفع وبناء الفهرس")
|
68 |
|
69 |
-
answer_btn = gr.Button("✍️ أجب عن السؤال", visible=False)
|
70 |
question_input = gr.Textbox(label="❓ اكتب سؤالك هنا", visible=False)
|
|
|
71 |
|
72 |
logs_output = gr.Textbox(label="📜 سجل العمليات", lines=10, interactive=False)
|
73 |
summary_output = gr.Textbox(label="📌 الملخص المستخدم", lines=5)
|
74 |
answer_output = gr.Textbox(label="✅ الإجابة النهائية", lines=5)
|
75 |
|
76 |
-
upload_btn.click(
|
77 |
-
|
78 |
-
inputs=[files_input, logs],
|
79 |
-
outputs=[logs_output, answer_btn, question_input]
|
80 |
-
)
|
81 |
-
|
82 |
-
# بعد اكتمال التلخيص التلقائي، نجعل زر "أجب عن السؤال" و "اكتب سؤالك" مرئيين
|
83 |
-
# ومن ثم يمكن للمستخدم كتابة السؤال والضغط مباشرة على "أجب عن السؤال"
|
84 |
-
answer_btn.click(
|
85 |
-
answer_question,
|
86 |
-
inputs=[question_input, logs],
|
87 |
-
outputs=[answer_output, summary_output, logs_output]
|
88 |
-
)
|
89 |
|
90 |
demo.launch()
|
|
|
9 |
return logs
|
10 |
|
11 |
def upload_and_index(files, logs):
|
|
|
|
|
|
|
|
|
12 |
logs = log_message("[RAG] بدء معالجة الملفات...", logs)
|
13 |
all_chunks = []
|
14 |
for file in files:
|
|
|
17 |
all_chunks.extend(chunks)
|
18 |
logs = log_message(f"[RAG] تم استخراج {len(chunks)} مقطع من {file.name}", logs)
|
19 |
|
|
|
20 |
rag.build_index(all_chunks)
|
21 |
logs = log_message("[RAG] تم بناء الفهرس.", logs)
|
22 |
|
23 |
+
# Summarize only first N chunks for performance
|
24 |
+
logs = log_message("[RAG] بدأ التلخيص التلقائي لأول 20 مقطع...", logs)
|
25 |
+
rag.summarize_all_chunks(max_chunks=20)
|
26 |
+
logs = log_message("[RAG] تم التلخيص لجميع المقاطع المحددة.", logs)
|
27 |
|
|
|
28 |
return logs, gr.update(visible=True), gr.update(visible=True)
|
29 |
|
30 |
def answer_question(question, logs):
|
|
|
|
|
|
|
31 |
logs = log_message(f"[RAG] استلام السؤال: {question}", logs)
|
32 |
|
|
|
33 |
answer, sources, combined_summary = rag.answer(question)
|
34 |
|
35 |
if not combined_summary.strip():
|
36 |
+
logs = log_message("[RAG] لم يتم إنشاء ملخص صالح.", logs)
|
37 |
return "", logs
|
38 |
|
39 |
if not answer.strip():
|
40 |
+
logs = log_message("[RAG] لم يتم توليد إجابة. حاول صياغة السؤال بشكل أوضح.", logs)
|
41 |
else:
|
42 |
+
logs = log_message("[RAG] تم توليد الإجابة بنجاح.", logs)
|
43 |
+
logs = log_message(f"[RAG] المقاطع المستخدمة: {sources}", logs)
|
44 |
|
45 |
return answer, combined_summary, logs
|
46 |
|
|
|
49 |
gr.Markdown("# 🕌 نظام استرجاع المعرفة باللغة العربية")
|
50 |
|
51 |
with gr.Row():
|
52 |
+
files_input = gr.File(file_types=[".pdf", ".docx", ".txt"], file_count="multiple", label="📂 رفع الملفات")
|
53 |
+
upload_btn = gr.Button("🔄 رفع وبناء الفهرس وتلخيص")
|
|
|
|
|
|
|
|
|
54 |
|
|
|
55 |
question_input = gr.Textbox(label="❓ اكتب سؤالك هنا", visible=False)
|
56 |
+
answer_btn = gr.Button("✍️ أجب عن السؤال", visible=False)
|
57 |
|
58 |
logs_output = gr.Textbox(label="📜 سجل العمليات", lines=10, interactive=False)
|
59 |
summary_output = gr.Textbox(label="📌 الملخص المستخدم", lines=5)
|
60 |
answer_output = gr.Textbox(label="✅ الإجابة النهائية", lines=5)
|
61 |
|
62 |
+
upload_btn.click(upload_and_index, inputs=[files_input, logs], outputs=[logs_output, question_input, answer_btn])
|
63 |
+
answer_btn.click(answer_question, inputs=[question_input, logs], outputs=[answer_output, summary_output, logs_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
demo.launch()
|