Spaces:
Sleeping
Sleeping
import gradio as gr | |
from diffusers import AutoPipelineForText2Image | |
import torch | |
import random | |
if torch.backends.mps.is_available(): | |
device = "mps" | |
elif torch.cuda.is_available(): | |
device = "cuda" | |
else: | |
device = "cpu" | |
torch_dtype = torch.float32 if device == "cpu" else torch.float16 | |
model_repo_id = "stabilityai/sdxl-turbo" | |
pipe = AutoPipelineForText2Image.from_pretrained(model_repo_id, torch_dtype=torch_dtype, variant="fp16") | |
pipe = pipe.to(device) | |
def infer(prompt): | |
num_inference_steps = 2 | |
guidance_scale = 0.0 | |
width = 512 | |
height = 512 | |
image = pipe( | |
prompt=prompt, | |
guidance_scale=guidance_scale, | |
num_inference_steps=num_inference_steps, | |
width=width, | |
height=height, | |
).images[0] | |
return image | |
# Lists for random prompt generation | |
subjects = [ | |
"a cute red panda", "a robot", "a steampunk owl", "a magical crystal", | |
"a floating island", "a cybernetic dolphin", "a time-traveling historian", | |
"a ghost pianist", "a rainbow dragon", "a space explorer", "a majestic tiger", | |
"a giant mushroom", "a neon cityscape", "a cosmic jellyfish", "a dreamy unicorn", | |
"a celestial mermaid", "a mechanical spider", "a mystical forest", "a futuristic city", | |
"a whimsical fairy", "a mythical phoenix", "a cosmic nebula", "a magical castle", | |
"a cyberpunk samurai", "a time-traveling astronaut", "a crystal cavern", | |
] | |
contexts = [ | |
"in an ancient library", "during a neon sunset", "floating in space", | |
"deep underwater", "in a floating market", "on top of a giant mushroom", | |
"inside a clockwork machine", "in a crystal cave", "during a meteor shower", | |
"in a bamboo forest", "in a futuristic cityscape", "on a magical island", | |
"in a cyberpunk alleyway", "in a steampunk workshop", | |
"in a post-apocalyptic wasteland", "in a dreamy cloud kingdom", | |
] | |
styles = [ | |
"cyberpunk style", "watercolor painting", "digital art", "oil painting", | |
"studio ghibli style", "synthwave aesthetics", "detailed pencil sketch", | |
"photorealistic", "vaporwave style", "low poly art", "lo-fi aesthetics", | |
"grainy film photography", "comic book style", "minimalist design", | |
] | |
image_qualities = [ | |
"highly detailed", "8K resolution", "dramatic lighting", | |
"professional photography", "cinematic composition", "hyperrealistic", | |
"atmospheric", "stunning", "award-winning", "masterpiece", | |
"low quality", "grainy", "blurry", "abstract", "surreal", | |
] | |
def generate_not_hot_dog_prompt(): | |
subject = random.choice(subjects) | |
context = random.choice(contexts) | |
style = random.choice(styles) | |
quality = random.choice(image_qualities) | |
return f"{subject} {context}, {style}, {quality}" | |
hot_dog_prompt = "A cinematic shot of a gourmet hotdog in extreme close-up, sitting in a rustic brioche bun, garnished with perfectly drizzled mustard and ketchup. The hotdog glistens under dramatic studio lighting with a slight overhead angle. Shallow depth of field with professional food photography styling. The background is dark and moody with subtle bokeh effects. Shot with a high-end DSLR camera, f/2.8 aperture, warm color grading reminiscent of a Wes Anderson film. Photorealistic quality, 8K resolution." | |
def generate_hot_dog(): | |
return infer(hot_dog_prompt) | |
def generate_not_hot_dog(): | |
return infer(generate_not_hot_dog_prompt()) | |
with gr.Blocks(theme="compact") as demo: | |
gr.Markdown("# What Do You Want To Create?") | |
with gr.Row(): | |
hot_dog_btn = gr.Button("π Hot Dog", variant="primary") | |
not_hot_dog_btn = gr.Button("β Not Hot Dog", variant="primary") | |
output_image = gr.Image(label="Generated Image", type="numpy") | |
hot_dog_btn.click( | |
fn=generate_hot_dog, | |
inputs=[], | |
outputs=output_image | |
) | |
not_hot_dog_btn.click( | |
fn=generate_not_hot_dog, | |
inputs=[], | |
outputs=output_image | |
) | |
if __name__ == "__main__": | |
demo.launch() | |