Download app.py from athaneduc/extractor: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/athaneduc/extractor/resolve/main/app.py
- Command line
-
hf download hf://spaces/athaneduc/extractor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/athaneduc/extractor/resolve/main/app.py
1.1 kB
| import gradio as gr | |
| import torch | |
| from PIL import Image | |
| from transformers import DonutProcessor, VisionEncoderDecoderModel | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model_repo_id = "selvakumarcts/sk_invoice_receipts" | |
| # Load model and processor | |
| processor = DonutProcessor.from_pretrained(model_repo_id) | |
| model = VisionEncoderDecoderModel.from_pretrained(model_repo_id).to(device) | |
| # Inference function | |
| def infer(image): | |
| image = image.convert("RGB") | |
| pixel_values = processor(image, return_tensors="pt").pixel_values.to(device) | |
| output = model.generate(pixel_values, max_length=512) | |
| result = processor.batch_decode(output, skip_special_tokens=True)[0] | |
| return result | |
| # UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown(" # Invoice/Receipt Reader (Donut Model)") | |
| with gr.Column(): | |
| image_input = gr.Image(type="pil", label="Upload Image") | |
| run_button = gr.Button("Run") | |
| result = gr.Textbox(label="Extracted JSON", lines=10) | |
| run_button.click(fn=infer, inputs=[image_input], outputs=[result]) | |
| if __name__ == "__main__": | |
| demo.launch() | |