File size: 1,056 Bytes
792a32b
 
2c57eba
792a32b
2c57eba
792a32b
2c57eba
 
 
 
 
 
792a32b
 
 
 
 
2c57eba
792a32b
 
 
 
 
 
 
 
2c57eba
792a32b
 
 
 
2c57eba
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
import streamlit as st
from diffusers import StableDiffusionPipeline
import torch
from PIL import Image

# Load the Stable Diffusion model
model_id = "CompVis/stable-diffusion-v1-4"
pipe = StableDiffusionPipeline.from_pretrained(model_id)

# Move the model to GPU if available, otherwise use CPU
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")

# Streamlit app layout
st.title("Text-to-Image Generation using Stable Diffusion")

# Text input for prompt
prompt = st.text_input("Enter a prompt to generate an image:", value="A futuristic city skyline at sunset")

# Button to trigger image generation
if st.button("Generate Image"):
    with st.spinner("Generating image..."):
        # Generate image based on prompt
        image = pipe(prompt).images[0]
        
        # Display the image
        st.image(image, caption="Generated Image", use_column_width=True)

        # Option to save the generated image
        output_path = "generated_image.png"
        image.save(output_path)
        st.success(f"Image saved as {output_path}")