Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,32 @@
|
|
1 |
-
|
|
|
2 |
import torch
|
|
|
3 |
|
4 |
-
#
|
5 |
model_id = "CompVis/stable-diffusion-v1-4"
|
6 |
-
|
7 |
-
# Load the pre-trained model
|
8 |
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
9 |
|
10 |
# Move the model to GPU if available, otherwise use CPU
|
11 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# Save the generated image
|
19 |
-
image.save(output_path)
|
20 |
-
print(f"Image saved at: {output_path}")
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
|
29 |
-
prompt3 = "A beautiful landscape from mountains during sunset"
|
30 |
-
generate_image(prompt3, "image3.png")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
import torch
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# Load the Stable Diffusion model
|
7 |
model_id = "CompVis/stable-diffusion-v1-4"
|
|
|
|
|
8 |
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
9 |
|
10 |
# Move the model to GPU if available, otherwise use CPU
|
11 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
|
13 |
+
# Streamlit app layout
|
14 |
+
st.title("Text-to-Image Generation using Stable Diffusion")
|
15 |
+
|
16 |
+
# Text input for prompt
|
17 |
+
prompt = st.text_input("Enter a prompt to generate an image:", value="A futuristic city skyline at sunset")
|
|
|
|
|
|
|
18 |
|
19 |
+
# Button to trigger image generation
|
20 |
+
if st.button("Generate Image"):
|
21 |
+
with st.spinner("Generating image..."):
|
22 |
+
# Generate image based on prompt
|
23 |
+
image = pipe(prompt).images[0]
|
24 |
+
|
25 |
+
# Display the image
|
26 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
27 |
|
28 |
+
# Option to save the generated image
|
29 |
+
output_path = "generated_image.png"
|
30 |
+
image.save(output_path)
|
31 |
+
st.success(f"Image saved as {output_path}")
|
32 |
|
|
|
|