Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,43 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import streamlit as st
|
| 4 |
-
from diffusers import DiffusionPipeline
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
import
|
| 10 |
except ModuleNotFoundError:
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Streamlit UI
|
| 14 |
st.title("Lightweight Text-to-Image Generator (CPU Friendly)")
|
| 15 |
st.write("Enter a description, and an AI model will generate an image!")
|
| 16 |
|
| 17 |
-
# Load
|
| 18 |
@st.cache_resource()
|
| 19 |
def load_model():
|
| 20 |
model_id = "CompVis/txt2img-f8-large" # Lightweight model for CPU
|
| 21 |
-
pipe = DiffusionPipeline.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import subprocess
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Function to install packages
|
| 5 |
+
def install(package):
|
| 6 |
+
subprocess.check_call([os.sys.executable, "-m", "pip", "install", package])
|
| 7 |
+
|
| 8 |
+
# Install torch if not already installed
|
| 9 |
try:
|
| 10 |
+
import torch
|
| 11 |
except ModuleNotFoundError:
|
| 12 |
+
install("torch")
|
| 13 |
+
import torch
|
| 14 |
+
|
| 15 |
+
import streamlit as st
|
| 16 |
+
from diffusers import DiffusionPipeline
|
| 17 |
+
from PIL import Image
|
| 18 |
|
| 19 |
# Streamlit UI
|
| 20 |
st.title("Lightweight Text-to-Image Generator (CPU Friendly)")
|
| 21 |
st.write("Enter a description, and an AI model will generate an image!")
|
| 22 |
|
| 23 |
+
# Load the model
|
| 24 |
@st.cache_resource()
|
| 25 |
def load_model():
|
| 26 |
model_id = "CompVis/txt2img-f8-large" # Lightweight model for CPU
|
| 27 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 28 |
+
pipe.to("cpu") # Ensure the model runs on CPU
|
| 29 |
+
return pipe
|
| 30 |
+
|
| 31 |
+
pipe = load_model()
|
| 32 |
+
|
| 33 |
+
# User input
|
| 34 |
+
text_input = st.text_input("Enter your description:")
|
| 35 |
+
|
| 36 |
+
# Generate and display image
|
| 37 |
+
if text_input:
|
| 38 |
+
with st.spinner("Generating image... Please wait ⏳"):
|
| 39 |
+
try:
|
| 40 |
+
image = pipe(text_input).images[0] # Generate image
|
| 41 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
st.error(f"Error generating image: {e}")
|