Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 8 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 9 |
+
pipe = pipe.to("cuda")
|
| 10 |
+
|
| 11 |
+
# Streamlit UI
|
| 12 |
+
st.title("Stable Diffusion XL Base 1.0 Demo")
|
| 13 |
+
st.markdown("## Generate Images with Text Prompts")
|
| 14 |
+
|
| 15 |
+
# Informational Sidebar
|
| 16 |
+
with st.sidebar:
|
| 17 |
+
st.header("Instructions")
|
| 18 |
+
st.write("""
|
| 19 |
+
1. Enter a descriptive text prompt.
|
| 20 |
+
2. Select the type of image you want to generate.
|
| 21 |
+
3. Click the "Generate Image" button.
|
| 22 |
+
4. Wait for the image to be generated and displayed.
|
| 23 |
+
""")
|
| 24 |
+
|
| 25 |
+
# Main Interface
|
| 26 |
+
prompt = st.text_input("Enter your prompt here:")
|
| 27 |
+
image_types = ["Artistic Illustration", "Product Design", "Landscape", "Character", "Photorealistic"]
|
| 28 |
+
selected_type = st.selectbox("Select the type of image:", image_types)
|
| 29 |
+
|
| 30 |
+
# Placeholder for generated image
|
| 31 |
+
image_placeholder = st.empty()
|
| 32 |
+
|
| 33 |
+
if st.button("Generate Image"):
|
| 34 |
+
if prompt:
|
| 35 |
+
with st.spinner("Generating image..."):
|
| 36 |
+
try:
|
| 37 |
+
image = pipe(prompt).images[0]
|
| 38 |
+
image_placeholder.image(image, caption=f"{selected_type} - {prompt}")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
st.error(f"An error occurred: {e}")
|
| 41 |
+
else:
|
| 42 |
+
st.warning("Please enter a prompt to generate an image.")
|
| 43 |
+
|
| 44 |
+
# Footer
|
| 45 |
+
st.markdown("""
|
| 46 |
+
---
|
| 47 |
+
**Stable Diffusion XL Base 1.0** by [Stability AI](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0)
|
| 48 |
+
""")
|