Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
3 |
-
from transformers import
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
|
7 |
# Initialize EasyOCR for Persian
|
8 |
reader = easyocr.Reader(['fa'])
|
9 |
|
10 |
-
#
|
11 |
-
model_name = "persiannlp/mt5-small-parsinlu-opus-translation" # مدل مناسب برای پردازش متن
|
12 |
try:
|
13 |
-
|
14 |
-
|
15 |
except Exception as e:
|
16 |
-
|
17 |
|
18 |
def run_ocr(image):
|
19 |
"""استخراج متن از تصویر با EasyOCR"""
|
@@ -26,19 +25,20 @@ def run_ocr(image):
|
|
26 |
return f"خطا در OCR: {str(e)}"
|
27 |
|
28 |
def process_text(text):
|
29 |
-
"""پردازش متن
|
30 |
if text == "متنی یافت نشد!":
|
31 |
return text
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
with gr.Blocks(title="OCR فارسی
|
41 |
-
gr.Markdown("##
|
42 |
|
43 |
with gr.Row():
|
44 |
with gr.Column():
|
@@ -47,7 +47,7 @@ with gr.Blocks(title="OCR فارسی با پردازش متن") as app:
|
|
47 |
|
48 |
with gr.Column():
|
49 |
ocr_output = gr.Textbox(label="متن استخراج شده")
|
50 |
-
processed_output = gr.Textbox(label="متن پردازش شده")
|
51 |
|
52 |
btn.click(
|
53 |
fn=lambda x: (run_ocr(x), process_text(run_ocr(x))),
|
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
3 |
+
from transformers import pipeline
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
|
7 |
# Initialize EasyOCR for Persian
|
8 |
reader = easyocr.Reader(['fa'])
|
9 |
|
10 |
+
# Initialize text processing pipeline
|
|
|
11 |
try:
|
12 |
+
# استفاده از مدل محلی یا مدلهای عمومی
|
13 |
+
text_processor = pipeline("text-generation", model="gpt2") # مدل جایگزین
|
14 |
except Exception as e:
|
15 |
+
text_processor = None
|
16 |
|
17 |
def run_ocr(image):
|
18 |
"""استخراج متن از تصویر با EasyOCR"""
|
|
|
25 |
return f"خطا در OCR: {str(e)}"
|
26 |
|
27 |
def process_text(text):
|
28 |
+
"""پردازش ساده متن"""
|
29 |
if text == "متنی یافت نشد!":
|
30 |
return text
|
31 |
+
|
32 |
+
# اگر پردازشگر متن وجود داشت از آن استفاده کن
|
33 |
+
if text_processor:
|
34 |
+
try:
|
35 |
+
return text_processor(text, max_length=50)[0]['generated_text']
|
36 |
+
except:
|
37 |
+
return text # اگر خطا رخ داد متن اصلی را برگردان
|
38 |
+
return text # اگر پردازشگر متن وجود نداشت
|
39 |
|
40 |
+
with gr.Blocks(title="سیستم OCR فارسی") as app:
|
41 |
+
gr.Markdown("## استخراج متن فارسی از تصاویر")
|
42 |
|
43 |
with gr.Row():
|
44 |
with gr.Column():
|
|
|
47 |
|
48 |
with gr.Column():
|
49 |
ocr_output = gr.Textbox(label="متن استخراج شده")
|
50 |
+
processed_output = gr.Textbox(label="متن پردازش شده", visible=False) # غیرفعال شده
|
51 |
|
52 |
btn.click(
|
53 |
fn=lambda x: (run_ocr(x), process_text(run_ocr(x))),
|