Upload 2 files
Browse files- app.py +81 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pdfplumber
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
+
from sentence_transformers import SentenceTransformer, util
|
| 5 |
+
import faiss
|
| 6 |
+
import numpy as np
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
# تابع استخراج متن از PDF
|
| 10 |
+
def extract_text_from_pdf(file):
|
| 11 |
+
text = ""
|
| 12 |
+
with pdfplumber.open(file.name) as pdf:
|
| 13 |
+
for page in pdf.pages:
|
| 14 |
+
page_text = page.extract_text()
|
| 15 |
+
if page_text:
|
| 16 |
+
text += page_text + "\n"
|
| 17 |
+
return text
|
| 18 |
+
|
| 19 |
+
# تابع پاکسازی متن
|
| 20 |
+
def clean_text(text):
|
| 21 |
+
text = re.sub(r'\n+', '\n', text)
|
| 22 |
+
text = re.sub(r'[ \t]+', ' ', text)
|
| 23 |
+
return text.strip()
|
| 24 |
+
|
| 25 |
+
# تابع تقسیمبندی متن به بخشهای معنادار
|
| 26 |
+
def chunk_text(text, chunk_size=500, overlap=50):
|
| 27 |
+
sentences = re.split(r'(?<=[.!?]) +', text)
|
| 28 |
+
chunks = []
|
| 29 |
+
current_chunk = ""
|
| 30 |
+
for sentence in sentences:
|
| 31 |
+
if len(current_chunk) + len(sentence) <= chunk_size:
|
| 32 |
+
current_chunk += " " + sentence
|
| 33 |
+
else:
|
| 34 |
+
chunks.append(current_chunk.strip())
|
| 35 |
+
current_chunk = sentence
|
| 36 |
+
if current_chunk:
|
| 37 |
+
chunks.append(current_chunk.strip())
|
| 38 |
+
return chunks
|
| 39 |
+
|
| 40 |
+
# بارگذاری مدل تعبیه
|
| 41 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 42 |
+
|
| 43 |
+
# تابع اصلی برای پاسخ به پرسشها
|
| 44 |
+
def answer_question(pdf_file, question):
|
| 45 |
+
# استخراج و پاکسازی متن
|
| 46 |
+
raw_text = extract_text_from_pdf(pdf_file)
|
| 47 |
+
cleaned_text = clean_text(raw_text)
|
| 48 |
+
|
| 49 |
+
# تقسیمبندی متن
|
| 50 |
+
chunks = chunk_text(cleaned_text)
|
| 51 |
+
|
| 52 |
+
# تعبیه بخشها
|
| 53 |
+
embeddings = model.encode(chunks)
|
| 54 |
+
|
| 55 |
+
# ساخت ایندکس FAISS
|
| 56 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 57 |
+
index.add(np.array(embeddings))
|
| 58 |
+
|
| 59 |
+
# تعبیه پرسش
|
| 60 |
+
question_embedding = model.encode([question])
|
| 61 |
+
|
| 62 |
+
# جستجوی نزدیکترین بخشها
|
| 63 |
+
D, I = index.search(np.array(question_embedding), k=3)
|
| 64 |
+
|
| 65 |
+
# جمعآوری پاسخها
|
| 66 |
+
answers = [chunks[i] for i in I[0]]
|
| 67 |
+
return "\n\n---\n\n".join(answers)
|
| 68 |
+
|
| 69 |
+
# رابط کاربری Gradio
|
| 70 |
+
iface = gr.Interface(
|
| 71 |
+
fn=answer_question,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.File(label="آپلود فایل PDF", file_types=[".pdf"]),
|
| 74 |
+
gr.Textbox(label="پرسش خود را وارد کنید")
|
| 75 |
+
],
|
| 76 |
+
outputs="text",
|
| 77 |
+
title="پاسخ به پرسشها از روی فایل PDF",
|
| 78 |
+
description="یک سیستم RAG ساده برای پاسخ به پرسشها از روی محتوای فایل PDF"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5.0.0
|
| 2 |
+
transformers>=4.39.3
|
| 3 |
+
sentence-transformers>=2.7.0
|
| 4 |
+
faiss-cpu>=1.7.4
|
| 5 |
+
pdfplumber>=0.10.0
|
| 6 |
+
PyMuPDF>=1.22.0
|