Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,53 +3,48 @@ from diffusers import StableDiffusionPipeline
|
|
3 |
import torch
|
4 |
from PIL import Image
|
5 |
|
6 |
-
#
|
7 |
st.title("🖼️ Stable Diffusion Image Generator")
|
8 |
-
st.write("Generate images from text using
|
9 |
|
10 |
-
# Sidebar for
|
11 |
-
st.sidebar.title("
|
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 |
-
#
|
16 |
@st.cache_resource
|
17 |
-
def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
22 |
-
revision="fp16" if torch.cuda.is_available() else None
|
23 |
-
)
|
24 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
25 |
-
pipe = pipe.to(device)
|
26 |
-
return pipe
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
# Generate image when button is clicked
|
31 |
if generate_button:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
image = pipe(prompt).images[0]
|
38 |
-
|
39 |
-
# Display the generated image
|
40 |
-
st.image(image, caption="Generated Image", use_column_width=True)
|
41 |
-
|
42 |
-
# Option to download the image
|
43 |
-
img_path = "generated_image.png"
|
44 |
-
image.save(img_path)
|
45 |
-
with open(img_path, "rb") as img_file:
|
46 |
-
st.download_button(
|
47 |
-
label="Download Image",
|
48 |
-
data=img_file,
|
49 |
-
file_name="generated_image.png",
|
50 |
-
mime="image/png"
|
51 |
-
)
|
52 |
-
except Exception as e:
|
53 |
-
st.error(f"An error occurred: {e}")
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import torch
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Streamlit App Title
|
7 |
st.title("🖼️ Stable Diffusion Image Generator")
|
8 |
+
st.write("Generate images from text prompts using Stable Diffusion!")
|
9 |
|
10 |
+
# Sidebar for prompt input and button
|
11 |
+
st.sidebar.title("Image Generation Settings")
|
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 |
+
# Cache the Stable Diffusion model to load it only once
|
16 |
@st.cache_resource
|
17 |
+
def load_model():
|
18 |
+
model = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
19 |
+
model = model.to("cuda" if torch.cuda.is_available() else "cpu")
|
20 |
+
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Load the model
|
23 |
+
pipe = load_model()
|
24 |
|
25 |
+
# Generate the image when the button is clicked
|
26 |
if generate_button:
|
27 |
+
if prompt.strip() == "":
|
28 |
+
st.error("Prompt cannot be empty. Please enter a valid text prompt.")
|
29 |
+
else:
|
30 |
+
st.write(f"### Prompt: {prompt}")
|
31 |
+
with st.spinner("Generating image... Please wait."):
|
32 |
+
try:
|
33 |
+
# Generate the image
|
34 |
image = pipe(prompt).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Display the image
|
37 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
38 |
+
|
39 |
+
# Provide download option
|
40 |
+
img_path = "generated_image.png"
|
41 |
+
image.save(img_path)
|
42 |
+
with open(img_path, "rb") as img_file:
|
43 |
+
st.download_button(
|
44 |
+
label="Download Image",
|
45 |
+
data=img_file,
|
46 |
+
file_name="generated_image.png",
|
47 |
+
mime="image/png"
|
48 |
+
)
|
49 |
+
except Exception as e:
|
50 |
+
st.error(f"An error occurred: {e}")
|