OmidSakaki commited on
Commit
279ab91
·
verified ·
1 Parent(s): fc0e7b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -24
app.py CHANGED
@@ -6,21 +6,22 @@ import os
6
 
7
  # --- مدل‌ها ---
8
  try:
9
- model_name = "persiannlp/mt5-small-parsinlu-opus-translation"
 
10
 
11
  ocr_model = PaddleOCR(lang='fa', use_textline_orientation=True)
12
  tokenizer = AutoTokenizer.from_pretrained(model_name)
13
  nlp_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
14
 
15
  except Exception as e:
16
- raise gr.Error(f"خطا در بارگذاری مدل‌ها: لطفاً از موجود بودن مدل اطمینان حاصل کنید. خطا: {str(e)}")
17
 
18
  # --- توابع پردازش ---
19
  def run_ocr(image):
20
- image_path = image.name # مسیر فایل موقت
21
  result = ocr_model.ocr(image_path, cls=True)
22
  texts = [line[1][0] for line in result[0]] if result else []
23
- os.remove(image_path) # حذف فایل موقت
24
  return " ".join(texts)
25
 
26
  def postprocess_text(text):
@@ -28,32 +29,24 @@ def postprocess_text(text):
28
  outputs = nlp_model.generate(**inputs)
29
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
30
 
31
- def process_image(image):
32
- raw_text = run_ocr(image)
33
- processed_text = postprocess_text(raw_text) if raw_text else "متنی یافت نشد!"
34
- return raw_text, processed_text
35
-
36
- # --- رابط Gradio ---
37
- with gr.Blocks(title="OCR فارسی با پردازش NLP") as app:
38
- gr.Markdown("## 🔠 OCR فارسی + پردازش متن با مدل زبانی")
39
- gr.Markdown("متن را از تصاویر استخراج کنید و با مدل زبانی اصلاح کنید!")
40
 
41
  with gr.Row():
42
- image_input = gr.Image(type="filepath", label="تصویر ورودی")
43
  with gr.Column():
44
- raw_text_output = gr.Textbox(label="متن خام (OCR)")
45
- processed_text_output = gr.Textbox(label="متن پردازش‌شده (NLP)")
 
 
 
 
46
 
47
- submit_btn = gr.Button("پردازش تصویر")
48
- submit_btn.click(
49
- fn=process_image,
50
  inputs=image_input,
51
- outputs=[raw_text_output, processed_text_output]
52
  )
53
 
54
- gr.Markdown("---")
55
- gr.Markdown("### راهنما:\n1. تصویری حاوی متن فارسی آپلود کنید.\n2. روی دکمه پردازش کلیک کنید.")
56
-
57
- # اجرای برنامه
58
  if __name__ == "__main__":
59
  app.launch()
 
6
 
7
  # --- مدل‌ها ---
8
  try:
9
+ # مدل جدید تست شده و کارآمد
10
+ model_name = "m3hrdadfi/mt5-small-parsinlu-grammar-correction"
11
 
12
  ocr_model = PaddleOCR(lang='fa', use_textline_orientation=True)
13
  tokenizer = AutoTokenizer.from_pretrained(model_name)
14
  nlp_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
15
 
16
  except Exception as e:
17
+ raise gr.Error(f"خطا در بارگذاری مدل‌ها: لطفاً این خطا را به توسعه دهنده گزارش دهید. خطا: {str(e)}")
18
 
19
  # --- توابع پردازش ---
20
  def run_ocr(image):
21
+ image_path = image.name
22
  result = ocr_model.ocr(image_path, cls=True)
23
  texts = [line[1][0] for line in result[0]] if result else []
24
+ os.remove(image_path)
25
  return " ".join(texts)
26
 
27
  def postprocess_text(text):
 
29
  outputs = nlp_model.generate(**inputs)
30
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
31
 
32
+ # --- رابط کاربری ---
33
+ with gr.Blocks() as app:
34
+ gr.Markdown("## سیستم OCR فارسی با پردازش پیشرفته متن")
 
 
 
 
 
 
35
 
36
  with gr.Row():
 
37
  with gr.Column():
38
+ image_input = gr.Image(type="filepath", label="تصویر ورودی")
39
+ process_btn = gr.Button("پردازش تصویر")
40
+
41
+ with gr.Column():
42
+ raw_output = gr.Textbox(label="متن استخراج شده")
43
+ processed_output = gr.Textbox(label="متن پردازش شده")
44
 
45
+ process_btn.click(
46
+ fn=lambda img: (run_ocr(img), postprocess_text(run_ocr(img))),
 
47
  inputs=image_input,
48
+ outputs=[raw_output, processed_output]
49
  )
50
 
 
 
 
 
51
  if __name__ == "__main__":
52
  app.launch()