Dua Rajper commited on
Commit
e47405a
·
verified ·
1 Parent(s): edb923b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -1,37 +1,37 @@
1
  from diffusers import StableDiffusionPipeline
 
2
  from PIL import Image
3
  import streamlit as st
4
- import torch
5
- from transformers import CLIPTextModel, CLIPFeatureExtractor
6
- import numpy as np
7
- import matplotlib.pyplot as plt
8
 
9
- # Load the model and tokenizer
10
  @st.cache(allow_output_mutation=True)
11
- def load_model():
12
- model_id = "CompVis/stable-diffusion-2-1"
13
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
14
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
15
- return pipe
16
 
17
- # Main function
18
  def main():
19
- st.title("Stable Diffusion 2.1 Image Generator")
20
- st.header("Generate images from your prompts!")
21
-
22
- prompt = st.text_input("Enter your prompt:", "")
23
- steps = st.slider("Number of steps:", min_value=20, max_value=200, value=50)
24
- guidance = st.slider("Guidance scale:", min_value=1.0, max_value=20.0, value=7.5)
25
-
 
 
 
26
  if st.button("Generate"):
27
  if not prompt:
28
- st.error("Please enter a prompt")
29
  return
30
-
31
- with st.spinner("Generating image..."):
32
- pipe = load_model()
33
- image = pipe(prompt, guidance_scale=guidance, num_inference_steps=steps).images[0]
34
- st.image(image, caption="Generated Image")
 
 
35
 
36
  if __name__ == "__main__":
37
  main()
 
1
  from diffusers import StableDiffusionPipeline
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("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
10
+ pipeline = pipeline.to("cuda" if torch.cuda.is_available() else "cpu")
11
+ return pipeline
 
12
 
 
13
  def main():
14
+ st.title("Stable Diffusion Image Generator")
15
+ st.write("Generate images from text prompts using Stable Diffusion 2.1")
16
+
17
+ # Initialize the pipeline
18
+ pipeline = load_pipeline()
19
+
20
+ # Text input for the prompt
21
+ prompt = st.text_input("Enter your text prompt", "")
22
+
23
+ # Generate button
24
  if st.button("Generate"):
25
  if not prompt:
26
+ st.warning("Please enter a prompt first.")
27
  return
28
+
29
+ st.write("Generating your image...")
30
+ with torch.no_grad():
31
+ image = pipeline(prompt).images[0]
32
+
33
+ st.write("Generated Image:")
34
+ st.image(image, use_column_width=True)
35
 
36
  if __name__ == "__main__":
37
  main()