File size: 1,022 Bytes
4c460a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import streamlit as st
from diffusers import StableDiffusionPipeline
import torch
from PIL import Image

# Load the stable diffusion model
@st.cache_resource
def load_model():
    model_id = "runwayml/stable-diffusion-v1-5"
    pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
    pipe = pipe.to("cuda")
    return pipe

pipe = load_model()

# Streamlit app title
st.title("Stable Diffusion Image Generator")

# User input for text prompt
prompt = st.text_input("Enter your image prompt:", "")

# Button to generate image
if st.button("Generate Image"):
    if prompt:
        # Generate the image
        with st.spinner("Generating..."):
            image = pipe(prompt).images[0]
            st.image(image, caption=prompt, use_column_width=True)
            
            # Save the image
            image.save(f"{prompt[:50].replace(' ', '_')}.png")
            st.success("Image generated successfully!")
    else:
        st.warning("Please enter a prompt to generate an image.")