Spaces:
Sleeping
Sleeping
Update app.py
#4
by
Muthuraja18
- opened
app.py
CHANGED
|
@@ -1,31 +1,35 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import streamlit as st
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
-
# Force CPU-only execution
|
| 12 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 13 |
-
"runwayml/stable-diffusion-v1-5"
|
|
|
|
| 14 |
)
|
| 15 |
-
pipe.to("cpu")
|
| 16 |
return pipe
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
prompt = st.text_input("Enter your prompt:",
|
| 21 |
-
"A
|
| 22 |
|
| 23 |
-
guidance = st.slider("
|
| 24 |
|
| 25 |
if st.button("Generate"):
|
| 26 |
-
with st.spinner("Generating on CPU... please wait
|
| 27 |
pipe = load_model()
|
| 28 |
image = pipe(prompt, guidance_scale=guidance).images[0]
|
|
|
|
| 29 |
st.image(image, caption="Generated Image", use_column_width=True)
|
| 30 |
|
| 31 |
buf = io.BytesIO()
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Force CPU usage on Spaces
|
| 9 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 10 |
|
| 11 |
@st.cache_resource
|
| 12 |
def load_model():
|
|
|
|
| 13 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 14 |
+
"runwayml/stable-diffusion-v1-5",
|
| 15 |
+
torch_dtype=torch.float32,
|
| 16 |
)
|
| 17 |
+
pipe.to("cpu") # Force CPU usage
|
| 18 |
return pipe
|
| 19 |
|
| 20 |
+
# Streamlit UI
|
| 21 |
+
st.title("π¨ AI Image Generator (CPU - Hugging Face Spaces)")
|
| 22 |
|
| 23 |
prompt = st.text_input("Enter your prompt:",
|
| 24 |
+
"A multi-dimensional futuristic city with glowing lights, fractals, 8K")
|
| 25 |
|
| 26 |
+
guidance = st.slider("Creativity (Guidance Scale)", 1.0, 20.0, 7.5)
|
| 27 |
|
| 28 |
if st.button("Generate"):
|
| 29 |
+
with st.spinner("Generating image on CPU... please wait β³"):
|
| 30 |
pipe = load_model()
|
| 31 |
image = pipe(prompt, guidance_scale=guidance).images[0]
|
| 32 |
+
|
| 33 |
st.image(image, caption="Generated Image", use_column_width=True)
|
| 34 |
|
| 35 |
buf = io.BytesIO()
|