Spaces:
Paused
Paused
Configure space to use PaddlePaddle/PaddleOCR-VL model
Browse files
app.py
CHANGED
|
@@ -1,7 +1,43 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoModel, AutoTokenizer
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
# Load PaddleOCR-VL model
|
| 8 |
+
model_name = "PaddlePaddle/PaddleOCR-VL"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
+
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
|
| 11 |
|
| 12 |
+
if torch.cuda.is_available():
|
| 13 |
+
model = model.cuda()
|
| 14 |
+
|
| 15 |
+
@spaces.GPU
|
| 16 |
+
def ocr_inference(image):
|
| 17 |
+
"""
|
| 18 |
+
Perform OCR on the input image using PaddleOCR-VL
|
| 19 |
+
"""
|
| 20 |
+
if image is None:
|
| 21 |
+
return "Please upload an image."
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# Convert to PIL Image if needed
|
| 25 |
+
if not isinstance(image, Image.Image):
|
| 26 |
+
image = Image.fromarray(image)
|
| 27 |
+
|
| 28 |
+
# Run OCR inference
|
| 29 |
+
result = model.chat(tokenizer, image, "Extract all text from this image.")
|
| 30 |
+
return result
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error during OCR: {str(e)}"
|
| 33 |
+
|
| 34 |
+
# Create Gradio interface
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=ocr_inference,
|
| 37 |
+
inputs=gr.Image(type="pil", label="Upload Image for OCR"),
|
| 38 |
+
outputs=gr.Textbox(label="Extracted Text"),
|
| 39 |
+
title="PaddleOCR-VL OCR Demo",
|
| 40 |
+
description="Upload an image to extract text using PaddlePaddle/PaddleOCR-VL model"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|