DocQA_Agent / app.py
OmidSakaki's picture
Update app.py
4e751ce verified
raw
history blame
1.97 kB
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()