amirkhanbloch commited on
Commit
bc8127d
·
verified ·
1 Parent(s): c011679

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -2
app.py CHANGED
@@ -1,4 +1,42 @@
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x)
 
1
+ #import streamlit as st
2
+
3
+ #x = st.slider('Select a value')
4
+ #st.write(x)
5
+
6
+
7
+ import torch
8
+ from diffusers import StableDiffusionPipeline
9
  import streamlit as st
10
+ from PIL import Image
11
+
12
+ # Streamlit UI Setup
13
+ st.title("Stable Diffusion Demo")
14
+
15
+ # Text input for prompt
16
+ prompt = st.text_input("Enter your prompt:", "A sunset over a mountain range")
17
+
18
+ # Load the model once, so that it doesn't reload on every change
19
+ @st.cache_resource
20
+ def load_model():
21
+ pipe = StableDiffusionPipeline.from_pretrained(
22
+ "CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16
23
+ )
24
+ pipe = pipe.to("cuda")
25
+ return pipe
26
+
27
+ pipe = load_model()
28
+
29
+ # Generate image on button click
30
+ if st.button("Generate Image"):
31
+ with st.spinner("Generating image..."):
32
+ image = pipe(prompt).images[0]
33
+
34
+ # Display the generated image in the app
35
+ st.image(image, caption=f"Generated image for prompt: {prompt}")
36
+
37
+ # Optionally, save the image
38
+ if st.button("Save Image"):
39
+ file_name = "generated_image.png"
40
+ image.save(file_name)
41
+ st.success(f"Image saved as {file_name}")
42