|
""" |
|
File: module_ocr.py |
|
Description: Use a vision language model for Optical Character Recognition (OCR) tasks. |
|
Author: Didier Guillevic |
|
Date: 2025-04-06 |
|
""" |
|
|
|
import gradio as gr |
|
import ocr |
|
|
|
|
|
|
|
|
|
def process(input_file: str): |
|
"""Process given file with OCR." |
|
""" |
|
return ocr.process_file(input_file) |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
|
|
|
with gr.Row(): |
|
input_file = gr.File(label="Upload a PDF file", scale=1) |
|
output_text = gr.Textbox(label="OCR output", scale=2) |
|
|
|
|
|
with gr.Row(): |
|
ocr_btn = gr.Button(value="OCR", variant="primary") |
|
clear_btn = gr.Button("Clear", variant="secondary") |
|
|
|
|
|
with gr.Accordion("Examples", open=False): |
|
examples = gr.Examples( |
|
[ |
|
['./scanned_doc.pdf',], |
|
['./passport_jp.png',] |
|
], |
|
inputs=[input_file,], |
|
outputs=[output_text,], |
|
fn=process, |
|
cache_examples=False, |
|
label="Examples" |
|
) |
|
|
|
|
|
ocr_btn.click( |
|
fn=process, |
|
inputs=[input_file,], |
|
outputs=[output_text,] |
|
) |
|
clear_btn.click( |
|
fn=lambda : (None, ''), |
|
inputs=[], |
|
outputs=[input_file, output_text] |
|
) |
|
|
|
if __name__ == '__main__': |
|
demo.launch() |
|
|