Download app.py from ethancoen/imageOCR: direct link, hf CLI and curl.
- Browser
- Download file 988 Bytes
-
https://huggingface.co/spaces/ethancoen/imageOCR/resolve/main/app.py
- Command line
-
hf download hf://spaces/ethancoen/imageOCR/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ethancoen/imageOCR/resolve/main/app.py
988 Bytes
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor | |
| from PIL import Image | |
| import torch | |
| # Load Qwen-VL model | |
| model_id = "Qwen/Qwen-VL-Chat" | |
| processor = AutoProcessor.from_pretrained(model_id) | |
| model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", trust_remote_code=True).eval() | |
| tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) | |
| def recognize_handwriting(img): | |
| prompt = "<|im_start|>user\nWhat is written in the image?\n<|im_end|>\n<|im_start|>assistant\n" | |
| inputs = processor(text=prompt, images=img, return_tensors="pt").to(model.device) | |
| outputs = model.generate(**inputs, max_new_tokens=128) | |
| result = processor.batch_decode(outputs, skip_special_tokens=True)[0] | |
| return result | |
| gr.Interface(fn=recognize_handwriting, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="Handwriting Recognition with Qwen-VL").launch() | |