Spaces:
Runtime error
Runtime error
Fix: Stable Gradio 4.44.1 + surgical patches
Browse files
app.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
import sys
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
try:
|
| 4 |
import audioop
|
| 5 |
except ImportError:
|
|
@@ -9,131 +12,88 @@ except ImportError:
|
|
| 9 |
except ImportError:
|
| 10 |
pass
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
import spaces
|
| 13 |
import gradio as gr
|
| 14 |
import torch
|
| 15 |
import numpy as np
|
| 16 |
from PIL import Image
|
| 17 |
-
import tempfile
|
| 18 |
|
| 19 |
-
#
|
| 20 |
BASE_MODEL = "cyberdelia/CyberRealisticPony"
|
| 21 |
LTX_MODEL = "Lightricks/LTX-Video"
|
| 22 |
LTX_NSFW_LORA = "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps"
|
| 23 |
-
|
| 24 |
-
pipe_t2i = None
|
| 25 |
-
pipe_video = None
|
| 26 |
-
|
| 27 |
NEG_DEFAULT = "blurry, low quality, bad anatomy, deformed, ugly, watermark, text"
|
| 28 |
|
| 29 |
-
# ─── LOADERS ───────────────────────────────────────────────────────────────────
|
| 30 |
def load_t2i(lora_id=None, lora_scale=1.0):
|
| 31 |
-
global pipe_t2i
|
| 32 |
from diffusers import StableDiffusionXLPipeline
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
if lora_id and len(lora_id.strip()) > 5:
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
pipe_t2i.fuse_lora(lora_scale=lora_scale)
|
| 42 |
except: pass
|
| 43 |
-
return
|
| 44 |
|
| 45 |
def load_video():
|
| 46 |
-
global pipe_video
|
| 47 |
from diffusers import LTXPipeline
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return pipe_video
|
| 54 |
|
| 55 |
-
|
| 56 |
-
@spaces.GPU(duration=100)
|
| 57 |
def generate_t2i(prompt, neg, lora_id, lora_scale, w, h):
|
| 58 |
-
# Valores internos para evitar errores de API
|
| 59 |
-
steps = 30
|
| 60 |
-
cfg = 7.0
|
| 61 |
-
seed = 42
|
| 62 |
-
|
| 63 |
pipe = load_t2i(lora_id, lora_scale).to("cuda")
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
prompt=prompt,
|
| 68 |
-
negative_prompt=neg,
|
| 69 |
-
num_inference_steps=steps,
|
| 70 |
-
guidance_scale=cfg,
|
| 71 |
-
width=int(w),
|
| 72 |
-
height=int(h),
|
| 73 |
-
generator=gen
|
| 74 |
-
).images[0]
|
| 75 |
-
|
| 76 |
-
pipe.to("cpu")
|
| 77 |
-
torch.cuda.empty_cache()
|
| 78 |
-
return result
|
| 79 |
|
| 80 |
@spaces.GPU(duration=200)
|
| 81 |
def generate_video(prompt, init_image, lora_scale):
|
| 82 |
-
# Valores internos fijos
|
| 83 |
-
steps = 30
|
| 84 |
-
num_frames = 49
|
| 85 |
-
fps = 24
|
| 86 |
-
seed = 42
|
| 87 |
-
|
| 88 |
from diffusers.utils import export_to_video
|
| 89 |
pipe = load_video().to("cuda")
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
kwargs = {
|
| 93 |
-
"prompt": prompt,
|
| 94 |
-
"negative_prompt": NEG_DEFAULT,
|
| 95 |
-
"num_frames": num_frames,
|
| 96 |
-
"num_inference_steps": steps,
|
| 97 |
-
"generator": gen
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
if init_image is not None:
|
| 101 |
kwargs["image"] = Image.fromarray(init_image).convert("RGB").resize((768, 512))
|
| 102 |
-
|
| 103 |
if lora_scale > 0:
|
| 104 |
kwargs["cross_attention_kwargs"] = {"scale": lora_scale}
|
| 105 |
-
|
| 106 |
output = pipe(**kwargs)
|
| 107 |
tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 108 |
-
export_to_video(output.frames[0], tmp.name, fps=
|
| 109 |
-
|
| 110 |
-
pipe.to("cpu")
|
| 111 |
-
torch.cuda.empty_cache()
|
| 112 |
return tmp.name
|
| 113 |
|
| 114 |
-
# ─── INTERFAZ TÉCNICA ─────────────────────────────���────────────────────────────
|
| 115 |
with gr.Blocks(title="Image Utility v2.1") as demo:
|
| 116 |
gr.HTML("<h1 style='text-align:center;'>🛠 Image Processing Utility v2.1.4</h1>")
|
| 117 |
-
|
| 118 |
with gr.Tabs():
|
| 119 |
with gr.Tab("D-Processor (T2I)"):
|
| 120 |
with gr.Row():
|
| 121 |
with gr.Column():
|
| 122 |
t2i_p = gr.Textbox(label="Input Data String", lines=3)
|
| 123 |
t2i_n = gr.Textbox(label="Excluded Data", value=NEG_DEFAULT)
|
| 124 |
-
t2i_lora = gr.Textbox(label="Extension ID"
|
| 125 |
t2i_ls = gr.Slider(0, 1.5, 0.8, label="Extension Weight")
|
| 126 |
with gr.Row():
|
| 127 |
t2i_w = gr.Slider(512, 1024, 1024, step=64, label="X-Axis")
|
| 128 |
t2i_h = gr.Slider(512, 1024, 1024, step=64, label="Y-Axis")
|
| 129 |
-
t2i_btn = gr.Button("Execute Process"
|
| 130 |
t2i_out = gr.Image(label="Output Preview")
|
| 131 |
-
|
| 132 |
-
t2i_btn.click(
|
| 133 |
-
fn=generate_t2i,
|
| 134 |
-
inputs=[t2i_p, t2i_n, t2i_lora, t2i_ls, t2i_w, t2i_h],
|
| 135 |
-
outputs=t2i_out
|
| 136 |
-
)
|
| 137 |
|
| 138 |
with gr.Tab("M-Sequence (Video)"):
|
| 139 |
with gr.Row():
|
|
@@ -141,13 +101,8 @@ with gr.Blocks(title="Image Utility v2.1") as demo:
|
|
| 141 |
v_p = gr.Textbox(label="Motion Vector String", lines=3)
|
| 142 |
v_img = gr.Image(label="Source Buffer", type="numpy")
|
| 143 |
v_ls = gr.Slider(0, 1.5, 0.8, label="Motion Weight")
|
| 144 |
-
v_btn = gr.Button("Process Sequence"
|
| 145 |
v_out = gr.Video(label="Sequence Output")
|
| 146 |
-
|
| 147 |
-
v_btn.click(
|
| 148 |
-
fn=generate_video,
|
| 149 |
-
inputs=[v_p, v_img, v_ls],
|
| 150 |
-
outputs=v_out
|
| 151 |
-
)
|
| 152 |
|
| 153 |
demo.launch()
|
|
|
|
| 1 |
import sys
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# --- PARCHES DE COMPATIBILIDAD CRÍTICOS ---
|
| 5 |
+
# 1. Parche para audioop (Python 3.13)
|
| 6 |
try:
|
| 7 |
import audioop
|
| 8 |
except ImportError:
|
|
|
|
| 12 |
except ImportError:
|
| 13 |
pass
|
| 14 |
|
| 15 |
+
# 2. Parche para HfFolder (removido en hf_hub nuevo)
|
| 16 |
+
import huggingface_hub
|
| 17 |
+
if not hasattr(huggingface_hub, "HfFolder"):
|
| 18 |
+
class MockHfFolder:
|
| 19 |
+
@staticmethod
|
| 20 |
+
def get_token(): return os.getenv("HF_TOKEN")
|
| 21 |
+
@staticmethod
|
| 22 |
+
def save_token(token): pass
|
| 23 |
+
huggingface_hub.HfFolder = MockHfFolder
|
| 24 |
+
# ------------------------------------------
|
| 25 |
+
|
| 26 |
import spaces
|
| 27 |
import gradio as gr
|
| 28 |
import torch
|
| 29 |
import numpy as np
|
| 30 |
from PIL import Image
|
| 31 |
+
import tempfile
|
| 32 |
|
| 33 |
+
# CONFIG
|
| 34 |
BASE_MODEL = "cyberdelia/CyberRealisticPony"
|
| 35 |
LTX_MODEL = "Lightricks/LTX-Video"
|
| 36 |
LTX_NSFW_LORA = "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
NEG_DEFAULT = "blurry, low quality, bad anatomy, deformed, ugly, watermark, text"
|
| 38 |
|
|
|
|
| 39 |
def load_t2i(lora_id=None, lora_scale=1.0):
|
|
|
|
| 40 |
from diffusers import StableDiffusionXLPipeline
|
| 41 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 42 |
+
BASE_MODEL, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
|
| 43 |
+
)
|
|
|
|
| 44 |
if lora_id and len(lora_id.strip()) > 5:
|
| 45 |
try:
|
| 46 |
+
pipe.load_lora_weights(lora_id.strip())
|
| 47 |
+
pipe.fuse_lora(lora_scale=lora_scale)
|
|
|
|
| 48 |
except: pass
|
| 49 |
+
return pipe
|
| 50 |
|
| 51 |
def load_video():
|
|
|
|
| 52 |
from diffusers import LTXPipeline
|
| 53 |
+
pipe = LTXPipeline.from_pretrained(LTX_MODEL, torch_dtype=torch.bfloat16)
|
| 54 |
+
try:
|
| 55 |
+
pipe.load_lora_weights(LTX_NSFW_LORA)
|
| 56 |
+
except: pass
|
| 57 |
+
return pipe
|
|
|
|
| 58 |
|
| 59 |
+
@spaces.GPU(duration=120)
|
|
|
|
| 60 |
def generate_t2i(prompt, neg, lora_id, lora_scale, w, h):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
pipe = load_t2i(lora_id, lora_scale).to("cuda")
|
| 62 |
+
img = pipe(prompt=prompt, negative_prompt=neg, num_inference_steps=30,
|
| 63 |
+
guidance_scale=7.0, width=int(w), height=int(h)).images[0]
|
| 64 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
@spaces.GPU(duration=200)
|
| 67 |
def generate_video(prompt, init_image, lora_scale):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
from diffusers.utils import export_to_video
|
| 69 |
pipe = load_video().to("cuda")
|
| 70 |
+
kwargs = {"prompt": prompt, "negative_prompt": NEG_DEFAULT, "num_frames": 49,
|
| 71 |
+
"num_inference_steps": 30}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if init_image is not None:
|
| 73 |
kwargs["image"] = Image.fromarray(init_image).convert("RGB").resize((768, 512))
|
|
|
|
| 74 |
if lora_scale > 0:
|
| 75 |
kwargs["cross_attention_kwargs"] = {"scale": lora_scale}
|
|
|
|
| 76 |
output = pipe(**kwargs)
|
| 77 |
tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 78 |
+
export_to_video(output.frames[0], tmp.name, fps=24)
|
|
|
|
|
|
|
|
|
|
| 79 |
return tmp.name
|
| 80 |
|
|
|
|
| 81 |
with gr.Blocks(title="Image Utility v2.1") as demo:
|
| 82 |
gr.HTML("<h1 style='text-align:center;'>🛠 Image Processing Utility v2.1.4</h1>")
|
|
|
|
| 83 |
with gr.Tabs():
|
| 84 |
with gr.Tab("D-Processor (T2I)"):
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column():
|
| 87 |
t2i_p = gr.Textbox(label="Input Data String", lines=3)
|
| 88 |
t2i_n = gr.Textbox(label="Excluded Data", value=NEG_DEFAULT)
|
| 89 |
+
t2i_lora = gr.Textbox(label="Extension ID")
|
| 90 |
t2i_ls = gr.Slider(0, 1.5, 0.8, label="Extension Weight")
|
| 91 |
with gr.Row():
|
| 92 |
t2i_w = gr.Slider(512, 1024, 1024, step=64, label="X-Axis")
|
| 93 |
t2i_h = gr.Slider(512, 1024, 1024, step=64, label="Y-Axis")
|
| 94 |
+
t2i_btn = gr.Button("Execute Process")
|
| 95 |
t2i_out = gr.Image(label="Output Preview")
|
| 96 |
+
t2i_btn.click(generate_t2i, [t2i_p, t2i_n, t2i_lora, t2i_ls, t2i_w, t2i_h], t2i_out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
with gr.Tab("M-Sequence (Video)"):
|
| 99 |
with gr.Row():
|
|
|
|
| 101 |
v_p = gr.Textbox(label="Motion Vector String", lines=3)
|
| 102 |
v_img = gr.Image(label="Source Buffer", type="numpy")
|
| 103 |
v_ls = gr.Slider(0, 1.5, 0.8, label="Motion Weight")
|
| 104 |
+
v_btn = gr.Button("Process Sequence")
|
| 105 |
v_out = gr.Video(label="Sequence Output")
|
| 106 |
+
v_btn.click(generate_video, [v_p, v_img, v_ls], v_out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
demo.launch()
|