Download app.py from Geek7/Testing3: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/spaces/Geek7/Testing3/resolve/main/app.py
- Command line
-
hf download hf://spaces/Geek7/Testing3/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Geek7/Testing3/resolve/main/app.py
1.87 kB
| 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 | |
| # Define a function to generate the image | |
| 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" # Use the correct ckpt for your step setting! | |
| # Load model. | |
| 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")) | |
| # Ensure sampler uses "trailing" timesteps. | |
| pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing") | |
| # Generate image | |
| image = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0).images[0] | |
| return image | |
| # Main function for Streamlit app | |
| def main(): | |
| st.title("AI Image Generator") | |
| # Input fields | |
| 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"): | |
| # Check if prompt is provided | |
| if prompt: | |
| # Generate image | |
| generated_image = generate_image(prompt, num_inference_steps) | |
| # Save image | |
| generated_image.save("output.png") | |
| # Display image | |
| st.image(generated_image, caption='Generated Image', use_column_width=True) | |
| else: | |
| st.error("Please enter a prompt.") | |
| if __name__ == "__main__": | |
| main() |