Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,51 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import CLIPProcessor, CLIPTextModel # Using CLIP as an example of a lighter model for image generation
|
| 4 |
|
| 5 |
@st.cache_resource
|
| 6 |
-
def
|
| 7 |
-
"""Load a lightweight image generation model."""
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
def generate_image(
|
| 15 |
-
"""Generate an image using the lightweight model."""
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
st.error(f"Error generating image: {str(e)}")
|
| 24 |
return None
|
| 25 |
|
| 26 |
-
# Streamlit app setup
|
| 27 |
st.title("Image Generator")
|
| 28 |
-
st.markdown(
|
| 29 |
-
st.markdown('<small>Effortlessly create stunning images with our lightweight generator.</small>', unsafe_allow_html=True)
|
| 30 |
|
| 31 |
prompt = st.text_input("Enter your prompt")
|
| 32 |
-
|
| 33 |
|
| 34 |
-
# Style table (Example styles)
|
| 35 |
styles_dict = {
|
| 36 |
-
"Neon Vibes": {"prompt": "neon lights", "negative_prompt": "
|
| 37 |
-
"Retro Sci-Fi": {"prompt": "retro futuristic
|
| 38 |
-
"Mystic Forest": {"prompt": "dark forest with
|
| 39 |
-
"Abstract Art": {"prompt": "abstract shapes
|
| 40 |
}
|
| 41 |
|
| 42 |
-
# Dropdown for selecting a style
|
| 43 |
style_name = st.selectbox("Select a Style", options=list(styles_dict.keys()))
|
| 44 |
|
| 45 |
-
# Display the selected style's prompt and negative prompt
|
| 46 |
if style_name:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
if st.button("Generate Image"):
|
| 52 |
with st.spinner("Generating your image..."):
|
| 53 |
-
|
| 54 |
-
if
|
| 55 |
-
st.image(
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
@st.cache_resource
|
| 6 |
+
def load_lightweight_pipeline():
|
|
|
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
pipeline = StableDiffusionPipeline.from_pretrained(
|
| 9 |
+
"CompVis/stable-diffusion-v1-4",
|
| 10 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 11 |
+
).to(device)
|
| 12 |
+
return pipeline
|
| 13 |
|
| 14 |
+
def generate_image(pipeline, prompt, negative_prompt):
|
|
|
|
| 15 |
try:
|
| 16 |
+
image = pipeline(
|
| 17 |
+
prompt=prompt,
|
| 18 |
+
negative_prompt=negative_prompt,
|
| 19 |
+
num_inference_steps=25,
|
| 20 |
+
guidance_scale=7.5
|
| 21 |
+
).images[0]
|
| 22 |
+
return image
|
| 23 |
except Exception as e:
|
| 24 |
st.error(f"Error generating image: {str(e)}")
|
| 25 |
return None
|
| 26 |
|
|
|
|
| 27 |
st.title("Image Generator")
|
| 28 |
+
st.markdown('<small>By Taizun</small>', unsafe_allow_html=True)
|
|
|
|
| 29 |
|
| 30 |
prompt = st.text_input("Enter your prompt")
|
| 31 |
+
pipeline = load_lightweight_pipeline()
|
| 32 |
|
|
|
|
| 33 |
styles_dict = {
|
| 34 |
+
"Neon Vibes": {"prompt": "neon lights in a futuristic city", "negative_prompt": "blurred, dull"},
|
| 35 |
+
"Retro Sci-Fi": {"prompt": "retro futuristic landscape", "negative_prompt": "realistic, modern"},
|
| 36 |
+
"Mystic Forest": {"prompt": "dark mystical forest with glowing lights", "negative_prompt": "sunny, bright"},
|
| 37 |
+
"Abstract Art": {"prompt": "abstract colorful shapes", "negative_prompt": "realistic, detailed"}
|
| 38 |
}
|
| 39 |
|
|
|
|
| 40 |
style_name = st.selectbox("Select a Style", options=list(styles_dict.keys()))
|
| 41 |
|
|
|
|
| 42 |
if style_name:
|
| 43 |
+
selected_style = styles_dict[style_name]
|
| 44 |
+
style_prompt = selected_style["prompt"]
|
| 45 |
+
negative_prompt = selected_style["negative_prompt"]
|
| 46 |
|
| 47 |
if st.button("Generate Image"):
|
| 48 |
with st.spinner("Generating your image..."):
|
| 49 |
+
image = generate_image(pipeline, prompt + " " + style_prompt, negative_prompt)
|
| 50 |
+
if image:
|
| 51 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|