Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Function to load the model
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
| 10 |
+
pipe.to("cpu") # Ensure the model runs on CPU
|
| 11 |
+
return pipe
|
| 12 |
+
|
| 13 |
+
# Load the model
|
| 14 |
+
pipe = load_model()
|
| 15 |
+
|
| 16 |
+
# Streamlit UI
|
| 17 |
+
st.title("Text-to-Image Generator")
|
| 18 |
+
st.write("Enter a description, and the AI model will generate an image!")
|
| 19 |
+
|
| 20 |
+
# User input
|
| 21 |
+
text_input = st.text_input("Enter your description:")
|
| 22 |
+
|
| 23 |
+
# Generate and display image
|
| 24 |
+
if text_input:
|
| 25 |
+
with st.spinner("Generating image... Please wait ⏳"):
|
| 26 |
+
try:
|
| 27 |
+
image = pipe(text_input).images[0] # Generate image
|
| 28 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Error generating image: {e}")
|