Spaces:
Running
Running
import torch | |
import streamlit as st | |
from diffusers import DiffusionPipeline | |
# Cache resource to load the pipeline | |
def load_pipeline(): | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", | |
torch_dtype=torch.float16 if device == "cuda" else torch.float32, | |
use_safetensors=True, | |
variant="fp16" if device == "cuda" else None) | |
return pipe | |
# Image generation function | |
def image_generation(pipe, prompt, negative_prompt): | |
try: | |
image = pipe( | |
prompt=prompt, | |
negative_prompt="blurred, ugly, watermark, low resolution" + negative_prompt, | |
num_inference_steps=20, | |
guidance_scale=9.0 | |
).images[0] | |
return image | |
except Exception as e: | |
st.error(f"Error generating image: {str(e)}") | |
return None | |
# Define the table as a list of dictionaries with more artistic styles | |
table = [ | |
{ | |
"name": "sai-neonpunk", | |
"prompt": "neonpunk style . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional", | |
"negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured" | |
}, | |
{ | |
"name": "futuristic-retro cyberpunk", | |
"prompt": "retro cyberpunk. 80's inspired, synthwave, neon, vibrant, detailed, retro futurism", | |
"negative_prompt": "modern, desaturated, black and white, realism, low contrast" | |
}, | |
{ | |
"name": "Dark Fantasy", | |
"prompt": "Dark Fantasy Art, dark, moody, dark fantasy style", | |
"negative_prompt": "ugly, deformed, noisy, blurry, low contrast, bright, sunny" | |
}, | |
{ | |
"name": "Double Exposure", | |
"prompt": "Double Exposure Style, double image ghost effect, image combination, double exposure style", | |
"negative_prompt": "ugly, deformed, noisy, blurry, low contrast" | |
}, | |
{ | |
"name": "Vintage Retro", | |
"prompt": "vintage retro style, old-school, warm colors, nostalgic, retro textures, faded colors, cozy atmosphere, worn, classic film", | |
"negative_prompt": "modern, bright, high definition, sharp" | |
}, | |
{ | |
"name": "Sci-Fi Futuristic", | |
"prompt": "Sci-Fi futuristic, cybernetic, sleek metallic designs, neon, artificial intelligence, high-tech, future cities, robots, alien landscapes", | |
"negative_prompt": "low-tech, blurry, old-fashioned, dull, dark" | |
}, | |
{ | |
"name": "Surrealism", | |
"prompt": "Surrealism, dreamlike, abstract, bizarre, imaginative, fantastical, magical realism, melting objects, floating landscapes", | |
"negative_prompt": "realistic, plain, dull, conventional" | |
}, | |
{ | |
"name": "Pop Art", | |
"prompt": "Pop Art, bold colors, comic book style, strong lines, graphic, playful, contemporary, inspired by Andy Warhol and Roy Lichtenstein", | |
"negative_prompt": "minimalistic, muted colors, traditional, abstract" | |
}, | |
{ | |
"name": "Gothic Horror", | |
"prompt": "Gothic horror, dark, eerie, vintage horror, Victorian architecture, spooky ambiance, haunted, gothic castles, fog, grim landscapes", | |
"negative_prompt": "light, cheerful, happy, bright" | |
}, | |
{ | |
"name": "Minimalism", | |
"prompt": "Minimalism, clean lines, simple forms, neutral tones, white space, simple color palette, elegant, modern design", | |
"negative_prompt": "cluttered, bright, complex, busy" | |
}, | |
{ | |
"name": "Steampunk", | |
"prompt": "Steampunk, Victorian industrial, brass gears, steam-powered machinery, fantasy airships, retro-futuristic, adventure, mechanical details", | |
"negative_prompt": "futuristic, modern, minimalistic, digital" | |
} | |
] | |
# Convert to dictionary for easy lookup | |
styles_dict = {entry["name"]: entry for entry in table} | |
# Streamlit UI | |
st.title("Image Generation") | |
# Display credit after the title in smaller font | |
st.markdown("<h3 style='font-size: 18px;'>Made by Taizun</h3>", unsafe_allow_html=True) | |
# User input for the custom prompt | |
prompt = st.text_input("Enter your Prompt") | |
# Load the pre-trained model | |
pipeline = load_pipeline() | |
# Dropdown for selecting a style | |
style_name = st.selectbox("Select a Style", options=list(styles_dict.keys())) | |
# Display the selected style's prompt and negative prompt | |
if style_name: | |
selected_entry = styles_dict[style_name] | |
selected_style_prompt = selected_entry["prompt"] | |
selected_style_negative_prompt = selected_entry["negative_prompt"] | |
# Button to trigger image generation | |
if st.button("Generate Awesome Image"): | |
with st.spinner("Generating your awesome image... This may take a while. Feel free to explore other spaces in the meantime"): | |
image = image_generation(pipeline, prompt + selected_style_prompt, selected_style_negative_prompt) | |
if image: | |
st.image(image) | |