Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Title and description of the app
|
7 |
+
st.title("🖼️ Stable Diffusion Image Generator")
|
8 |
+
st.write("Generate images from text using the Stable Diffusion v1.5 model!")
|
9 |
+
|
10 |
+
# Sidebar for user inputs
|
11 |
+
st.sidebar.title("Input Options")
|
12 |
+
prompt = st.sidebar.text_input("Enter your prompt", "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k")
|
13 |
+
generate_button = st.sidebar.button("Generate Image")
|
14 |
+
|
15 |
+
# Load the pipeline when the app starts
|
16 |
+
@st.cache_resource
|
17 |
+
def load_pipeline():
|
18 |
+
pipe = DiffusionPipeline.from_pretrained(
|
19 |
+
"runwayml/stable-diffusion-v1-5",
|
20 |
+
torch_dtype=torch.float16
|
21 |
+
)
|
22 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
+
return pipe.to(device)
|
24 |
+
|
25 |
+
pipe = load_pipeline()
|
26 |
+
|
27 |
+
# Generate image when button is clicked
|
28 |
+
if generate_button:
|
29 |
+
st.write(f"### Prompt: {prompt}")
|
30 |
+
with st.spinner("Generating image... Please wait."):
|
31 |
+
# Generate the image
|
32 |
+
image = pipe(prompt).images[0]
|
33 |
+
|
34 |
+
# Display the generated image
|
35 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
36 |
+
|
37 |
+
# Option to download the image
|
38 |
+
img_path = "generated_image.png"
|
39 |
+
image.save(img_path)
|
40 |
+
with open(img_path, "rb") as img_file:
|
41 |
+
st.download_button(
|
42 |
+
label="Download Image",
|
43 |
+
data=img_file,
|
44 |
+
file_name="generated_image.png",
|
45 |
+
mime="image/png"
|
46 |
+
)
|