OmidSakaki commited on
Commit
8f46e75
·
verified ·
1 Parent(s): 3c028c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -1,19 +1,18 @@
1
  import gradio as gr
2
  import easyocr
3
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  from PIL import Image
5
  import numpy as np
6
 
7
  # Initialize EasyOCR for Persian
8
  reader = easyocr.Reader(['fa'])
9
 
10
- # Load NLP model - استفاده از مدل مناسب برای تصحیح متن
11
- model_name = "persiannlp/mt5-small-parsinlu-opus-translation" # مدل مناسب برای پردازش متن
12
  try:
13
- tokenizer = AutoTokenizer.from_pretrained(model_name)
14
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
15
  except Exception as e:
16
- raise gr.Error(f"خطا در بارگذاری مدل زبانی: {str(e)}\n\nلطفاً از مدل دیگری استفاده کنید یا با توسعه دهنده تماس بگیرید.")
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
- """پردازش متن با مدل Seq2Seq"""
30
  if text == "متنی یافت نشد!":
31
  return text
32
-
33
- try:
34
- inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
35
- outputs = model.generate(**inputs)
36
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
37
- except Exception as e:
38
- return f"خطا در پردازش متن: {str(e)}"
 
39
 
40
- with gr.Blocks(title="OCR فارسی با پردازش متن") as app:
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))),