panchadip commited on
Commit
792a32b
·
verified ·
1 Parent(s): 8fdd2bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -1,30 +1,32 @@
1
- from diffusers import StableDiffusionPipeline # Import the StableDiffusionPipeline class from diffusers
 
2
  import torch
 
3
 
4
- # Define the model ID for Stable Diffusion
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
- # Function to generate images based on text prompts
14
- def generate_image(prompt, output_path="generated_image.png"):
15
- # Generate the image
16
- image = pipe(prompt).images[0]
17
-
18
- # Save the generated image
19
- image.save(output_path)
20
- print(f"Image saved at: {output_path}")
21
 
22
- # Example prompts for image generation
23
- prompt1 = "A futuristic city skyline at sunset"
24
- generate_image(prompt1, "image1.png")
 
 
 
 
 
25
 
26
- prompt2 = "A beautiful landscape from mountains during sunrise"
27
- generate_image(prompt2, "image2.png")
 
 
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