Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| from PIL import Image | |
| from io import BytesIO | |
| from transformers import pipeline | |
| # Load OCR pipeline once (server-side) | |
| ocr = pipeline("image-to-text", model="microsoft/trocr-base-stage1") | |
| def ocr_from_url(image_url: str): | |
| try: | |
| # Download image | |
| resp = requests.get(image_url, timeout=10) | |
| resp.raise_for_status() | |
| img = Image.open(BytesIO(resp.content)).convert("RGB") | |
| # Run OCR | |
| result = ocr(img) | |
| # pipeline returns list of dicts like [{'generated_text': 'ABC123'}] | |
| text = result[0].get("generated_text", "UNKNOWN") | |
| return text | |
| except Exception as e: | |
| return f"ERROR: {str(e)}" | |
| # Gradio interface: single text input (image URL) -> text output (plate) | |
| demo = gr.Interface(fn=ocr_from_url, | |
| inputs=gr.Textbox(label="Image URL"), | |
| outputs=gr.Textbox(label="Detected Plate"), | |
| title="License Plate OCR") | |
| if __name__ == "__main__": | |
| demo.launch() |