Spaces:
Runtime error
Runtime error
Download app.py from sriusha/Extraction: direct link, hf CLI and curl.
- Browser
- Download file 879 Bytes
-
https://huggingface.co/spaces/sriusha/Extraction/resolve/main/app.py
- Command line
-
hf download hf://spaces/sriusha/Extraction/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sriusha/Extraction/resolve/main/app.py
879 Bytes
| import gradio as gr | |
| from transformers import TrOCRProcessor, VisionEncoderDecoderModel | |
| from PIL import Image | |
| # Load processor + model | |
| processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten") | |
| model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten") | |
| def ocr_image(image): | |
| # Convert to PIL Image if uploaded | |
| if not isinstance(image, Image.Image): | |
| image = Image.fromarray(image) | |
| pixel_values = processor(images=image, return_tensors="pt").pixel_values | |
| generated_ids = model.generate(pixel_values) | |
| text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
| return text | |
| demo = gr.Interface( | |
| fn=ocr_image, | |
| inputs=gr.Image(type="pil"), # Image upload box | |
| outputs=gr.Textbox(label="Extracted Text") # Show extracted text | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |