File size: 2,415 Bytes
4028152
2d575e5
4028152
2d575e5
 
72b0fdc
 
 
 
 
2d575e5
72b0fdc
 
 
 
2d575e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b0fdc
 
 
 
 
 
4028152
 
2d575e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b0fdc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gradio as gr
import os
from rag_pipeline import RAGPipeline
from PyPDF2 import PdfReader
import docx

logs = []

def logger(message):
    logs.append(message)
    if len(logs) > 100:
        logs.pop(0)

rag = RAGPipeline(logger=logger)

def extract_text_from_file(file):
    text = ""
    if file.name.endswith(".pdf"):
        reader = PdfReader(file.name)
        for page in reader.pages:
            text += page.extract_text() + "\n"
    elif file.name.endswith(".docx"):
        doc = docx.Document(file.name)
        for para in doc.paragraphs:
            text += para.text + "\n"
    elif file.name.endswith(".txt"):
        with open(file.name, "r", encoding="utf-8") as f:
            text = f.read()
    else:
        logger(f"[RAG] تنسيق الملف غير مدعوم: {file.name}")
    return text

def process_documents(files):
    all_text = ""
    for file in files:
        all_text += extract_text_from_file(file) + "\n"
    chunks = [t.strip() for t in all_text.split("\n") if t.strip()]
    msg = rag.build_index(chunks)
    return msg, "\n".join(logs)

def answer_question(question):
    answer, sources = rag.generate_answer(question)
    return answer, "\n".join(sources), "\n".join(logs)

with gr.Blocks() as demo:
    gr.Markdown("## 🧠 نظام RAG باللغة العربية")

    with gr.Tab("📄 رفع المستندات وبناء الفهرس"):
        file_input = gr.File(label="اختر ملفات المستندات", file_types=[".pdf", ".docx", ".txt"], file_count="multiple")
        build_btn = gr.Button("🔨 بناء الفهرس")
        build_status = gr.Textbox(label="حالة بناء الفهرس", lines=2)
        logs_box = gr.Textbox(label="📜 سجلات التشغيل", lines=10)

        build_btn.click(fn=process_documents, inputs=file_input, outputs=[build_status, logs_box])

    with gr.Tab("❓ طرح الأسئلة"):
        question_input = gr.Textbox(label="اكتب سؤالك هنا", lines=2)
        ask_btn = gr.Button("📩 إرسال السؤال")
        answer_output = gr.Textbox(label="الإجابة", lines=5)
        sources_output = gr.Textbox(label="المراجع المستخدمة", lines=5)
        logs_box2 = gr.Textbox(label="📜 سجلات التشغيل", lines=10)

        ask_btn.click(fn=answer_question, inputs=question_input, outputs=[answer_output, sources_output, logs_box2])

demo.launch()