juanelot's picture
otr
294e509 verified
raw
history blame
2.65 kB
import gradio as gr
from diffusers import DiffusionPipeline
import torch
# Cargar ambos modelos base y refiner
base = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)
base.to("cuda")
refiner = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0",
text_encoder_2=base.text_encoder_2,
vae=base.vae,
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16",
)
refiner.to("cuda")
# Definir cuántos pasos y qué porcentaje de pasos ejecutar en cada experto (80/20) aquí
n_steps = 40
high_noise_frac = 0.8
# Definir función para generar imagen
def generate_image(prompt):
image = base(
prompt=prompt,
num_inference_steps=n_steps,
denoising_end=high_noise_frac,
output_type="latent",
).images
image = refiner(
prompt=prompt,
num_inference_steps=n_steps,
denoising_start=high_noise_frac,
image=image,
).images[0]
return image
# Crear la interfaz de Gradio
iface = gr.Interface(
fn=generate_image,
inputs=gr.Textbox(lines=5, label="Descripción de la imagen", placeholder="Introduce el texto aquí..."),
outputs=gr.Image(type="pil", label="Imagen generada"),
title="Generador de Imágenes a partir de Texto",
description="Ingrese un texto y obtenga una imagen generada por IA.",
examples=[
["A picturesque view of a local market. The first light of day illuminates the stone facades and worn tiles of the houses and buildings, some of which date back centuries. At the center of the scene, a cobblestone square leads to an open-air market that begins to come to life, with vendors setting up their stalls selling fruits, vegetables, flowers, and local crafts. The narrow, winding streets are lined with old lanterns, now unlit, while lazy cats lounge on the stone steps. In one corner, an ancient fountain, adorned with weathered carvings, murmurs softly, adding to the tranquil atmosphere. In the background, the towers of an ancient cathedral rise, capturing the first rays of sunlight that paint the sky in soft pinks and oranges. This image should convey a sense of tranquility, beauty, and a deep connection to the past, celebrating the rich history and timeless charm of the ancient village or town."]
],
live=True,
css="""
.gradio-container {
background-color: #f9f9f9;
color: #333;
font-family: Arial, sans-serif;
}
.gradio-container h1 {
color: #4CAF50;
}
"""
)
# Ejecutar la interfaz
iface.launch()