Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
|
|
| 1 |
# coding=utf-8
|
| 2 |
"""OpenHTR-Gen: interactive handwriting text synthesis demo (Space / ZeroGPU)."""
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import random
|
| 7 |
import threading
|
| 8 |
import time
|
|
@@ -12,20 +24,16 @@ import gradio as gr
|
|
| 12 |
import engine
|
| 13 |
import selection
|
| 14 |
|
| 15 |
-
os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
|
| 16 |
-
|
| 17 |
-
try:
|
| 18 |
-
import spaces # provided by ZeroGPU
|
| 19 |
-
_HAS_SPACES = True
|
| 20 |
-
except ImportError: # pragma: no cover
|
| 21 |
-
spaces = None
|
| 22 |
-
_HAS_SPACES = False
|
| 23 |
-
|
| 24 |
|
| 25 |
TITLE = "OpenHTR-Gen"
|
| 26 |
BASE_MODEL = os.environ.get("BASE_MODEL", "black-forest-labs/FLUX.1-Fill-dev")
|
| 27 |
GPU_DURATION = int(os.environ.get("GPU_DURATION", "120"))
|
| 28 |
QUANTIZE_8BIT = os.environ.get("HW_QUANTIZE_8BIT", "0").lower() in ("1", "true", "yes", "8bit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
MAX_CHARS = int(os.environ.get("MAX_CHARS", "35"))
|
| 30 |
SIZE = engine.DEFAULT_SIZE # 512, matches the training resolution
|
| 31 |
|
|
@@ -43,7 +51,7 @@ _pipe_lock = threading.Lock()
|
|
| 43 |
|
| 44 |
|
| 45 |
def ensure_pipeline():
|
| 46 |
-
"""
|
| 47 |
global _pipe
|
| 48 |
if _pipe is not None:
|
| 49 |
return _pipe
|
|
@@ -55,8 +63,9 @@ def ensure_pipeline():
|
|
| 55 |
lora_path=engine.resolve_lora_path(),
|
| 56 |
quantize_8bit=QUANTIZE_8BIT,
|
| 57 |
device="cuda",
|
|
|
|
| 58 |
)
|
| 59 |
-
print(f"[load] ready in {time.time() - t0:.1f}s")
|
| 60 |
return _pipe
|
| 61 |
|
| 62 |
|
|
@@ -79,8 +88,12 @@ def _warm_hf_cache():
|
|
| 79 |
print(f"[warm] adapter: {engine.resolve_lora_path()}")
|
| 80 |
|
| 81 |
|
| 82 |
-
if
|
| 83 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
threading.Thread(target=_warm_hf_cache, daemon=True).start()
|
| 85 |
|
| 86 |
|
|
|
|
| 1 |
+
|
| 2 |
# coding=utf-8
|
| 3 |
"""OpenHTR-Gen: interactive handwriting text synthesis demo (Space / ZeroGPU)."""
|
| 4 |
from __future__ import annotations
|
| 5 |
|
| 6 |
import os
|
| 7 |
+
|
| 8 |
+
# ZeroGPU patches CUDA during import. This MUST precede Gradio, engine and
|
| 9 |
+
# anything else that may import torch or initialize CUDA.
|
| 10 |
+
os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
|
| 11 |
+
try:
|
| 12 |
+
import spaces # provided by Hugging Face ZeroGPU
|
| 13 |
+
_HAS_SPACES = True
|
| 14 |
+
except ImportError: # local/non-ZeroGPU execution
|
| 15 |
+
spaces = None
|
| 16 |
+
_HAS_SPACES = False
|
| 17 |
+
|
| 18 |
import random
|
| 19 |
import threading
|
| 20 |
import time
|
|
|
|
| 24 |
import engine
|
| 25 |
import selection
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
TITLE = "OpenHTR-Gen"
|
| 29 |
BASE_MODEL = os.environ.get("BASE_MODEL", "black-forest-labs/FLUX.1-Fill-dev")
|
| 30 |
GPU_DURATION = int(os.environ.get("GPU_DURATION", "120"))
|
| 31 |
QUANTIZE_8BIT = os.environ.get("HW_QUANTIZE_8BIT", "0").lower() in ("1", "true", "yes", "8bit")
|
| 32 |
+
# ZeroGPU packs model weights placed on CUDA at module scope for reuse by GPU
|
| 33 |
+
# workers. Keep lazy loading for ordinary local execution unless requested.
|
| 34 |
+
EAGER_LOAD = os.environ.get("HW_EAGER_LOAD", "1" if _HAS_SPACES else "0").lower() in (
|
| 35 |
+
"1", "true", "yes"
|
| 36 |
+
)
|
| 37 |
MAX_CHARS = int(os.environ.get("MAX_CHARS", "35"))
|
| 38 |
SIZE = engine.DEFAULT_SIZE # 512, matches the training resolution
|
| 39 |
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def ensure_pipeline():
|
| 54 |
+
"""Initialize once; ZeroGPU prefers calling this at module scope."""
|
| 55 |
global _pipe
|
| 56 |
if _pipe is not None:
|
| 57 |
return _pipe
|
|
|
|
| 63 |
lora_path=engine.resolve_lora_path(),
|
| 64 |
quantize_8bit=QUANTIZE_8BIT,
|
| 65 |
device="cuda",
|
| 66 |
+
zero_gpu=_HAS_SPACES,
|
| 67 |
)
|
| 68 |
+
print(f"[load] ready in {time.time() - t0:.1f}s", flush=True)
|
| 69 |
return _pipe
|
| 70 |
|
| 71 |
|
|
|
|
| 88 |
print(f"[warm] adapter: {engine.resolve_lora_path()}")
|
| 89 |
|
| 90 |
|
| 91 |
+
if EAGER_LOAD:
|
| 92 |
+
# ZeroGPU CUDA emulation registers the model weights here, before GPU tasks.
|
| 93 |
+
# Do not spawn a competing download thread while loading the same weights.
|
| 94 |
+
ensure_pipeline()
|
| 95 |
+
elif _HAS_SPACES:
|
| 96 |
+
# Optional legacy lazy mode: prefetch to keep GPU tasks mostly compute-bound.
|
| 97 |
threading.Thread(target=_warm_hf_cache, daemon=True).start()
|
| 98 |
|
| 99 |
|