File size: 1,644 Bytes
19fa8b2
1a2afdb
5504e73
19fa8b2
96a7542
5504e73
96a7542
21b9e96
1a2afdb
5504e73
96a7542
 
 
 
 
 
 
 
 
1a2afdb
96a7542
1a2afdb
19fa8b2
96a7542
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69f9f13
96a7542
 
 
 
 
 
 
 
 
 
 
69f9f13
96a7542
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
from diffusers import StableDiffusionPipeline
from PIL import Image
import torch
import os

device = "cuda" if torch.cuda.is_available() else "cpu"

model_id = "OFA-Sys/small-stable-diffusion-v0"

try:
    pipe = StableDiffusionPipeline.from_pretrained(model_id)
    pipe = pipe.to(device)
    print(f"Modelo cargado y movido a: {device.upper()}")
except Exception as e:
    print(f"Error al cargar el modelo: {e}")
    pipe = None


def generar_imagen(prompt):
    """Genera una imagen a partir de un prompt usando Stable Diffusion."""
    if not prompt:
        return None
    if pipe is None:
        return Image.new('RGB', (512, 512), color = 'red') 
    
    try:
        image = pipe(prompt).images[0] 
        return image
    except Exception as e:
        print(f"Error al generar la imagen: {e}")
        return Image.new('RGB', (512, 512), color = 'red')


with gr.Blocks(title="Generador de Imágenes Ligero") as demo:
    with gr.Row():
        with gr.Column(scale=1):
            prompt_input = gr.Textbox(
                label="Prompt (Describe la imagen que quieres)", 
                placeholder="Un astronauta montando a caballo, estilo fotorealista"
            )
            generate_button = gr.Button("🖼️ Generar Imagen")
        
        with gr.Column(scale=1):
            image_output = gr.Image(
                type="pil",
                label="Imagen Generada",
                height=512,
                width=512
            )
    
    generate_button.click(
        fn=generar_imagen, 
        inputs=prompt_input, 
        outputs=image_output
    )


demo.launch(inbrowser=True)