Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
3 |
-
from transformers import pipeline
|
4 |
import numpy as np
|
5 |
from typing import Tuple
|
6 |
|
7 |
-
## 1. تنظیمات اولیه و مدلها
|
8 |
-
# ----------------------------------
|
9 |
class OCRProcessor:
|
10 |
def __init__(self):
|
11 |
self.reader = easyocr.Reader(['fa'])
|
12 |
-
|
13 |
def extract_text(self, image: np.ndarray) -> str:
|
14 |
-
"""استخراج متن از تصویر با EasyOCR"""
|
15 |
try:
|
16 |
results = self.reader.readtext(image, detail=0, paragraph=True)
|
17 |
return "\n".join(results) if results else ""
|
@@ -25,57 +21,28 @@ class TextPostProcessor:
|
|
25 |
'۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4',
|
26 |
'۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9'
|
27 |
}
|
28 |
-
try:
|
29 |
-
# استفاده از مدل instruction-tuned مناسب بازنویسی
|
30 |
-
self.llm = pipeline(
|
31 |
-
"text2text-generation",
|
32 |
-
model="ParsiAI/gpt2-medium-fa-instruction",
|
33 |
-
tokenizer="ParsiAI/gpt2-medium-fa-instruction"
|
34 |
-
)
|
35 |
-
except Exception as e:
|
36 |
-
print("خطا در بارگذاری مدل بازنویسی:", e)
|
37 |
-
self.llm = None
|
38 |
|
39 |
def preprocess(self, text: str) -> str:
|
40 |
-
"""نرمالسازی ساده متن"""
|
41 |
if not text:
|
42 |
return ""
|
43 |
for old, new in self.replacements.items():
|
44 |
text = text.replace(old, new)
|
45 |
return " ".join(text.split())
|
46 |
|
47 |
-
def enhance_with_llm(self, text: str) -> str:
|
48 |
-
"""بازنویسی متن با مدل instruction-tuned"""
|
49 |
-
if not text or not self.llm:
|
50 |
-
return text
|
51 |
-
prompt = f"متن زیر را کامل کن:\n{text}"
|
52 |
-
try:
|
53 |
-
result = self.llm(prompt, max_length=256, num_return_sequences=1)
|
54 |
-
enhanced_text = result[0]['generated_text'].strip()
|
55 |
-
# اگر بازنویسی مدل بیمعنا یا کوتاه بود، همان متن را برگردان
|
56 |
-
if len(enhanced_text) < 8:
|
57 |
-
return text
|
58 |
-
return enhanced_text
|
59 |
-
except Exception as e:
|
60 |
-
print("خطا در بازنویسی:", e)
|
61 |
-
return text
|
62 |
-
|
63 |
-
## 2. پایپلاین اصلی
|
64 |
def full_processing(image: np.ndarray) -> Tuple[str, str]:
|
65 |
try:
|
66 |
ocr_text = OCRProcessor().extract_text(image)
|
67 |
post_processor = TextPostProcessor()
|
68 |
cleaned_text = post_processor.preprocess(ocr_text)
|
69 |
-
|
70 |
-
return cleaned_text,
|
71 |
except Exception as e:
|
72 |
return f"خطا: {str(e)}", ""
|
73 |
|
74 |
-
|
75 |
-
with gr.Blocks(title="پایپلاین OCR و بازنویسی متن فارسی") as app:
|
76 |
gr.Markdown("""
|
77 |
-
#
|
78 |
-
|
79 |
""")
|
80 |
with gr.Row():
|
81 |
with gr.Column():
|
@@ -84,8 +51,8 @@ with gr.Blocks(title="پایپلاین OCR و بازنویسی متن فارسی
|
|
84 |
with gr.Column():
|
85 |
with gr.Tab("متن استخراج شده"):
|
86 |
raw_output = gr.Textbox(label="متن استخراج شده")
|
87 |
-
with gr.Tab("متن
|
88 |
-
enhanced_output = gr.Textbox(label="متن
|
89 |
with gr.Tab("پیشنمایش تصویر"):
|
90 |
img_preview = gr.Image(label="", interactive=False)
|
91 |
|
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
|
|
3 |
import numpy as np
|
4 |
from typing import Tuple
|
5 |
|
|
|
|
|
6 |
class OCRProcessor:
|
7 |
def __init__(self):
|
8 |
self.reader = easyocr.Reader(['fa'])
|
9 |
+
|
10 |
def extract_text(self, image: np.ndarray) -> str:
|
|
|
11 |
try:
|
12 |
results = self.reader.readtext(image, detail=0, paragraph=True)
|
13 |
return "\n".join(results) if results else ""
|
|
|
21 |
'۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4',
|
22 |
'۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9'
|
23 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def preprocess(self, text: str) -> str:
|
|
|
26 |
if not text:
|
27 |
return ""
|
28 |
for old, new in self.replacements.items():
|
29 |
text = text.replace(old, new)
|
30 |
return " ".join(text.split())
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def full_processing(image: np.ndarray) -> Tuple[str, str]:
|
33 |
try:
|
34 |
ocr_text = OCRProcessor().extract_text(image)
|
35 |
post_processor = TextPostProcessor()
|
36 |
cleaned_text = post_processor.preprocess(ocr_text)
|
37 |
+
# بازنویسی واقعی انجام نمیشود، فقط متن تمیز میشود
|
38 |
+
return cleaned_text, cleaned_text
|
39 |
except Exception as e:
|
40 |
return f"خطا: {str(e)}", ""
|
41 |
|
42 |
+
with gr.Blocks(title="پایپلاین OCR و تصحیح متن فارسی") as app:
|
|
|
43 |
gr.Markdown("""
|
44 |
+
# استخراج و تصحیح متن فارسی از تصویر
|
45 |
+
فقط متن را تصحیح (نرمالسازی) میکند و بازنویسی واقعی انجام نمیدهد.
|
46 |
""")
|
47 |
with gr.Row():
|
48 |
with gr.Column():
|
|
|
51 |
with gr.Column():
|
52 |
with gr.Tab("متن استخراج شده"):
|
53 |
raw_output = gr.Textbox(label="متن استخراج شده")
|
54 |
+
with gr.Tab("متن نهایی (تصحیح شده)"):
|
55 |
+
enhanced_output = gr.Textbox(label="متن نهایی")
|
56 |
with gr.Tab("پیشنمایش تصویر"):
|
57 |
img_preview = gr.Image(label="", interactive=False)
|
58 |
|