Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,36 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from diffusers import AutoPipelineForImage2Image
|
4 |
+
from diffusers.utils import make_image_grid, load_image
|
5 |
|
6 |
+
# Streamlit UI
|
7 |
+
st.title("Stable Diffusion Image-to-Image & Slider Demo")
|
8 |
+
|
9 |
+
# Slider input
|
10 |
+
x = st.slider('Select a value', 1, 100, 10)
|
11 |
+
st.write(x, 'squared is', x * x)
|
12 |
+
|
13 |
+
# Image generation pipeline
|
14 |
+
@st.cache_resource()
|
15 |
+
def load_pipeline():
|
16 |
+
pipeline = AutoPipelineForImage2Image.from_pretrained(
|
17 |
+
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
18 |
+
)
|
19 |
+
pipeline.enable_model_cpu_offload()
|
20 |
+
pipeline.enable_xformers_memory_efficient_attention()
|
21 |
+
return pipeline
|
22 |
+
|
23 |
+
pipeline = load_pipeline()
|
24 |
+
|
25 |
+
# Image processing
|
26 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
|
27 |
+
init_image = load_image(url)
|
28 |
+
|
29 |
+
prompt = st.text_input("Enter a prompt", "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k")
|
30 |
+
|
31 |
+
generate_button = st.button("Generate Image")
|
32 |
+
|
33 |
+
if generate_button:
|
34 |
+
with st.spinner("Generating..."):
|
35 |
+
image = pipeline(prompt, image=init_image).images[0]
|
36 |
+
st.image(make_image_grid([init_image, image], rows=1, cols=2), caption=["Original", "Generated"], use_column_width=True)
|