import sys, os, gc, torch, spaces, tempfile import gradio as gr from PIL import Image # PATCH GRADIO try: import gradio_client.utils as client_utils if not hasattr(client_utils, "_old_json_schema_to_python_type"): client_utils._old_json_schema_to_python_type = client_utils._json_schema_to_python_type def patched_json_schema_to_python_type(schema, defs=None): if isinstance(schema, bool): return "Any" return client_utils._old_json_schema_to_python_type(schema, defs) client_utils._json_schema_to_python_type = patched_json_schema_to_python_type except: pass def flush(): gc.collect() if torch.cuda.is_available(): torch.cuda.empty_cache() MODELS = {"Pony Diffusion V6 XL": "cyberdelia/CyberRealisticPony"} LORAS = { "Ninguno": "", "💎 NSFW: Real Nudity": "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps", "📜 DOCS: ID Card": "j0rdan/passport-sdxl", "🔫 WEAPONS: Tactical": "Ostris/SDXL_LoRA_Test" } @spaces.GPU(duration=120) def generate(prompt, lora_name, w, h, init_img=None, strength=0.6): flush() from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline p = f"score_9, score_8_up, score_7_up, {prompt}" pipe = StableDiffusionXLPipeline.from_pretrained("cyberdelia/CyberRealisticPony", torch_dtype=torch.float16, variant="fp16", low_cpu_mem_usage=True).to("cuda") lid = LORAS.get(lora_name) if lid: try: pipe.load_lora_weights(lid) except: pass if init_img: pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_pipe(pipe) res = pipe_i2i(prompt=p, image=init_img, strength=strength, num_inference_steps=25).images[0] del pipe_i2i else: res = pipe(prompt=p, num_inference_steps=30, width=int(w), height=int(h)).images[0] del pipe flush() return res @spaces.GPU(duration=250) def video(prompt, init_img): flush() from diffusers import LTXPipeline from diffusers.utils import export_to_video pipe = LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16, low_cpu_mem_usage=True).to("cuda") kw = {"prompt": prompt, "num_inference_steps": 20, "num_frames": 25, "width": 704, "height": 480} if init_img: kw["image"] = init_img out = pipe(**kw) tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) export_to_video(out.frames[0], tmp.name, fps=16) del pipe flush() return tmp.name with gr.Blocks() as demo: gr.HTML("