ramysaidagieb commited on
Commit
2d575e5
·
verified ·
1 Parent(s): fcec14a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -24
app.py CHANGED
@@ -1,18 +1,40 @@
1
  import gradio as gr
 
2
  from rag_pipeline import RAGPipeline
 
 
3
 
4
  logs = []
5
 
6
  def logger(message):
7
  logs.append(message)
8
- if len(logs) > 50:
9
  logs.pop(0)
10
 
11
- # تهيئة RAGPipeline مع ال logger
12
  rag = RAGPipeline(logger=logger)
13
 
14
- def process_documents(texts):
15
- chunks = [t.strip() for t in texts.split("\n") if t.strip()]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  msg = rag.build_index(chunks)
17
  return msg, "\n".join(logs)
18
 
@@ -21,25 +43,23 @@ def answer_question(question):
21
  return answer, "\n".join(sources), "\n".join(logs)
22
 
23
  with gr.Blocks() as demo:
24
- gr.Markdown("## نظام RAG بالعربية مع عرض سجلات التشغيل")
25
-
26
- with gr.Tab("رفع المستندات وبناء الفهرس"):
27
- docs_input = gr.Textbox(label="أدخل نصوص المستندات (سطر لكل مستند)", lines=10)
28
- build_btn = gr.Button("بناء الفهرس")
29
- build_status = gr.Textbox(label="حالة بناء الفهرس", lines=3, interactive=False)
30
- logs_box = gr.Textbox(label="سجلات التشغيل (Logs)", lines=10, interactive=False)
31
-
32
- build_btn.click(fn=process_documents, inputs=docs_input, outputs=[build_status, logs_box])
33
-
34
- with gr.Tab("طرح الأسئلة"):
35
- question_input = gr.Textbox(label="السؤال", lines=2)
36
- answer_output = gr.Textbox(label="الإجابة", lines=5, interactive=False)
37
- sources_output = gr.Textbox(label="المراجع المستخدمة", lines=5, interactive=False)
38
- logs_box2 = gr.Textbox(label="سجلات التشغيل (Logs)", lines=10, interactive=False)
39
-
40
- question_input.submit(fn=answer_question, inputs=question_input, outputs=[answer_output, sources_output, logs_box2])
41
- # أو زر سؤال
42
- # ask_btn = gr.Button("اطرح السؤال")
43
- # ask_btn.click(fn=answer_question, inputs=question_input, outputs=[answer_output, sources_output, logs_box2])
44
 
45
  demo.launch()
 
1
  import gradio as gr
2
+ import os
3
  from rag_pipeline import RAGPipeline
4
+ from PyPDF2 import PdfReader
5
+ import docx
6
 
7
  logs = []
8
 
9
  def logger(message):
10
  logs.append(message)
11
+ if len(logs) > 100:
12
  logs.pop(0)
13
 
 
14
  rag = RAGPipeline(logger=logger)
15
 
16
+ def extract_text_from_file(file):
17
+ text = ""
18
+ if file.name.endswith(".pdf"):
19
+ reader = PdfReader(file.name)
20
+ for page in reader.pages:
21
+ text += page.extract_text() + "\n"
22
+ elif file.name.endswith(".docx"):
23
+ doc = docx.Document(file.name)
24
+ for para in doc.paragraphs:
25
+ text += para.text + "\n"
26
+ elif file.name.endswith(".txt"):
27
+ with open(file.name, "r", encoding="utf-8") as f:
28
+ text = f.read()
29
+ else:
30
+ logger(f"[RAG] تنسيق الملف غير مدعوم: {file.name}")
31
+ return text
32
+
33
+ def process_documents(files):
34
+ all_text = ""
35
+ for file in files:
36
+ all_text += extract_text_from_file(file) + "\n"
37
+ chunks = [t.strip() for t in all_text.split("\n") if t.strip()]
38
  msg = rag.build_index(chunks)
39
  return msg, "\n".join(logs)
40
 
 
43
  return answer, "\n".join(sources), "\n".join(logs)
44
 
45
  with gr.Blocks() as demo:
46
+ gr.Markdown("## 🧠 نظام RAG باللغة العربية")
47
+
48
+ with gr.Tab("📄 رفع المستندات وبناء الفهرس"):
49
+ file_input = gr.File(label="اختر ملفات المستندات", file_types=[".pdf", ".docx", ".txt"], file_count="multiple")
50
+ build_btn = gr.Button("🔨 بناء الفهرس")
51
+ build_status = gr.Textbox(label="حالة بناء الفهرس", lines=2)
52
+ logs_box = gr.Textbox(label="📜 سجلات التشغيل", lines=10)
53
+
54
+ build_btn.click(fn=process_documents, inputs=file_input, outputs=[build_status, logs_box])
55
+
56
+ with gr.Tab("طرح الأسئلة"):
57
+ question_input = gr.Textbox(label="اكتب سؤالك هنا", lines=2)
58
+ ask_btn = gr.Button("📩 إرسال السؤال")
59
+ answer_output = gr.Textbox(label="الإجابة", lines=5)
60
+ sources_output = gr.Textbox(label="المراجع المستخدمة", lines=5)
61
+ logs_box2 = gr.Textbox(label="📜 سجلات التشغيل", lines=10)
62
+
63
+ ask_btn.click(fn=answer_question, inputs=question_input, outputs=[answer_output, sources_output, logs_box2])
 
 
64
 
65
  demo.launch()