Spaces:
Sleeping
Sleeping
fix: add @spaces.GPU decorator for Hugging Face ZeroGPU compatibility
Browse files
app.py
CHANGED
|
@@ -38,6 +38,20 @@ except ImportError:
|
|
| 38 |
from config import DEVICE
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def b64_to_pil(b64_str: str) -> Image.Image:
|
| 42 |
"""Decode a data URI base64 string to a PIL Image."""
|
| 43 |
if "," in b64_str:
|
|
@@ -48,11 +62,18 @@ def b64_to_pil(b64_str: str) -> Image.Image:
|
|
| 48 |
return Image.open(BytesIO(data))
|
| 49 |
|
| 50 |
|
|
|
|
| 51 |
def run_gradio_inference(image_file):
|
| 52 |
"""Gradio handler for interactive testing."""
|
| 53 |
if image_file is None:
|
| 54 |
return None, None, "Please provide an image or GeoTIFF file."
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
file_path = Path(image_file)
|
| 57 |
try:
|
| 58 |
result = inference_engine.run_user_file(file_path)
|
|
|
|
| 38 |
from config import DEVICE
|
| 39 |
|
| 40 |
|
| 41 |
+
try:
|
| 42 |
+
import spaces
|
| 43 |
+
except ImportError:
|
| 44 |
+
class _MockSpaces:
|
| 45 |
+
@staticmethod
|
| 46 |
+
def GPU(func=None, **kwargs):
|
| 47 |
+
if func is not None:
|
| 48 |
+
return func
|
| 49 |
+
def decorator(f):
|
| 50 |
+
return f
|
| 51 |
+
return decorator
|
| 52 |
+
spaces = _MockSpaces()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
def b64_to_pil(b64_str: str) -> Image.Image:
|
| 56 |
"""Decode a data URI base64 string to a PIL Image."""
|
| 57 |
if "," in b64_str:
|
|
|
|
| 62 |
return Image.open(BytesIO(data))
|
| 63 |
|
| 64 |
|
| 65 |
+
@spaces.GPU
|
| 66 |
def run_gradio_inference(image_file):
|
| 67 |
"""Gradio handler for interactive testing."""
|
| 68 |
if image_file is None:
|
| 69 |
return None, None, "Please provide an image or GeoTIFF file."
|
| 70 |
|
| 71 |
+
import torch
|
| 72 |
+
if torch.cuda.is_available() and getattr(inference_engine, "device", None) and inference_engine.device.type != "cuda":
|
| 73 |
+
inference_engine.device = torch.device("cuda")
|
| 74 |
+
if getattr(inference_engine, "hat_model", None) is not None:
|
| 75 |
+
inference_engine.hat_model = inference_engine.hat_model.to("cuda")
|
| 76 |
+
|
| 77 |
file_path = Path(image_file)
|
| 78 |
try:
|
| 79 |
result = inference_engine.run_user_file(file_path)
|