Spaces:
Runtime error
Runtime error
Fix: Switch to Native Gradio SDK for ZeroGPU stability
Browse files- app.py +42 -99
- requirements.txt +7 -10
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import torch
|
| 5 |
|
| 6 |
-
#
|
| 7 |
try:
|
| 8 |
import gradio_client.utils as client_utils
|
| 9 |
if not hasattr(client_utils, "_old_json_schema_to_python_type"):
|
|
@@ -14,135 +13,79 @@ try:
|
|
| 14 |
client_utils._json_schema_to_python_type = patched_json_schema_to_python_type
|
| 15 |
except: pass
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
import tempfile
|
| 21 |
-
|
| 22 |
-
# CONFIG
|
| 23 |
-
MODELS = {
|
| 24 |
-
"Pony Diffusion V6 XL": "cyberdelia/CyberRealisticPony",
|
| 25 |
-
"RealVisXL V4.0": "SG161222/RealVisXL_V4.0"
|
| 26 |
-
}
|
| 27 |
|
|
|
|
| 28 |
LORAS = {
|
| 29 |
"Ninguno": "",
|
| 30 |
-
"π NSFW: Real Nudity": "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps",
|
| 31 |
-
"π DOCS: ID Card
|
| 32 |
-
"π« WEAPONS: Tactical
|
| 33 |
-
"βοΈ TEXT: Typography": "ntc/Typography-SDXL"
|
| 34 |
}
|
| 35 |
|
| 36 |
-
def flush():
|
| 37 |
-
gc.collect()
|
| 38 |
-
if torch.cuda.is_available():
|
| 39 |
-
torch.cuda.empty_cache()
|
| 40 |
-
|
| 41 |
-
# --- MOTOR DE IMAGEN ---
|
| 42 |
@spaces.GPU(duration=120)
|
| 43 |
-
def
|
| 44 |
flush()
|
| 45 |
-
# ImportaciΓ³n local para ahorrar RAM
|
| 46 |
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if "Pony" in model_name:
|
| 52 |
-
prompt = f"score_9, score_8_up, score_7_up, {prompt}"
|
| 53 |
-
|
| 54 |
-
# Carga con bajo consumo de CPU
|
| 55 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 56 |
-
model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True,
|
| 57 |
-
low_cpu_mem_usage=True
|
| 58 |
-
).to("cuda")
|
| 59 |
-
|
| 60 |
-
# Activar offload para ahorrar RAM
|
| 61 |
-
pipe.enable_model_cpu_offload()
|
| 62 |
-
|
| 63 |
-
lora_id = lora_id_custom if lora_id_custom else LORAS.get(lora_name)
|
| 64 |
-
if lora_id:
|
| 65 |
-
try:
|
| 66 |
-
pipe.load_lora_weights(lora_id)
|
| 67 |
-
pipe.fuse_lora(lora_scale=lora_scale)
|
| 68 |
except: pass
|
| 69 |
|
| 70 |
-
if init_img
|
| 71 |
pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_pipe(pipe)
|
| 72 |
-
res = pipe_i2i(prompt=
|
| 73 |
del pipe_i2i
|
| 74 |
else:
|
| 75 |
-
res = pipe(prompt=
|
| 76 |
|
| 77 |
del pipe
|
| 78 |
flush()
|
| 79 |
return res
|
| 80 |
|
| 81 |
-
# --- MOTOR DE VIDEO ---
|
| 82 |
@spaces.GPU(duration=250)
|
| 83 |
-
def
|
| 84 |
flush()
|
| 85 |
from diffusers import LTXPipeline
|
| 86 |
from diffusers.utils import export_to_video
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
).to("cuda")
|
| 92 |
-
|
| 93 |
-
pipe.enable_model_cpu_offload()
|
| 94 |
-
pipe.enable_vae_slicing()
|
| 95 |
-
|
| 96 |
-
kwargs = {
|
| 97 |
-
"prompt": f"score_9, {prompt}",
|
| 98 |
-
"negative_prompt": "low quality, blurry, static, ugly",
|
| 99 |
-
"num_inference_steps": int(steps),
|
| 100 |
-
"guidance_scale": cfg,
|
| 101 |
-
"num_frames": 33,
|
| 102 |
-
"width": 704,
|
| 103 |
-
"height": 480
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
if init_img is not None:
|
| 107 |
-
kwargs["image"] = init_img
|
| 108 |
-
|
| 109 |
-
output = pipe(**kwargs)
|
| 110 |
tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 111 |
-
export_to_video(
|
| 112 |
-
|
| 113 |
del pipe
|
| 114 |
flush()
|
| 115 |
return tmp.name
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
gr.HTML("<h1 style='text-align:center;'>π Omni-Studio v3.1 (Power-Optimized)</h1>")
|
| 120 |
-
|
| 121 |
with gr.Tabs():
|
| 122 |
-
with gr.Tab("
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column():
|
| 125 |
-
p = gr.Textbox(label="Prompt"
|
| 126 |
-
n = gr.Textbox(label="Negativo", value="blurry, lowres")
|
| 127 |
-
m = gr.Dropdown(choices=list(MODELS.keys()), value=list(MODELS.keys())[0], label="Motor")
|
| 128 |
l = gr.Dropdown(choices=list(LORAS.keys()), value="Ninguno", label="LoRA")
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
out_i = gr.Image(label="Resultado")
|
| 136 |
-
|
| 137 |
with gr.Tab("π₯ Video"):
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column():
|
| 140 |
-
vp = gr.Textbox(label="
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
|
| 148 |
demo.queue().launch(show_api=False, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import sys, os, gc, torch, spaces, tempfile
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
+
# PATCH GRADIO
|
| 6 |
try:
|
| 7 |
import gradio_client.utils as client_utils
|
| 8 |
if not hasattr(client_utils, "_old_json_schema_to_python_type"):
|
|
|
|
| 13 |
client_utils._json_schema_to_python_type = patched_json_schema_to_python_type
|
| 14 |
except: pass
|
| 15 |
|
| 16 |
+
def flush():
|
| 17 |
+
gc.collect()
|
| 18 |
+
if torch.cuda.is_available(): torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
MODELS = {"Pony Diffusion V6 XL": "cyberdelia/CyberRealisticPony"}
|
| 21 |
LORAS = {
|
| 22 |
"Ninguno": "",
|
| 23 |
+
"π NSFW: Real Nudity": "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps",
|
| 24 |
+
"π DOCS: ID Card": "j0rdan/passport-sdxl",
|
| 25 |
+
"π« WEAPONS: Tactical": "Ostris/SDXL_LoRA_Test"
|
|
|
|
| 26 |
}
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@spaces.GPU(duration=120)
|
| 29 |
+
def generate(prompt, lora_name, w, h, init_img=None, strength=0.6):
|
| 30 |
flush()
|
|
|
|
| 31 |
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
|
| 32 |
+
p = f"score_9, score_8_up, score_7_up, {prompt}"
|
| 33 |
+
pipe = StableDiffusionXLPipeline.from_pretrained("cyberdelia/CyberRealisticPony", torch_dtype=torch.float16, variant="fp16", low_cpu_mem_usage=True).to("cuda")
|
| 34 |
|
| 35 |
+
lid = LORAS.get(lora_name)
|
| 36 |
+
if lid:
|
| 37 |
+
try: pipe.load_lora_weights(lid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except: pass
|
| 39 |
|
| 40 |
+
if init_img:
|
| 41 |
pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_pipe(pipe)
|
| 42 |
+
res = pipe_i2i(prompt=p, image=init_img, strength=strength, num_inference_steps=25).images[0]
|
| 43 |
del pipe_i2i
|
| 44 |
else:
|
| 45 |
+
res = pipe(prompt=p, num_inference_steps=30, width=int(w), height=int(h)).images[0]
|
| 46 |
|
| 47 |
del pipe
|
| 48 |
flush()
|
| 49 |
return res
|
| 50 |
|
|
|
|
| 51 |
@spaces.GPU(duration=250)
|
| 52 |
+
def video(prompt, init_img):
|
| 53 |
flush()
|
| 54 |
from diffusers import LTXPipeline
|
| 55 |
from diffusers.utils import export_to_video
|
| 56 |
+
pipe = LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16, low_cpu_mem_usage=True).to("cuda")
|
| 57 |
+
kw = {"prompt": prompt, "num_inference_steps": 20, "num_frames": 25, "width": 704, "height": 480}
|
| 58 |
+
if init_img: kw["image"] = init_img
|
| 59 |
+
out = pipe(**kw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 61 |
+
export_to_video(out.frames[0], tmp.name, fps=16)
|
|
|
|
| 62 |
del pipe
|
| 63 |
flush()
|
| 64 |
return tmp.name
|
| 65 |
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.HTML("<h1 style='text-align:center;'>π Omni-Studio v3.2</h1>")
|
|
|
|
|
|
|
| 68 |
with gr.Tabs():
|
| 69 |
+
with gr.Tab("πΌ Imagen"):
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column():
|
| 72 |
+
p = gr.Textbox(label="Prompt")
|
|
|
|
|
|
|
| 73 |
l = gr.Dropdown(choices=list(LORAS.keys()), value="Ninguno", label="LoRA")
|
| 74 |
+
w = gr.Slider(512, 1024, 832, step=64)
|
| 75 |
+
h = gr.Slider(512, 1024, 1216, step=64)
|
| 76 |
+
img = gr.Image(label="Base", type="pil")
|
| 77 |
+
st = gr.Slider(0.1, 0.9, 0.6, label="Mod Strength")
|
| 78 |
+
btn = gr.Button("GENERAR")
|
| 79 |
+
out = gr.Image(label="Resultado")
|
|
|
|
|
|
|
| 80 |
with gr.Tab("π₯ Video"):
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
| 83 |
+
vp = gr.Textbox(label="Prompt")
|
| 84 |
+
vi = gr.Image(label="Base", type="pil")
|
| 85 |
+
vbtn = gr.Button("GENERAR VIDEO")
|
| 86 |
+
vout = gr.Video(label="Resultado")
|
| 87 |
|
| 88 |
+
btn.click(generate, [p, l, w, h, img, st], out)
|
| 89 |
+
vbtn.click(video, [vp, vi], vout)
|
| 90 |
|
| 91 |
demo.queue().launch(show_api=False, server_name="0.0.0.0", server_port=7860)
|
requirements.txt
CHANGED
|
@@ -1,14 +1,11 @@
|
|
| 1 |
-
spaces
|
| 2 |
gradio==4.44.1
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
huggingface-hub==0.24.2
|
| 6 |
-
audioop-lts
|
| 7 |
diffusers>=0.31.0
|
| 8 |
-
transformers
|
| 9 |
-
accelerate
|
| 10 |
peft
|
| 11 |
-
torch
|
| 12 |
sentencepiece
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
| 1 |
gradio==4.44.1
|
| 2 |
+
spaces
|
| 3 |
+
torch
|
|
|
|
|
|
|
| 4 |
diffusers>=0.31.0
|
| 5 |
+
transformers
|
| 6 |
+
accelerate
|
| 7 |
peft
|
|
|
|
| 8 |
sentencepiece
|
| 9 |
+
safetensors
|
| 10 |
+
numpy
|
| 11 |
+
pillow
|