File size: 1,971 Bytes
9453eac
5c3f634
4abc449
 
 
9453eac
4abc449
 
9453eac
4e751ce
 
 
 
 
 
 
9453eac
4abc449
 
dd4c7df
4abc449
 
 
4e751ce
768d260
4e751ce
768d260
4e751ce
 
 
 
 
2bf547d
4e751ce
 
9453eac
 
 
4e751ce
 
279ab91
 
4e751ce
 
2bf547d
4e751ce
 
 
 
9453eac
 
 
4abc449
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr
import easyocr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from PIL import Image
import numpy as np

# Initialize EasyOCR for Persian
reader = easyocr.Reader(['fa'])

# Load NLP model - استفاده از مدل جایگزین
model_name = "HooshvareLab/bert-fa-base-uncased"
try:
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
except Exception as e:
    raise gr.Error(f"خطا در بارگذاری مدل زبانی: {str(e)}")

def run_ocr(image):
    """استخراج متن از تصویر با EasyOCR"""
    try:
        if isinstance(image, Image.Image):
            image = np.array(image)
        results = reader.readtext(image)
        return " ".join([result[1] for result in results]) if results else "متنی یافت نشد!"
    except Exception as e:
        return f"خطا در OCR: {str(e)}"

def process_text(text):
    """پردازش متن (ساده‌شده)"""
    # در این نسخه ساده، فقط متن را برمی‌گردانیم
    # می‌توانید پردازش‌های دیگر اضافه کنید
    return text if text != "متنی یافت نشد!" else text

with gr.Blocks(title="OCR فارسی") as app:
    gr.Markdown("## استخراج متن فارسی از تصاویر")
    
    with gr.Row():
        with gr.Column():
            img_input = gr.Image(label="تصویر ورودی", type="pil")
            btn = gr.Button("پردازش")
        
        with gr.Column():
            ocr_output = gr.Textbox(label="متن استخراج شده")
            # nl_output = gr.Textbox(label="متن پردازش شده")  # غیرفعال شده
    
    btn.click(
        fn=lambda x: (run_ocr(x), process_text(run_ocr(x))),
        inputs=img_input,
        outputs=[ocr_output]  # فقط خروجی OCR نمایش داده می‌شود
    )

if __name__ == "__main__":
    app.launch()