Spaces:
Build error
Build error
Download app.py from sal-maq/Image-Generation-Stable-Diffusion: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/sal-maq/Image-Generation-Stable-Diffusion/resolve/main/app.py
- Command line
-
hf download hf://spaces/sal-maq/Image-Generation-Stable-Diffusion/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sal-maq/Image-Generation-Stable-Diffusion/resolve/main/app.py
1.46 kB
| # Import the necessary libraries | |
| from diffusers import StableDiffusionPipeline | |
| import torch | |
| from PIL import Image | |
| import gradio as gr | |
| # Load the Stable Diffusion model | |
| model_id = "runwayml/stable-diffusion-v1-5" | |
| pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16) | |
| # Use CPU as fallback if CUDA is not available | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| pipe = pipe.to(device) | |
| # Function to generate an image | |
| def generate_image(prompt): | |
| image = pipe(prompt).images[0] | |
| return image | |
| # Define the Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.HTML( | |
| """ | |
| <div style="text-align: center;"> | |
| <h1>🌄 Generate Stunning Images with Stable Diffusion</h1> | |
| <h3>Type a prompt to create a beautiful image:</h3> | |
| </div> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| prompt = gr.Textbox(label="Enter your prompt here", placeholder="e.g., beautiful and realistic mountain scenery 🌄", lines=1) | |
| submit_button = gr.Button("Generate Image 🚀") | |
| with gr.Column(): | |
| image_output = gr.Image(label="Generated Image", type="pil") | |
| submit_button.click(fn=generate_image, inputs=prompt, outputs=image_output) | |
| gr.HTML( | |
| """ | |
| <div style="text-align: center;"> | |
| <h4>Developed by Salman Maqbool ✨</h4> | |
| </div> | |
| """ | |
| ) | |
| # Launch the Gradio app | |
| demo.launch() | |