Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Title
|
| 7 |
+
st.title("Text-to-Image Generator")
|
| 8 |
+
|
| 9 |
+
# Sidebar Input
|
| 10 |
+
st.sidebar.header("Input Prompt")
|
| 11 |
+
text_prompt = st.sidebar.text_input("Enter a description:", "A fantasy landscape with mountains and rivers")
|
| 12 |
+
|
| 13 |
+
# Generate Button
|
| 14 |
+
if st.sidebar.button("Generate Image"):
|
| 15 |
+
with st.spinner("Generating image..."):
|
| 16 |
+
# Load the model (make sure you have access to GPUs for faster generation)
|
| 17 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
| 18 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
|
| 20 |
+
# Generate image
|
| 21 |
+
image = pipe(text_prompt).images[0]
|
| 22 |
+
|
| 23 |
+
# Display the generated image
|
| 24 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
| 25 |
+
st.success("Image generated successfully!")
|
| 26 |
+
|
| 27 |
+
# Download Option
|
| 28 |
+
if 'image' in locals():
|
| 29 |
+
with st.sidebar:
|
| 30 |
+
st.download_button(
|
| 31 |
+
"Download Image",
|
| 32 |
+
data=image.tobytes(),
|
| 33 |
+
file_name="generated_image.png",
|
| 34 |
+
mime="image/png",
|
| 35 |
+
)
|