Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -6,12 +8,21 @@ import torch
|
|
| 6 |
from diffusers import AudioLDM2Pipeline
|
| 7 |
from scipy.io.wavfile import write as write_wav
|
| 8 |
import numpy as np
|
| 9 |
-
import logging
|
| 10 |
|
| 11 |
# --- Setup Logging ---
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# --- Initialize FastAPI App ---
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
@@ -24,12 +35,12 @@ torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
logger.info(f"Attempting to load model '{MODEL_REPO}' on device: {device} with dtype: {torch_dtype}")
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
pipeline = AudioLDM2Pipeline.from_pretrained(
|
| 30 |
MODEL_REPO,
|
| 31 |
torch_dtype=torch_dtype,
|
| 32 |
-
cache_dir=
|
| 33 |
)
|
| 34 |
pipeline = pipeline.to(device)
|
| 35 |
logger.info("Model loaded successfully and moved to device.")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from fastapi.responses import StreamingResponse
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 8 |
from diffusers import AudioLDM2Pipeline
|
| 9 |
from scipy.io.wavfile import write as write_wav
|
| 10 |
import numpy as np
|
|
|
|
| 11 |
|
| 12 |
# --- Setup Logging ---
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# --- CRITICAL FIX for Hugging Face Spaces Permissions ---
|
| 17 |
+
# Set the cache directory for all Hugging Face libraries BEFORE they are used.
|
| 18 |
+
# This forces the model download and any temporary files to a writable location.
|
| 19 |
+
cache_dir = "/tmp/huggingface_cache"
|
| 20 |
+
os.environ["HF_HOME"] = cache_dir
|
| 21 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = cache_dir
|
| 22 |
+
# Create the directory if it doesn't exist, just in case.
|
| 23 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 24 |
+
logger.info(f"Hugging Face cache directory globally set to: {cache_dir}")
|
| 25 |
+
|
| 26 |
# --- Initialize FastAPI App ---
|
| 27 |
app = FastAPI()
|
| 28 |
|
|
|
|
| 35 |
|
| 36 |
try:
|
| 37 |
logger.info(f"Attempting to load model '{MODEL_REPO}' on device: {device} with dtype: {torch_dtype}")
|
| 38 |
+
# The cache_dir argument is now redundant because of the environment variable,
|
| 39 |
+
# but we'll leave it for extra safety.
|
| 40 |
pipeline = AudioLDM2Pipeline.from_pretrained(
|
| 41 |
MODEL_REPO,
|
| 42 |
torch_dtype=torch_dtype,
|
| 43 |
+
cache_dir=cache_dir
|
| 44 |
)
|
| 45 |
pipeline = pipeline.to(device)
|
| 46 |
logger.info("Model loaded successfully and moved to device.")
|