cobramv12's picture
Fix: System-level monkeypatch for Gradio stability
0c5ac88 verified
raw
history blame
4.34 kB
import sys
import os
# --- PARCHE QUIRÚRGICO DE NIVEL SISTEMA ---
# 1. Parche de audioop para Python 3.13
try:
import audioop
except ImportError:
try:
import audioop_lts as audioop
sys.modules["audioop"] = audioop
except ImportError:
pass
# 2. Parche para el bug de Gradio (TypeError: argument of type 'bool' is not iterable)
import gradio_client.utils as client_utils
original_get_type = client_utils.get_type
def patched_get_type(schema):
if isinstance(schema, bool):
return "str" # Evita el colapso si schema es un booleano
return original_get_type(schema)
client_utils.get_type = patched_get_type
# ------------------------------------------
import spaces
import gradio as gr
import torch
import numpy as np
from PIL import Image
import tempfile
# CONFIG
BASE_MODEL = "cyberdelia/CyberRealisticPony"
LTX_MODEL = "Lightricks/LTX-Video"
LTX_NSFW_LORA = "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps"
NEG_DEFAULT = "blurry, low quality, bad anatomy, deformed, ugly, watermark, text"
pipe_t2i = None
pipe_video = None
def load_t2i(lora_id=None, lora_scale=1.0):
from diffusers import StableDiffusionXLPipeline
pipe = StableDiffusionXLPipeline.from_pretrained(
BASE_MODEL, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
)
if lora_id and len(lora_id.strip()) > 5:
try:
pipe.load_lora_weights(lora_id.strip())
pipe.fuse_lora(lora_scale=lora_scale)
except: pass
return pipe
def load_video():
from diffusers import LTXPipeline
pipe = LTXPipeline.from_pretrained(LTX_MODEL, torch_dtype=torch.bfloat16)
try:
pipe.load_lora_weights(LTX_NSFW_LORA)
except: pass
return pipe
@spaces.GPU(duration=100)
def generate_t2i(prompt, neg, lora_id, lora_scale, w, h):
pipe = load_t2i(lora_id, lora_scale).to("cuda")
img = pipe(prompt=prompt, negative_prompt=neg, num_inference_steps=30,
guidance_scale=7.0, width=int(w), height=int(h),
generator=torch.Generator("cuda").manual_seed(42)).images[0]
return img
@spaces.GPU(duration=200)
def generate_video(prompt, init_image, lora_scale):
from diffusers.utils import export_to_video
pipe = load_video().to("cuda")
kwargs = {"prompt": prompt, "negative_prompt": NEG_DEFAULT, "num_frames": 49,
"num_inference_steps": 30, "generator": torch.Generator("cuda").manual_seed(42)}
if init_image is not None:
kwargs["image"] = Image.fromarray(init_image).convert("RGB").resize((768, 512))
if lora_scale > 0:
kwargs["cross_attention_kwargs"] = {"scale": lora_scale}
output = pipe(**kwargs)
tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
export_to_video(output.frames[0], tmp.name, fps=24)
return tmp.name
# UI
with gr.Blocks(title="Image Utility v2.1") as demo:
gr.HTML("<h1 style='text-align:center;'>🛠 Image Processing Utility v2.1.4</h1>")
with gr.Tabs():
with gr.Tab("D-Processor (T2I)"):
with gr.Row():
with gr.Column():
t2i_p = gr.Textbox(label="Input Data String", lines=3)
t2i_n = gr.Textbox(label="Excluded Data", value=NEG_DEFAULT)
t2i_lora = gr.Textbox(label="Extension ID")
t2i_ls = gr.Slider(0, 1.5, 0.8, label="Extension Weight")
with gr.Row():
t2i_w = gr.Slider(512, 1024, 1024, step=64, label="X-Axis")
t2i_h = gr.Slider(512, 1024, 1024, step=64, label="Y-Axis")
t2i_btn = gr.Button("Execute Process")
t2i_out = gr.Image(label="Output Preview")
t2i_btn.click(generate_t2i, [t2i_p, t2i_n, t2i_lora, t2i_ls, t2i_w, t2i_h], t2i_out)
with gr.Tab("M-Sequence (Video)"):
with gr.Row():
with gr.Column():
v_p = gr.Textbox(label="Motion Vector String", lines=3)
v_img = gr.Image(label="Source Buffer", type="numpy")
v_ls = gr.Slider(0, 1.5, 0.8, label="Motion Weight")
v_btn = gr.Button("Process Sequence")
v_out = gr.Video(label="Sequence Output")
v_btn.click(generate_video, [v_p, v_img, v_ls], v_out)
demo.launch()