Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,32 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ
|
| 3 |
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def ocr_image(image):
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
demo = gr.Interface(
|
| 14 |
fn=ocr_image,
|
| 15 |
inputs=gr.Image(type="pil"),
|
| 16 |
outputs="text",
|
| 17 |
title="DeepSeek OCR",
|
| 18 |
-
description="Upload an image and get extracted text using DeepSeek-OCR."
|
| 19 |
)
|
| 20 |
|
| 21 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1") # optional speed-up
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# important: trust_remote_code=True avoids the interactive prompt
|
| 9 |
+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 10 |
+
|
| 11 |
+
ocr = pipeline(
|
| 12 |
+
"image-to-text",
|
| 13 |
+
model="deepseek-ai/DeepSeek-OCR",
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
device_map="auto", # uses GPU if available, else CPU
|
| 16 |
+
torch_dtype=dtype,
|
| 17 |
+
)
|
| 18 |
|
| 19 |
def ocr_image(image):
|
| 20 |
+
out = ocr(image)
|
| 21 |
+
# pipeline returns a list of dicts like [{'generated_text': '...'}]
|
| 22 |
+
return out[0]["generated_text"] if out else ""
|
| 23 |
|
| 24 |
demo = gr.Interface(
|
| 25 |
fn=ocr_image,
|
| 26 |
inputs=gr.Image(type="pil"),
|
| 27 |
outputs="text",
|
| 28 |
title="DeepSeek OCR",
|
| 29 |
+
description="Upload an image and get extracted text using DeepSeek-OCR."
|
| 30 |
)
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|