Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,24 @@ import pytesseract
|
|
6 |
from PIL import Image
|
7 |
import io
|
8 |
import numpy as np
|
|
|
|
|
9 |
|
10 |
def extract_text_from_pdf(pdf_file):
|
11 |
try:
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
full_text = ""
|
15 |
has_ocr_processed = False
|
16 |
|
@@ -26,11 +39,18 @@ def extract_text_from_pdf(pdf_file):
|
|
26 |
# تبدیل صفحه به تصویر با وضوح بالا
|
27 |
mat = fitz.Matrix(300/72, 300/72) # وضوح 300 DPI
|
28 |
pix = page.get_pixmap(matrix=mat)
|
29 |
-
img_data = pix.tobytes("
|
30 |
|
31 |
# استفاده از OCR برای استخراج متن از تصویر
|
32 |
image = Image.open(io.BytesIO(img_data))
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
text = ocr_text
|
35 |
has_ocr_processed = True
|
36 |
|
@@ -39,6 +59,9 @@ def extract_text_from_pdf(pdf_file):
|
|
39 |
|
40 |
doc.close()
|
41 |
|
|
|
|
|
|
|
42 |
# پردازش متن برای زبانهای راستبهچپ
|
43 |
try:
|
44 |
reshaped_text = arabic_reshaper.reshape(full_text)
|
@@ -48,10 +71,17 @@ def extract_text_from_pdf(pdf_file):
|
|
48 |
bidi_text = "[⚠️ برخی صفحات با OCR پردازش شدند]\n\n" + bidi_text
|
49 |
|
50 |
return bidi_text
|
51 |
-
except:
|
|
|
52 |
return full_text
|
53 |
|
54 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
return f"خطا در پردازش فایل: {str(e)}"
|
56 |
|
57 |
# ایجاد رابط Gradio
|
@@ -60,12 +90,21 @@ with gr.Blocks(title="PDF Text Extractor with OCR") as demo:
|
|
60 |
gr.Markdown("با قابلیت پردازش OCR برای PDFهای تصویری")
|
61 |
|
62 |
with gr.Row():
|
63 |
-
pdf_input = gr.File(
|
|
|
|
|
|
|
|
|
64 |
|
65 |
extract_btn = gr.Button("🔄 استخراج متن")
|
66 |
|
67 |
with gr.Row():
|
68 |
-
text_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
gr.Markdown("""
|
71 |
**⚠️ توجه:**
|
@@ -82,4 +121,4 @@ with gr.Blocks(title="PDF Text Extractor with OCR") as demo:
|
|
82 |
)
|
83 |
|
84 |
if __name__ == "__main__":
|
85 |
-
demo.launch()
|
|
|
6 |
from PIL import Image
|
7 |
import io
|
8 |
import numpy as np
|
9 |
+
import tempfile
|
10 |
+
import os
|
11 |
|
12 |
def extract_text_from_pdf(pdf_file):
|
13 |
try:
|
14 |
+
# ایجاد فایل موقت برای PDF
|
15 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
|
16 |
+
if hasattr(pdf_file, 'read'):
|
17 |
+
# اگر فایل قابل خواندن است
|
18 |
+
tmp_file.write(pdf_file.read())
|
19 |
+
else:
|
20 |
+
# اگر از Gradio File component است
|
21 |
+
tmp_file.write(pdf_file)
|
22 |
+
|
23 |
+
tmp_path = tmp_file.name
|
24 |
+
|
25 |
+
# باز کردن فایل PDF از مسیر
|
26 |
+
doc = fitz.open(tmp_path)
|
27 |
full_text = ""
|
28 |
has_ocr_processed = False
|
29 |
|
|
|
39 |
# تبدیل صفحه به تصویر با وضوح بالا
|
40 |
mat = fitz.Matrix(300/72, 300/72) # وضوح 300 DPI
|
41 |
pix = page.get_pixmap(matrix=mat)
|
42 |
+
img_data = pix.tobytes("png")
|
43 |
|
44 |
# استفاده از OCR برای استخراج متن از تصویر
|
45 |
image = Image.open(io.BytesIO(img_data))
|
46 |
+
|
47 |
+
# تنظیمات OCR برای زبانهای مختلف
|
48 |
+
custom_config = r'--oem 3 --psm 6'
|
49 |
+
ocr_text = pytesseract.image_to_string(
|
50 |
+
image,
|
51 |
+
lang='fas+ara+eng',
|
52 |
+
config=custom_config
|
53 |
+
)
|
54 |
text = ocr_text
|
55 |
has_ocr_processed = True
|
56 |
|
|
|
59 |
|
60 |
doc.close()
|
61 |
|
62 |
+
# حذف فایل موقت
|
63 |
+
os.unlink(tmp_path)
|
64 |
+
|
65 |
# پردازش متن برای زبانهای راستبهچپ
|
66 |
try:
|
67 |
reshaped_text = arabic_reshaper.reshape(full_text)
|
|
|
71 |
bidi_text = "[⚠️ برخی صفحات با OCR پردازش شدند]\n\n" + bidi_text
|
72 |
|
73 |
return bidi_text
|
74 |
+
except Exception as proc_error:
|
75 |
+
print(f"خطا در پردازش متن: {proc_error}")
|
76 |
return full_text
|
77 |
|
78 |
except Exception as e:
|
79 |
+
# حذف فایل موقت در صورت خطا
|
80 |
+
try:
|
81 |
+
if 'tmp_path' in locals():
|
82 |
+
os.unlink(tmp_path)
|
83 |
+
except:
|
84 |
+
pass
|
85 |
return f"خطا در پردازش فایل: {str(e)}"
|
86 |
|
87 |
# ایجاد رابط Gradio
|
|
|
90 |
gr.Markdown("با قابلیت پردازش OCR برای PDFهای تصویری")
|
91 |
|
92 |
with gr.Row():
|
93 |
+
pdf_input = gr.File(
|
94 |
+
label="فایل PDF را انتخاب کنید",
|
95 |
+
file_types=[".pdf"],
|
96 |
+
type="bytes" # استفاده از bytes به جای file path
|
97 |
+
)
|
98 |
|
99 |
extract_btn = gr.Button("🔄 استخراج متن")
|
100 |
|
101 |
with gr.Row():
|
102 |
+
text_output = gr.Textbox(
|
103 |
+
label="متن استخراج شده",
|
104 |
+
lines=20,
|
105 |
+
interactive=False,
|
106 |
+
show_copy_button=True
|
107 |
+
)
|
108 |
|
109 |
gr.Markdown("""
|
110 |
**⚠️ توجه:**
|
|
|
121 |
)
|
122 |
|
123 |
if __name__ == "__main__":
|
124 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|