text2image / app.py
balaramas's picture
Update app.py
f8e211a verified
import gradio as gr
import subprocess
subprocess.check_call(["pip", "install", "safetensors"])
subprocess.check_call(["pip", "install", "transformers"])
subprocess.check_call(["pip", "install", "torch"])
subprocess.check_call(["pip", "install", "diffusers"])
subprocess.check_call(["pip", "install", "accelerate"])
import torch
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
base = "stabilityai/stable-diffusion-xl-base-1.0"
repo = "ByteDance/SDXL-Lightning"
ckpt = "sdxl_lightning_2step_unet.safetensors" # Use the correct ckpt for your step setting!
# Load model.
unet = UNet2DConditionModel.from_config(base, subfolder="unet")
unet.load_state_dict(load_file(hf_hub_download(repo, ckpt)))
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16")
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
def generate_image(text):
pipe("krishna", num_inference_steps=2, guidance_scale=0).images[0].save("output.png")
return "output.png"
# Create a Gradio interface
iface = gr.Interface(
fn=generate_image,
inputs=gr.Textbox(lines=5, label="Enter a description for the image"),
outputs=gr.Image(type="filepath", label="Generated Image"),
title="Text to Image Generation",
description="Enter a text description and get an image.",
theme="compact"
)
# Launch the Gradio app
iface.launch()