File size: 1,241 Bytes
479980a
e47405a
479980a
 
 
e47405a
479980a
e47405a
ba3eabc
decfef7
 
ba3eabc
 
 
e47405a
479980a
 
e47405a
decfef7
e47405a
 
 
 
 
 
 
 
decfef7
 
 
 
ba3eabc
decfef7
 
 
 
ba3eabc
decfef7
 
479980a
 
decfef7
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
from diffusers import StableDiffusionPipeline
import torch
from PIL import Image
import streamlit as st

# Load the Stable Diffusion pipeline
@st.cache(allow_output_mutation=True)
def load_pipeline():
    pipeline = StableDiffusionPipeline.from_pretrained(
        "CompVis/stable-diffusion-v1-4",
        torch_dtype=torch.float32  # Use float32 for CPU support
    )
    device = "cuda" if torch.cuda.is_available() else "cpu"
    pipeline.to(device)
    return pipeline

def main():
    st.title("Stable Diffusion Image Generator")
    st.write("Generate images from text prompts using Stable Diffusion")

    # Initialize the pipeline
    pipeline = load_pipeline()

    # Text input for the prompt
    prompt = st.text_input("Enter your text prompt", "")

    # Generate button
    if st.button("Generate"):
        if not prompt:
            st.warning("Please enter a prompt first.")
            return  # ✅ Fixed indentation

        st.write("Generating your image...")
        with torch.no_grad():
            result = pipeline(prompt)
            image = result.images[0]  # Extract the generated image

        st.write("Generated Image:")
        st.image(image, use_column_width=True)

if __name__ == "__main__":
    main()