Spaces:
Running
on
Zero
Running
on
Zero
Update Gradio app with multiple files
Browse files- app.py +13 -7
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -5,10 +5,7 @@ from PIL import Image
|
|
| 5 |
import io
|
| 6 |
import os
|
| 7 |
from typing import Optional
|
| 8 |
-
|
| 9 |
-
# Set device
|
| 10 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
| 11 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
| 13 |
# Load model and tokenizer
|
| 14 |
model_name = "deepseek-ai/DeepSeek-OCR"
|
|
@@ -19,11 +16,10 @@ model = AutoModel.from_pretrained(
|
|
| 19 |
trust_remote_code=True,
|
| 20 |
use_safetensors=True,
|
| 21 |
)
|
| 22 |
-
model = model.eval()
|
| 23 |
-
if device == "cuda":
|
| 24 |
-
model = model.to(torch.bfloat16)
|
| 25 |
|
| 26 |
|
|
|
|
| 27 |
def ocr_process(
|
| 28 |
image_input: Image.Image,
|
| 29 |
task_type: str = "ocr",
|
|
@@ -48,6 +44,9 @@ def ocr_process(
|
|
| 48 |
return "Please upload an image first."
|
| 49 |
|
| 50 |
try:
|
|
|
|
|
|
|
|
|
|
| 51 |
# Save image temporarily
|
| 52 |
temp_image_path = "/tmp/temp_ocr_image.jpg"
|
| 53 |
image_input.save(temp_image_path)
|
|
@@ -75,9 +74,16 @@ def ocr_process(
|
|
| 75 |
if os.path.exists(temp_image_path):
|
| 76 |
os.remove(temp_image_path)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
return output if output else "No text detected in image."
|
| 79 |
|
| 80 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 81 |
return f"Error processing image: {str(e)}"
|
| 82 |
|
| 83 |
|
|
|
|
| 5 |
import io
|
| 6 |
import os
|
| 7 |
from typing import Optional
|
| 8 |
+
import spaces
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Load model and tokenizer
|
| 11 |
model_name = "deepseek-ai/DeepSeek-OCR"
|
|
|
|
| 16 |
trust_remote_code=True,
|
| 17 |
use_safetensors=True,
|
| 18 |
)
|
| 19 |
+
model = model.eval()
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
+
@spaces.GPU
|
| 23 |
def ocr_process(
|
| 24 |
image_input: Image.Image,
|
| 25 |
task_type: str = "ocr",
|
|
|
|
| 44 |
return "Please upload an image first."
|
| 45 |
|
| 46 |
try:
|
| 47 |
+
# Move model to GPU and set dtype
|
| 48 |
+
model.to("cuda")
|
| 49 |
+
model.to(torch.bfloat16)
|
| 50 |
# Save image temporarily
|
| 51 |
temp_image_path = "/tmp/temp_ocr_image.jpg"
|
| 52 |
image_input.save(temp_image_path)
|
|
|
|
| 74 |
if os.path.exists(temp_image_path):
|
| 75 |
os.remove(temp_image_path)
|
| 76 |
|
| 77 |
+
# Move model back to CPU to free GPU memory
|
| 78 |
+
model.to("cpu")
|
| 79 |
+
torch.cuda.empty_cache()
|
| 80 |
+
|
| 81 |
return output if output else "No text detected in image."
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
+
# Ensure model is moved back to CPU on error
|
| 85 |
+
model.to("cpu")
|
| 86 |
+
torch.cuda.empty_cache()
|
| 87 |
return f"Error processing image: {str(e)}"
|
| 88 |
|
| 89 |
|
requirements.txt
CHANGED
|
@@ -5,3 +5,4 @@ Pillow>=10.0.0
|
|
| 5 |
deepseek-ai
|
| 6 |
safetensors>=0.4.0
|
| 7 |
flash-attn>=2.5.0
|
|
|
|
|
|
| 5 |
deepseek-ai
|
| 6 |
safetensors>=0.4.0
|
| 7 |
flash-attn>=2.5.0
|
| 8 |
+
spaces
|