import gradio as gr import torch from diffusers import DiffusionPipeline import random import numpy as np import time # ============================================== # CONFIGURACIÓN DE MODELOS (SOLO LOS VIABLES) # ============================================== AVAILABLE_MODELS = { "⚡ Turbo (SD-Turbo)": { "id": "stabilityai/sd-turbo", "guidance_default": 1.0, "steps_default": 4, "max_steps": 8, }, "🌸 Anime (LCM Dreamshaper)": { "id": "SimianLuo/LCM_Dreamshaper_v7", "guidance_default": 1.0, "steps_default": 4, "max_steps": 8, }, "🎨 Arte (OpenJourney)": { "id": "prompthero/openjourney", "guidance_default": 7.5, "steps_default": 20, "max_steps": 50, }, "📸 Realista (Realistic Vision)": { "id": "SG161222/Realistic_Vision_V5.1_noVAE", "guidance_default": 7.5, "steps_default": 20, "max_steps": 50, }, "🔞 NSFW (sd-nsfw)": { "id": "Kernel/sd-nsfw", "guidance_default": 7.5, "steps_default": 20, "max_steps": 50, }, "💨 Ultra-Ligero (SSD-1B)": { "id": "segmind/SSD-1B", "guidance_default": 1.0, "steps_default": 4, "max_steps": 8, }, } device = "cuda" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 current_pipe = None current_model_id = None def load_model(model_key): global current_pipe, current_model_id if current_model_id == model_key and current_pipe is not None: return current_pipe model_info = AVAILABLE_MODELS[model_key] model_id = model_info["id"] print(f"Cargando {model_id}...") if current_pipe is not None: del current_pipe torch.cuda.empty_cache() if torch.cuda.is_available() else None pipe = DiffusionPipeline.from_pretrained( model_id, torch_dtype=torch_dtype, ) pipe.safety_checker = None pipe = pipe.to(device) if device == "cpu": pipe.enable_attention_slicing() current_pipe = pipe current_model_id = model_key return pipe def infer(model_choice, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress()): pipe = load_model(model_choice) model_info = AVAILABLE_MODELS[model_choice] if randomize_seed: seed = random.randint(0, np.iinfo(np.int32).max) generator = torch.Generator(device=device).manual_seed(seed) progress(0.2, desc=f"🎨 Generando con {model_choice}...") start_time = time.time() image = pipe( prompt=prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, width=width, height=height, generator=generator, ).images[0] duration = round(time.time() - start_time, 2) return image, seed, f"✅ {model_choice} | {duration}s | Semilla: {seed}" # ============================================== # INTERFAZ # ============================================== css = """ .gradio-container { max-width: 800px !important; margin: auto !important; } """ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo: gr.Markdown("# 🎨 Generador Multi-Modelo (Optimizado para 16GB RAM)") gr.Markdown("_Elige el modelo según el estilo que quieras. Cada uno se carga bajo demanda._") with gr.Row(): with gr.Column(scale=1): model_choice = gr.Dropdown( choices=list(AVAILABLE_MODELS.keys()), label="Modelo", value="⚡ Turbo (SD-Turbo)" ) with gr.Column(scale=3): prompt = gr.Textbox( label="Prompt", placeholder="Describe lo que quieres ver...", lines=2 ) with gr.Column(scale=1): run_btn = gr.Button("🚀 Generar", variant="primary", size="lg") result = gr.Image(label="Imagen", show_label=True) info_text = gr.Markdown("💡 Selecciona un modelo y escribe tu prompt.") with gr.Accordion("⚙️ Ajustes", open=False): with gr.Row(): width = gr.Slider(label="Ancho", minimum=256, maximum=768, step=64, value=512) height = gr.Slider(label="Alto", minimum=256, maximum=768, step=64, value=512) with gr.Row(): guidance_scale = gr.Slider( label="Escala de Guía", minimum=0.0, maximum=10.0, step=0.1, value=1.0, ) num_inference_steps = gr.Slider( label="Pasos", minimum=1, maximum=50, step=1, value=4, ) with gr.Row(): seed = gr.Slider(label="Semilla", minimum=0, maximum=np.iinfo(np.int32).max, step=1, value=0) randomize_seed = gr.Checkbox(label="Aleatorizar semilla", value=True) prompt.submit( fn=infer, inputs=[model_choice, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], outputs=[result, seed, info_text] ) run_btn.click( fn=infer, inputs=[model_choice, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], outputs=[result, seed, info_text] ) if __name__ == "__main__": demo.launch()