Spaces:
Running on Zero
Running on Zero
SEN-10: propagate image patch from fhdr-uncensored (fallback anti-gated models)
Browse files
app.py
CHANGED
|
@@ -17,9 +17,20 @@ from diffusers.utils import export_to_video
|
|
| 17 |
SPACE_ID = os.getenv("SPACE_ID", "").lower()
|
| 18 |
IS_VIDEO_SPACE = any(k in SPACE_ID for k in ["hunyuanvideo", "wan-2-1"])
|
| 19 |
|
| 20 |
-
IMAGE_MODEL_ID = os.getenv("IMAGE_MODEL_ID", "
|
| 21 |
VIDEO_MODEL_ID = os.getenv("VIDEO_MODEL_ID", "damo-vilab/text-to-video-ms-1.7b")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
_image_pipe: Optional[DiffusionPipeline] = None
|
| 24 |
_video_pipe: Optional[DiffusionPipeline] = None
|
| 25 |
|
|
@@ -36,7 +47,33 @@ def _load_image_pipe() -> DiffusionPipeline:
|
|
| 36 |
global _image_pipe
|
| 37 |
if _image_pipe is None:
|
| 38 |
device, dtype = _device_dtype()
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if device == "cuda":
|
| 41 |
_image_pipe.enable_model_cpu_offload()
|
| 42 |
else:
|
|
@@ -73,13 +110,17 @@ def generate_image(prompt: str, steps: int, guidance_scale: float, seed: int):
|
|
| 73 |
pipe = _load_image_pipe()
|
| 74 |
|
| 75 |
gen = torch.Generator(device="cpu").manual_seed(int(seed))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
image: Image.Image = pipe(
|
| 77 |
prompt=prompt,
|
| 78 |
num_inference_steps=int(steps),
|
| 79 |
guidance_scale=float(guidance_scale),
|
| 80 |
generator=gen,
|
| 81 |
-
width=
|
| 82 |
-
height=
|
| 83 |
).images[0]
|
| 84 |
return image
|
| 85 |
|
|
|
|
| 17 |
SPACE_ID = os.getenv("SPACE_ID", "").lower()
|
| 18 |
IS_VIDEO_SPACE = any(k in SPACE_ID for k in ["hunyuanvideo", "wan-2-1"])
|
| 19 |
|
| 20 |
+
IMAGE_MODEL_ID = os.getenv("IMAGE_MODEL_ID", "runwayml/stable-diffusion-v1-5")
|
| 21 |
VIDEO_MODEL_ID = os.getenv("VIDEO_MODEL_ID", "damo-vilab/text-to-video-ms-1.7b")
|
| 22 |
|
| 23 |
+
# Known ungated defaults per space: avoids GatedRepoError on HF Spaces without manual model-license acceptance.
|
| 24 |
+
SPACE_IMAGE_DEFAULTS = {
|
| 25 |
+
"fhdr-uncensored": "SG161222/Realistic_Vision_V6.0_B1_noVAE",
|
| 26 |
+
"z-image-turbo": "stabilityai/sdxl-turbo",
|
| 27 |
+
}
|
| 28 |
+
FALLBACK_IMAGE_MODELS = [
|
| 29 |
+
IMAGE_MODEL_ID,
|
| 30 |
+
SPACE_IMAGE_DEFAULTS.get(SPACE_ID.split("/")[-1], ""),
|
| 31 |
+
"runwayml/stable-diffusion-v1-5",
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
_image_pipe: Optional[DiffusionPipeline] = None
|
| 35 |
_video_pipe: Optional[DiffusionPipeline] = None
|
| 36 |
|
|
|
|
| 47 |
global _image_pipe
|
| 48 |
if _image_pipe is None:
|
| 49 |
device, dtype = _device_dtype()
|
| 50 |
+
last_error = None
|
| 51 |
+
for model_id in [m for m in FALLBACK_IMAGE_MODELS if m]:
|
| 52 |
+
try:
|
| 53 |
+
# Try to disable safety checker when supported.
|
| 54 |
+
_image_pipe = DiffusionPipeline.from_pretrained(
|
| 55 |
+
model_id,
|
| 56 |
+
torch_dtype=dtype,
|
| 57 |
+
safety_checker=None,
|
| 58 |
+
requires_safety_checker=False,
|
| 59 |
+
)
|
| 60 |
+
break
|
| 61 |
+
except TypeError:
|
| 62 |
+
_image_pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=dtype)
|
| 63 |
+
break
|
| 64 |
+
except Exception as e:
|
| 65 |
+
last_error = e
|
| 66 |
+
_image_pipe = None
|
| 67 |
+
|
| 68 |
+
if _image_pipe is None:
|
| 69 |
+
raise RuntimeError(f"Unable to load image model from {FALLBACK_IMAGE_MODELS}: {last_error}")
|
| 70 |
+
|
| 71 |
+
# Explicit runtime bypass for diffusion pipelines exposing an NSFW safety checker.
|
| 72 |
+
if hasattr(_image_pipe, "safety_checker"):
|
| 73 |
+
_image_pipe.safety_checker = None
|
| 74 |
+
if hasattr(_image_pipe, "requires_safety_checker"):
|
| 75 |
+
_image_pipe.requires_safety_checker = False
|
| 76 |
+
|
| 77 |
if device == "cuda":
|
| 78 |
_image_pipe.enable_model_cpu_offload()
|
| 79 |
else:
|
|
|
|
| 110 |
pipe = _load_image_pipe()
|
| 111 |
|
| 112 |
gen = torch.Generator(device="cpu").manual_seed(int(seed))
|
| 113 |
+
default_size = 512 if "v1-5" in str(getattr(pipe, "name_or_path", "")).lower() else 1024
|
| 114 |
+
width = int(os.getenv("IMAGE_WIDTH", default_size))
|
| 115 |
+
height = int(os.getenv("IMAGE_HEIGHT", default_size))
|
| 116 |
+
|
| 117 |
image: Image.Image = pipe(
|
| 118 |
prompt=prompt,
|
| 119 |
num_inference_steps=int(steps),
|
| 120 |
guidance_scale=float(guidance_scale),
|
| 121 |
generator=gen,
|
| 122 |
+
width=width,
|
| 123 |
+
height=height,
|
| 124 |
).images[0]
|
| 125 |
return image
|
| 126 |
|