|
import streamlit as st |
|
import torch |
|
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler |
|
from huggingface_hub import hf_hub_download |
|
from safetensors.torch import load_file |
|
|
|
|
|
def generate_image(prompt, num_inference_steps): |
|
base = "stabilityai/stable-diffusion-xl-base-1.0" |
|
repo = "ByteDance/SDXL-Lightning" |
|
ckpt = "sdxl_lightning_2step_unet.safetensors" |
|
|
|
|
|
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to(torch.device("cpu"), torch.float16) |
|
unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=torch.device("cpu"))) |
|
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to(torch.device("cpu")) |
|
|
|
|
|
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing") |
|
|
|
|
|
image = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0).images[0] |
|
|
|
return image |
|
|
|
|
|
def main(): |
|
st.title("AI Image Generator") |
|
|
|
|
|
prompt = st.text_input("Enter prompt") |
|
num_inference_steps = st.slider("Number of Inference Steps", min_value=1, max_value=10, value=2) |
|
|
|
if st.button("Generate Image"): |
|
|
|
if prompt: |
|
|
|
generated_image = generate_image(prompt, num_inference_steps) |
|
|
|
generated_image.save("output.png") |
|
|
|
st.image(generated_image, caption='Generated Image', use_column_width=True) |
|
else: |
|
st.error("Please enter a prompt.") |
|
|
|
if __name__ == "__main__": |
|
main() |