Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline, DPMSolverMultistepScheduler
|
| 4 |
-
from diffusers.models import UNet2DConditionModel
|
| 5 |
import torch
|
| 6 |
import os
|
| 7 |
from PIL import Image
|
| 8 |
import base64
|
| 9 |
import time
|
| 10 |
import logging
|
|
|
|
| 11 |
|
| 12 |
-
# Disable GPU detection
|
| 13 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 14 |
os.environ["CUDA_DEVICE_ORDER"] = ""
|
| 15 |
os.environ["TORCH_CUDA_ARCH_LIST"] = ""
|
|
@@ -28,7 +28,7 @@ logger.info(f"Device in use: {torch.device('cpu')}")
|
|
| 28 |
# Model cache
|
| 29 |
model_cache = {}
|
| 30 |
model_paths = {
|
| 31 |
-
"ssd-1b": "
|
| 32 |
"sd-v1-5": "remiai3/stable-diffusion-v1-5"
|
| 33 |
}
|
| 34 |
|
|
@@ -41,63 +41,25 @@ ratio_to_dims = {
|
|
| 41 |
|
| 42 |
def load_model(model_id):
|
| 43 |
if model_id not in model_cache:
|
| 44 |
-
logger.info(f"Loading model {model_id}
|
|
|
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
logger.warning(f"StableDiffusionXLPipeline failed for {model_id}: {str(e)}")
|
| 60 |
-
logger.info(f"Falling back to StableDiffusionPipeline for {model_id}")
|
| 61 |
-
# Fallback to StableDiffusionPipeline with patched UNet
|
| 62 |
-
unet_config = UNet2DConditionModel.load_config(
|
| 63 |
-
f"{model_paths[model_id]}/unet",
|
| 64 |
-
use_auth_token=os.getenv("HF_TOKEN"),
|
| 65 |
-
force_download=True
|
| 66 |
-
)
|
| 67 |
-
if "reverse_transformer_layers_per_block" in unet_config:
|
| 68 |
-
logger.info(f"Original UNet config for {model_id}: {unet_config}")
|
| 69 |
-
unet_config["reverse_transformer_layers_per_block"] = None
|
| 70 |
-
logger.info(f"Patched UNet config for {model_id}: {unet_config}")
|
| 71 |
-
unet = UNet2DConditionModel.from_config(unet_config)
|
| 72 |
-
unet.load_state_dict(
|
| 73 |
-
torch.load(
|
| 74 |
-
f"{model_paths[model_id]}/unet/diffusion_pytorch_model.bin",
|
| 75 |
-
map_location="cpu"
|
| 76 |
-
)
|
| 77 |
-
)
|
| 78 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 79 |
-
model_paths[model_id],
|
| 80 |
-
unet=unet,
|
| 81 |
-
torch_dtype=torch.float32,
|
| 82 |
-
use_auth_token=os.getenv("HF_TOKEN"),
|
| 83 |
-
use_safetensors=True,
|
| 84 |
-
low_cpu_mem_usage=True,
|
| 85 |
-
force_download=True
|
| 86 |
-
)
|
| 87 |
-
else:
|
| 88 |
-
# Standard loading for sd-v1-5
|
| 89 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 90 |
-
model_paths[model_id],
|
| 91 |
-
torch_dtype=torch.float32,
|
| 92 |
-
use_auth_token=os.getenv("HF_TOKEN"),
|
| 93 |
-
use_safetensors=True,
|
| 94 |
-
low_cpu_mem_usage=True,
|
| 95 |
-
force_download=True
|
| 96 |
-
)
|
| 97 |
-
logger.info(f"Pipeline components loading for {model_id}...")
|
| 98 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 99 |
pipe.enable_attention_slicing()
|
| 100 |
-
pipe.to(torch.device("cpu"))
|
| 101 |
model_cache[model_id] = pipe
|
| 102 |
logger.info(f"Model {model_id} loaded successfully")
|
| 103 |
except Exception as e:
|
|
@@ -135,7 +97,7 @@ def generate():
|
|
| 135 |
|
| 136 |
width, height = ratio_to_dims.get(ratio, (256, 256))
|
| 137 |
pipe = load_model(model_id)
|
| 138 |
-
pipe.to(torch.device("cpu"))
|
| 139 |
|
| 140 |
images = []
|
| 141 |
num_inference_steps = 20 if model_id == 'ssd-1b' else 30
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline, DPMSolverMultistepScheduler
|
|
|
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
| 7 |
import base64
|
| 8 |
import time
|
| 9 |
import logging
|
| 10 |
+
from huggingface_hub import list_repo_files
|
| 11 |
|
| 12 |
+
# Disable GPU detection (remove these lines if GPU is available)
|
| 13 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 14 |
os.environ["CUDA_DEVICE_ORDER"] = ""
|
| 15 |
os.environ["TORCH_CUDA_ARCH_LIST"] = ""
|
|
|
|
| 28 |
# Model cache
|
| 29 |
model_cache = {}
|
| 30 |
model_paths = {
|
| 31 |
+
"ssd-1b": "segmind/SSD-1B", # Use segmind/SSD-1B for testing
|
| 32 |
"sd-v1-5": "remiai3/stable-diffusion-v1-5"
|
| 33 |
}
|
| 34 |
|
|
|
|
| 41 |
|
| 42 |
def load_model(model_id):
|
| 43 |
if model_id not in model_cache:
|
| 44 |
+
logger.info(f"Loading model {model_id} from {model_paths[model_id]}")
|
| 45 |
+
logger.info(f"HF_TOKEN present: {os.getenv('HF_TOKEN') is not None}")
|
| 46 |
try:
|
| 47 |
+
# Log repository files for debugging
|
| 48 |
+
repo_files = list_repo_files(model_paths[model_id], token=os.getenv("HF_TOKEN"))
|
| 49 |
+
logger.info(f"Files in {model_paths[model_id]}: {repo_files}")
|
| 50 |
+
|
| 51 |
+
# Choose pipeline based on model
|
| 52 |
+
pipe_class = StableDiffusionXLPipeline if model_id == "ssd-1b" else StableDiffusionPipeline
|
| 53 |
+
pipe = pipe_class.from_pretrained(
|
| 54 |
+
model_paths[model_id],
|
| 55 |
+
torch_dtype=torch.float32,
|
| 56 |
+
use_auth_token=os.getenv("HF_TOKEN"),
|
| 57 |
+
use_safetensors=True,
|
| 58 |
+
low_cpu_mem_usage=True
|
| 59 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 61 |
pipe.enable_attention_slicing()
|
| 62 |
+
pipe.to(torch.device("cpu")) # Change to "cuda" if GPU is available
|
| 63 |
model_cache[model_id] = pipe
|
| 64 |
logger.info(f"Model {model_id} loaded successfully")
|
| 65 |
except Exception as e:
|
|
|
|
| 97 |
|
| 98 |
width, height = ratio_to_dims.get(ratio, (256, 256))
|
| 99 |
pipe = load_model(model_id)
|
| 100 |
+
pipe.to(torch.device("cpu")) # Change to "cuda" if GPU is available
|
| 101 |
|
| 102 |
images = []
|
| 103 |
num_inference_steps = 20 if model_id == 'ssd-1b' else 30
|