snehakingrani commited on
Commit
decdace
·
verified ·
1 Parent(s): 81c7a6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -1,30 +1,25 @@
1
  import streamlit as st
2
- from diffusers import StableDiffusionPipeline
3
  import torch
 
 
4
 
5
- # Function to load the model
6
- @st.cache_resource
7
  def load_model():
8
  model_id = "CompVis/stable-diffusion-v1-4"
9
- pipe = StableDiffusionPipeline.from_pretrained(model_id)
10
- pipe.to("cpu") # Ensure the model runs on CPU
 
11
  return pipe
12
 
13
- # Load the model
14
  pipe = load_model()
15
 
16
- # Streamlit UI
17
- st.title("Text-to-Image Generator")
18
- st.write("Enter a description, and the AI model will generate an image!")
19
-
20
- # User input
21
- text_input = st.text_input("Enter your description:")
22
 
23
- # Generate and display image
24
- if text_input:
25
- with st.spinner("Generating image... Please wait ⏳"):
26
- try:
27
- image = pipe(text_input).images[0] # Generate image
28
- st.image(image, caption="Generated Image", use_column_width=True)
29
- except Exception as e:
30
- st.error(f"Error generating image: {e}")
 
1
  import streamlit as st
2
+ from transformers import CLIPTextModel, CLIPTokenizer
3
  import torch
4
+ from torch import autocast
5
+ from diffusers import StableDiffusionPipeline
6
 
7
+ # Load the model
8
+ @st.cache(allow_output_mutation=True)
9
  def load_model():
10
  model_id = "CompVis/stable-diffusion-v1-4"
11
+ device = "cpu"
12
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True)
13
+ pipe.to(device)
14
  return pipe
15
 
 
16
  pipe = load_model()
17
 
18
+ # Streamlit app
19
+ st.title("Text to Image Generator")
20
+ prompt = st.text_input("Enter your prompt:", "A scenic landscape of mountains during sunset")
 
 
 
21
 
22
+ if st.button("Generate Image"):
23
+ with st.spinner("Generating..."):
24
+ image = pipe(prompt)["sample"][0]
25
+ st.image(image, caption=prompt)