Spaces:
Runtime error
Runtime error
Dua Rajper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,31 @@
|
|
|
|
|
|
|
|
| 1 |
from diffusers import StableDiffusionPipeline
|
| 2 |
import torch
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import streamlit as st
|
| 5 |
-
|
| 6 |
-
# Load the Stable Diffusion pipeline
|
| 7 |
-
@st.cache(allow_output_mutation=True)
|
| 8 |
-
def load_pipeline():
|
| 9 |
-
pipeline = StableDiffusionPipeline.from_pretrained(
|
| 10 |
-
"CompVis/stable-diffusion-v1-4",
|
| 11 |
-
torch_dtype=torch.float32 # Use float32 for CPU support
|
| 12 |
-
)
|
| 13 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
-
pipeline.to(device)
|
| 15 |
-
return pipeline
|
| 16 |
-
|
| 17 |
-
def main():
|
| 18 |
-
st.title("Stable Diffusion Image Generator")
|
| 19 |
-
st.write("Generate images from text prompts using Stable Diffusion")
|
| 20 |
-
|
| 21 |
-
# Initialize the pipeline
|
| 22 |
-
pipeline = load_pipeline()
|
| 23 |
-
|
| 24 |
-
# Text input for the prompt
|
| 25 |
-
prompt = st.text_input("Enter your text prompt", "")
|
| 26 |
-
|
| 27 |
-
# Generate button
|
| 28 |
-
if st.button("Generate"):
|
| 29 |
-
if not prompt:
|
| 30 |
-
st.warning("Please enter a prompt first.")
|
| 31 |
-
return # ✅ Make sure this return is correctly indented
|
| 32 |
-
|
| 33 |
-
st.write("Generating your image...")
|
| 34 |
-
with torch.no_grad():
|
| 35 |
-
result = pipeline(prompt)
|
| 36 |
-
image = result.images[0] # Extract the generated image
|
| 37 |
-
|
| 38 |
-
st.write("Generated Image:")
|
| 39 |
-
st.image(image, use_column_width=True)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load the pre-trained Stable Diffusion model
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_model():
|
| 9 |
+
model = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v-1-4-original", torch_dtype=torch.float16)
|
| 10 |
+
model.to("cuda")
|
| 11 |
+
return model
|
| 12 |
+
|
| 13 |
+
# Initialize model
|
| 14 |
+
model = load_model()
|
| 15 |
+
|
| 16 |
+
# Streamlit Interface
|
| 17 |
+
st.title("Text to Image Generator using Stable Diffusion")
|
| 18 |
+
st.write("Enter a description below, and the model will generate an image based on it.")
|
| 19 |
+
|
| 20 |
+
# Text input field for user to enter prompt
|
| 21 |
+
user_input = st.text_area("Enter the text prompt", "A beautiful landscape with mountains and lakes.")
|
| 22 |
+
|
| 23 |
+
if st.button("Generate Image"):
|
| 24 |
+
if user_input:
|
| 25 |
+
with st.spinner("Generating image..."):
|
| 26 |
+
# Generate image based on user input
|
| 27 |
+
generated_image = model(user_input).images[0]
|
| 28 |
+
st.image(generated_image)
|
| 29 |
+
else:
|
| 30 |
+
st.error("Please enter a text prompt!")
|
| 31 |
|