File size: 1,185 Bytes
4c460a8
 
 
 
 
 
 
 
 
a1cebdc
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
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"
    
    # Load the model
    pipe = StableDiffusionPipeline.from_pretrained(model_id)
    
    # Check if CUDA is available and use it, otherwise fall back to CPU
    if torch.cuda.is_available():
        pipe = pipe.to("cuda")
    else:
        pipe = pipe.to("cpu")
        
    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.")