Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from rag_pipeline import RAGPipeline
|
| 3 |
from utils import process_documents
|
| 4 |
-
import asyncio
|
| 5 |
import time
|
| 6 |
|
| 7 |
rag = RAGPipeline()
|
| 8 |
|
| 9 |
def log_message(msg, logs):
|
| 10 |
-
logs
|
| 11 |
return logs
|
| 12 |
|
| 13 |
def upload_and_index(files, logs):
|
|
@@ -21,10 +20,10 @@ def upload_and_index(files, logs):
|
|
| 21 |
|
| 22 |
logs = log_message(f"[RAG] بناء الفهرس لـ {len(all_chunks)} مقطع...", logs)
|
| 23 |
start = time.time()
|
| 24 |
-
rag.build_index(all_chunks
|
| 25 |
duration = time.time() - start
|
| 26 |
logs = log_message(f"[RAG] تم بناء الفهرس في {duration:.2f} ثانية.", logs)
|
| 27 |
-
return logs, True
|
| 28 |
|
| 29 |
def answer_question(question, logs):
|
| 30 |
logs = log_message(f"[RAG] استلام السؤال: {question}", logs)
|
|
@@ -36,22 +35,19 @@ def answer_question(question, logs):
|
|
| 36 |
return answer, logs
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
logs = gr.State(
|
| 40 |
-
gr.Markdown("# نظام استرجاع المعرفة
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
files_input = gr.File(file_types=['.pdf', '.docx', '.txt'], file_count="multiple", label="رفع الملفات")
|
| 44 |
upload_btn = gr.Button("رفع وبناء الفهرس")
|
| 45 |
-
logs_output = gr.Textbox(label="سجل العمليات", lines=10, interactive=False)
|
| 46 |
|
|
|
|
| 47 |
question_input = gr.Textbox(label="اكتب سؤالك هنا", visible=False)
|
| 48 |
ask_btn = gr.Button("إرسال السؤال", visible=False)
|
| 49 |
answer_output = gr.Textbox(label="الإجابة", lines=5)
|
| 50 |
|
| 51 |
-
upload_btn.click(upload_and_index, inputs=[files_input, logs], outputs=[logs_output, question_input])
|
| 52 |
-
upload_btn.click(lambda: True, None, question_input) # اظهار مربع السؤال
|
| 53 |
-
upload_btn.click(lambda: True, None, ask_btn) # اظهار زر السؤال
|
| 54 |
-
|
| 55 |
ask_btn.click(answer_question, inputs=[question_input, logs], outputs=[answer_output, logs_output])
|
| 56 |
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from rag_pipeline import RAGPipeline
|
| 3 |
from utils import process_documents
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
rag = RAGPipeline()
|
| 7 |
|
| 8 |
def log_message(msg, logs):
|
| 9 |
+
logs = logs + msg + "\n"
|
| 10 |
return logs
|
| 11 |
|
| 12 |
def upload_and_index(files, logs):
|
|
|
|
| 20 |
|
| 21 |
logs = log_message(f"[RAG] بناء الفهرس لـ {len(all_chunks)} مقطع...", logs)
|
| 22 |
start = time.time()
|
| 23 |
+
rag.build_index(all_chunks)
|
| 24 |
duration = time.time() - start
|
| 25 |
logs = log_message(f"[RAG] تم بناء الفهرس في {duration:.2f} ثانية.", logs)
|
| 26 |
+
return logs, gr.update(visible=True), gr.update(visible=True)
|
| 27 |
|
| 28 |
def answer_question(question, logs):
|
| 29 |
logs = log_message(f"[RAG] استلام السؤال: {question}", logs)
|
|
|
|
| 35 |
return answer, logs
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
+
logs = gr.State("")
|
| 39 |
+
gr.Markdown("# نظام استرجاع المعرفة (RAG)")
|
| 40 |
|
| 41 |
with gr.Row():
|
| 42 |
files_input = gr.File(file_types=['.pdf', '.docx', '.txt'], file_count="multiple", label="رفع الملفات")
|
| 43 |
upload_btn = gr.Button("رفع وبناء الفهرس")
|
|
|
|
| 44 |
|
| 45 |
+
logs_output = gr.Textbox(label="سجل العمليات", lines=12, interactive=False, value="")
|
| 46 |
question_input = gr.Textbox(label="اكتب سؤالك هنا", visible=False)
|
| 47 |
ask_btn = gr.Button("إرسال السؤال", visible=False)
|
| 48 |
answer_output = gr.Textbox(label="الإجابة", lines=5)
|
| 49 |
|
| 50 |
+
upload_btn.click(upload_and_index, inputs=[files_input, logs], outputs=[logs_output, question_input, ask_btn])
|
|
|
|
|
|
|
|
|
|
| 51 |
ask_btn.click(answer_question, inputs=[question_input, logs], outputs=[answer_output, logs_output])
|
| 52 |
|
| 53 |
demo.launch()
|