Dua Rajper commited on
Commit
decfef7
·
verified ·
1 Parent(s): 693eeee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -19
app.py CHANGED
@@ -2,25 +2,21 @@ from diffusers import StableDiffusionPipeline
2
  import torch
3
  from PIL import Image
4
  import streamlit as st
5
- import sys
6
- print(sys.version)
7
-
8
 
9
  # Load the Stable Diffusion pipeline
10
  @st.cache(allow_output_mutation=True)
11
  def load_pipeline():
12
  pipeline = StableDiffusionPipeline.from_pretrained(
13
- "CompVis/stable-diffusion-v1-4",
14
- torch_dtype=torch.float32 # Change to float32 for CPU
15
  )
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
  pipeline.to(device)
18
  return pipeline
19
 
20
-
21
  def main():
22
  st.title("Stable Diffusion Image Generator")
23
- st.write("Generate images from text prompts using Stable Diffusion 2.1")
24
 
25
  # Initialize the pipeline
26
  pipeline = load_pipeline()
@@ -29,19 +25,18 @@ def main():
29
  prompt = st.text_input("Enter your text prompt", "")
30
 
31
  # Generate button
32
- if st.button("Generate"):
33
- if not prompt:
34
- st.warning("Please enter a prompt first.")
35
- return
36
 
37
- st.write("Generating your image...")
38
- with torch.no_grad():
39
- result = pipeline(prompt) # Returns a dictionary
40
- image = result.images[0] # Extract the first generated image
41
 
42
- st.write("Generated Image:")
43
- st.image(image, use_column_width=True)
44
-
45
 
46
  if __name__ == "__main__":
47
- main()
 
2
  import torch
3
  from PIL import Image
4
  import streamlit as st
 
 
 
5
 
6
  # Load the Stable Diffusion pipeline
7
  @st.cache(allow_output_mutation=True)
8
  def load_pipeline():
9
  pipeline = StableDiffusionPipeline.from_pretrained(
10
+ "CompVis/stable-diffusion-v1-4",
11
+ torch_dtype=torch.float32 # Use float32 for CPU support
12
  )
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
  pipeline.to(device)
15
  return pipeline
16
 
 
17
  def main():
18
  st.title("Stable Diffusion Image Generator")
19
+ st.write("Generate images from text prompts using Stable Diffusion")
20
 
21
  # Initialize the pipeline
22
  pipeline = load_pipeline()
 
25
  prompt = st.text_input("Enter your text prompt", "")
26
 
27
  # Generate button
28
+ if st.button("Generate"):
29
+ if not prompt:
30
+ st.warning("Please enter a prompt first.")
31
+ return # ✅ Fixed indentation
32
 
33
+ st.write("Generating your image...")
34
+ with torch.no_grad():
35
+ result = pipeline(prompt)
36
+ image = result.images[0] # Extract the generated image
37
 
38
+ st.write("Generated Image:")
39
+ st.image(image, use_column_width=True)
 
40
 
41
  if __name__ == "__main__":
42
+ main()