Spaces:
Running on Zero
Running on Zero
Keep API startup independent of model prewarming
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import platform
|
|
|
|
| 3 |
import shutil
|
| 4 |
import tempfile
|
| 5 |
import torch
|
|
@@ -7,6 +8,13 @@ import torchaudio
|
|
| 7 |
import gradio as gr
|
| 8 |
import spaces
|
| 9 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from fastapi.responses import JSONResponse
|
| 11 |
from einops import rearrange
|
| 12 |
from huggingface_hub import login
|
|
@@ -135,17 +143,8 @@ def load_model(model_name):
|
|
| 135 |
return MODEL_CACHE[model_name]
|
| 136 |
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
#
|
| 140 |
-
import threading
|
| 141 |
-
|
| 142 |
-
def _prewarm():
|
| 143 |
-
_log("Pre-warming models...")
|
| 144 |
-
for _name in ["small-music", "small-sfx"]:
|
| 145 |
-
load_model(_name)
|
| 146 |
-
_log("All models ready!")
|
| 147 |
-
|
| 148 |
-
threading.Thread(target=_prewarm, daemon=True).start()
|
| 149 |
|
| 150 |
|
| 151 |
def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|
|
|
|
| 1 |
import os
|
| 2 |
import platform
|
| 3 |
+
import sys
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
| 6 |
import torch
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
import spaces
|
| 10 |
from fastapi import FastAPI
|
| 11 |
+
|
| 12 |
+
# Hugging Face Spaces should use UTF-8; explicitly configure streams so model
|
| 13 |
+
# libraries cannot inherit a platform-specific charmap encoding.
|
| 14 |
+
for _stream in (sys.stdout, sys.stderr):
|
| 15 |
+
if hasattr(_stream, "reconfigure"):
|
| 16 |
+
_stream.reconfigure(encoding="utf-8", errors="backslashreplace")
|
| 17 |
+
|
| 18 |
from fastapi.responses import JSONResponse
|
| 19 |
from einops import rearrange
|
| 20 |
from huggingface_hub import login
|
|
|
|
| 143 |
return MODEL_CACHE[model_name]
|
| 144 |
|
| 145 |
|
| 146 |
+
# Model loading is lazy: startup must remain healthy even when a model download
|
| 147 |
+
# or initialization fails. The first generation request loads the selected model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
|
| 150 |
def generate_audio(prompt, duration, steps, cfg_scale, seed, model_name):
|