ramysaidagieb commited on
Commit
e2c1ba7
·
verified ·
1 Parent(s): 286b392

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -41
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 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()
 
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()