Spaces:
Running
Running
mahazainab
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load the stable diffusion model
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
10 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
11 |
+
pipe = pipe.to("cuda")
|
12 |
+
return pipe
|
13 |
+
|
14 |
+
pipe = load_model()
|
15 |
+
|
16 |
+
# Streamlit app title
|
17 |
+
st.title("Stable Diffusion Image Generator")
|
18 |
+
|
19 |
+
# User input for text prompt
|
20 |
+
prompt = st.text_input("Enter your image prompt:", "")
|
21 |
+
|
22 |
+
# Button to generate image
|
23 |
+
if st.button("Generate Image"):
|
24 |
+
if prompt:
|
25 |
+
# Generate the image
|
26 |
+
with st.spinner("Generating..."):
|
27 |
+
image = pipe(prompt).images[0]
|
28 |
+
st.image(image, caption=prompt, use_column_width=True)
|
29 |
+
|
30 |
+
# Save the image
|
31 |
+
image.save(f"{prompt[:50].replace(' ', '_')}.png")
|
32 |
+
st.success("Image generated successfully!")
|
33 |
+
else:
|
34 |
+
st.warning("Please enter a prompt to generate an image.")
|