Spaces:
Running on Zero
Running on Zero
pass theme/css to gr.Blocks() and add graceful spaces fallback
Browse files
app.py
CHANGED
|
@@ -4,13 +4,27 @@ HuggingFace Space Demo (ZeroGPU)
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import logging
|
|
|
|
| 7 |
import sys
|
| 8 |
import tempfile
|
| 9 |
import time
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# NOTE: Do NOT import torch or cordon at module level!
|
| 16 |
# ZeroGPU requires all CUDA-related imports to happen inside @spaces.GPU functions.
|
|
@@ -191,7 +205,7 @@ THEME = gr.themes.Base(
|
|
| 191 |
def create_interface():
|
| 192 |
"""Create the Gradio interface."""
|
| 193 |
|
| 194 |
-
with gr.Blocks(title="Cordon") as demo:
|
| 195 |
|
| 196 |
# Header
|
| 197 |
gr.HTML("""
|
|
@@ -350,6 +364,7 @@ logger.info("=" * 60)
|
|
| 350 |
logger.info("Cordon Space starting...")
|
| 351 |
logger.info(f"Python version: {sys.version}")
|
| 352 |
logger.info(f"Gradio version: {gr.__version__}")
|
|
|
|
| 353 |
logger.info("PyTorch/Cordon loaded lazily inside @spaces.GPU function")
|
| 354 |
logger.info("=" * 60)
|
| 355 |
|
|
@@ -360,7 +375,6 @@ try:
|
|
| 360 |
logger.info("Gradio interface created successfully")
|
| 361 |
|
| 362 |
# Detect if running on HuggingFace Spaces
|
| 363 |
-
import os
|
| 364 |
is_hf_space = os.getenv("SPACE_ID") is not None
|
| 365 |
|
| 366 |
if is_hf_space:
|
|
@@ -369,16 +383,11 @@ try:
|
|
| 369 |
demo.launch(
|
| 370 |
server_name="0.0.0.0",
|
| 371 |
server_port=7860,
|
| 372 |
-
theme=THEME,
|
| 373 |
-
css=CUSTOM_CSS
|
| 374 |
)
|
| 375 |
else:
|
| 376 |
# Local development: let Gradio find an available port
|
| 377 |
logger.info("Running locally - auto-detecting available port")
|
| 378 |
-
demo.launch(
|
| 379 |
-
theme=THEME,
|
| 380 |
-
css=CUSTOM_CSS
|
| 381 |
-
)
|
| 382 |
logger.info("Demo launched successfully")
|
| 383 |
except Exception as e:
|
| 384 |
logger.error(f"Failed to launch demo: {str(e)}", exc_info=True)
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import logging
|
| 7 |
+
import os
|
| 8 |
import sys
|
| 9 |
import tempfile
|
| 10 |
import time
|
| 11 |
from pathlib import Path
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
+
|
| 15 |
+
# ZeroGPU support - graceful fallback for local development
|
| 16 |
+
try:
|
| 17 |
+
import spaces
|
| 18 |
+
ZEROGPU_AVAILABLE = True
|
| 19 |
+
except ImportError:
|
| 20 |
+
ZEROGPU_AVAILABLE = False
|
| 21 |
+
# Create a no-op decorator for local development
|
| 22 |
+
class spaces:
|
| 23 |
+
@staticmethod
|
| 24 |
+
def GPU(duration=60):
|
| 25 |
+
def decorator(fn):
|
| 26 |
+
return fn
|
| 27 |
+
return decorator
|
| 28 |
|
| 29 |
# NOTE: Do NOT import torch or cordon at module level!
|
| 30 |
# ZeroGPU requires all CUDA-related imports to happen inside @spaces.GPU functions.
|
|
|
|
| 205 |
def create_interface():
|
| 206 |
"""Create the Gradio interface."""
|
| 207 |
|
| 208 |
+
with gr.Blocks(title="Cordon", theme=THEME, css=CUSTOM_CSS) as demo:
|
| 209 |
|
| 210 |
# Header
|
| 211 |
gr.HTML("""
|
|
|
|
| 364 |
logger.info("Cordon Space starting...")
|
| 365 |
logger.info(f"Python version: {sys.version}")
|
| 366 |
logger.info(f"Gradio version: {gr.__version__}")
|
| 367 |
+
logger.info(f"ZeroGPU available: {ZEROGPU_AVAILABLE}")
|
| 368 |
logger.info("PyTorch/Cordon loaded lazily inside @spaces.GPU function")
|
| 369 |
logger.info("=" * 60)
|
| 370 |
|
|
|
|
| 375 |
logger.info("Gradio interface created successfully")
|
| 376 |
|
| 377 |
# Detect if running on HuggingFace Spaces
|
|
|
|
| 378 |
is_hf_space = os.getenv("SPACE_ID") is not None
|
| 379 |
|
| 380 |
if is_hf_space:
|
|
|
|
| 383 |
demo.launch(
|
| 384 |
server_name="0.0.0.0",
|
| 385 |
server_port=7860,
|
|
|
|
|
|
|
| 386 |
)
|
| 387 |
else:
|
| 388 |
# Local development: let Gradio find an available port
|
| 389 |
logger.info("Running locally - auto-detecting available port")
|
| 390 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 391 |
logger.info("Demo launched successfully")
|
| 392 |
except Exception as e:
|
| 393 |
logger.error(f"Failed to launch demo: {str(e)}", exc_info=True)
|