Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
model_name = "deepseek-ai/DeepSeek-OCR"
|
| 8 |
+
|
| 9 |
+
print("Loading tokenizer...")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 11 |
+
|
| 12 |
+
print("Loading model...")
|
| 13 |
+
model = AutoModel.from_pretrained(
|
| 14 |
+
model_name,
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
+
_attn_implementation="flash_attention_2",
|
| 17 |
+
use_safetensors=True
|
| 18 |
+
).cuda().to(torch.bfloat16).eval()
|
| 19 |
+
|
| 20 |
+
def ocr_infer(image):
|
| 21 |
+
# Save temporary image
|
| 22 |
+
image.save("input.png")
|
| 23 |
+
|
| 24 |
+
# Standard OCR prompt
|
| 25 |
+
prompt = "<image>\nFree OCR."
|
| 26 |
+
|
| 27 |
+
result = model.infer(
|
| 28 |
+
tokenizer,
|
| 29 |
+
prompt=prompt,
|
| 30 |
+
image_file="input.png",
|
| 31 |
+
output_path=".",
|
| 32 |
+
base_size=1024,
|
| 33 |
+
image_size=640,
|
| 34 |
+
crop_mode=True,
|
| 35 |
+
save_results=False
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
return result["text"]
|
| 39 |
+
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=ocr_infer,
|
| 42 |
+
inputs=gr.Image(type="pil"),
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="DeepSeek-OCR API (HuggingFace)",
|
| 45 |
+
description="Upload an image and get OCR text using DeepSeek-OCR"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
demo.launch()
|