Commit ·
ef8e0ee
1
Parent(s): 9735ae9
Refactor import order and enhance environment detection for spaces; streamline local execution support
Browse files
app.py
CHANGED
|
@@ -1,25 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import tempfile
|
| 4 |
-
import shutil
|
| 5 |
-
import uuid
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import torch
|
| 10 |
-
import numpy as np
|
| 11 |
-
from PIL import Image
|
| 12 |
-
|
| 13 |
-
from diffusers import Flux2Pipeline
|
| 14 |
-
from diffusers.utils import load_image
|
| 15 |
-
|
| 16 |
-
import py7zr
|
| 17 |
-
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
|
| 18 |
-
from skimage.util import img_as_float
|
| 19 |
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
-
#
|
|
|
|
| 23 |
# =========================
|
| 24 |
def _is_hf_spaces_env() -> bool:
|
| 25 |
"""Detect if running in Hugging Face Spaces environment"""
|
|
@@ -30,16 +15,15 @@ def _is_hf_spaces_env() -> bool:
|
|
| 30 |
|
| 31 |
IS_HF_SPACES = _is_hf_spaces_env()
|
| 32 |
|
| 33 |
-
# CRITICAL: ZeroGPU scanner requires seeing `import spaces` at module level
|
| 34 |
-
# We use try/except to handle local environment where spaces is not installed
|
| 35 |
try:
|
| 36 |
import spaces
|
| 37 |
|
| 38 |
HAS_SPACES = True
|
| 39 |
except ImportError:
|
| 40 |
HAS_SPACES = False
|
| 41 |
-
|
| 42 |
# Create dummy module for local environment
|
|
|
|
|
|
|
| 43 |
class _SpacesDummy:
|
| 44 |
@staticmethod
|
| 45 |
def GPU(duration: int = 180, **kwargs):
|
|
@@ -50,13 +34,31 @@ except ImportError:
|
|
| 50 |
|
| 51 |
return decorator
|
| 52 |
|
| 53 |
-
import sys
|
| 54 |
-
from types import ModuleType
|
| 55 |
-
|
| 56 |
spaces = ModuleType("spaces")
|
| 57 |
spaces.GPU = _SpacesDummy.GPU
|
| 58 |
sys.modules["spaces"] = spaces
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# =========================
|
| 62 |
# Config
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
+
# CRITICAL: Import spaces FIRST before any CUDA-related packages
|
| 7 |
+
# ZeroGPU requires spaces to be imported before torch/diffusers
|
| 8 |
# =========================
|
| 9 |
def _is_hf_spaces_env() -> bool:
|
| 10 |
"""Detect if running in Hugging Face Spaces environment"""
|
|
|
|
| 15 |
|
| 16 |
IS_HF_SPACES = _is_hf_spaces_env()
|
| 17 |
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
import spaces
|
| 20 |
|
| 21 |
HAS_SPACES = True
|
| 22 |
except ImportError:
|
| 23 |
HAS_SPACES = False
|
|
|
|
| 24 |
# Create dummy module for local environment
|
| 25 |
+
from types import ModuleType
|
| 26 |
+
|
| 27 |
class _SpacesDummy:
|
| 28 |
@staticmethod
|
| 29 |
def GPU(duration: int = 180, **kwargs):
|
|
|
|
| 34 |
|
| 35 |
return decorator
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
spaces = ModuleType("spaces")
|
| 38 |
spaces.GPU = _SpacesDummy.GPU
|
| 39 |
sys.modules["spaces"] = spaces
|
| 40 |
|
| 41 |
+
# =========================
|
| 42 |
+
# Now import CUDA-related packages
|
| 43 |
+
# =========================
|
| 44 |
+
import zipfile
|
| 45 |
+
import tempfile
|
| 46 |
+
import shutil
|
| 47 |
+
import uuid
|
| 48 |
+
from pathlib import Path
|
| 49 |
+
|
| 50 |
+
import gradio as gr
|
| 51 |
+
import torch
|
| 52 |
+
import numpy as np
|
| 53 |
+
from PIL import Image
|
| 54 |
+
|
| 55 |
+
from diffusers import Flux2Pipeline
|
| 56 |
+
from diffusers.utils import load_image
|
| 57 |
+
|
| 58 |
+
import py7zr
|
| 59 |
+
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
|
| 60 |
+
from skimage.util import img_as_float
|
| 61 |
+
|
| 62 |
|
| 63 |
# =========================
|
| 64 |
# Config
|