Spaces:
Build error
Build error
File size: 5,069 Bytes
7438126 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import gradio as gr
from diffusers import StableDiffusionImg2ImgPipeline
import torch
from PIL import Image
import io
import random
# ---- STYLE PROMPTS ----
STYLE_PROMPTS = {
"no style change": "",
"tattoo": "blackwork tattoo style, bold clean lines, vector look, high contrast",
"street art": "street art mural style, graffiti, spray paint, bold outlines, urban, vector look",
"pop art": "pop art style, halftone dots, bold primary colors, clean outlines",
"minimalism": "minimalist vector illustration, simple flat colors, lots of negative space",
"geometric": "geometric vector art, sharp shapes, clean lines, abstract patterns",
"y2k": "Y2K digital art style, chrome, gradients, early 2000s web aesthetics, shiny, vector look",
"abstract": "abstract vector art, bold shapes, experimental, high contrast",
"trippy": "psychedelic trippy art, warped shapes, vivid colors, surreal, vector look",
"retro": "retro vintage poster style, muted colors, worn texture, clean outlines",
"grunge": "grunge poster art, distressed textures, rough edges, dark tones"
}
TARGET_MAX_SIDE = 6000 # pixels (good for shirts, posters, etc.)
DPI = 350
# ---- LOAD PIPELINE ----
MODEL_ID = "runwayml/stable-diffusion-v1-5"
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16 if device == "cuda" else torch.float32
).to(device)
def resize_for_model(image: Image.Image, max_side: int = 1024) -> Image.Image:
w, h = image.size
scale = max_side / max(w, h)
if scale < 1:
new_size = (int(w * scale), int(h * scale))
return image.resize(new_size, Image.LANCZOS)
return image
def upscale_to_target(image: Image.Image, target_max_side: int = TARGET_MAX_SIDE) -> Image.Image:
w, h = image.size
max_side = max(w, h)
if max_side >= target_max_side:
return image
scale = target_max_side / max_side
new_size = (int(w * scale), int(h * scale))
return image.resize(new_size, Image.LANCZOS)
def generate_pod_art(input_image, style_name, num_variants=3, strength=0.7, guidance_scale=7.5):
if input_image is None:
return []
base_image = input_image.convert("RGB")
# Prepare for model
model_input = resize_for_model(base_image, max_side=1024)
style_prompt = STYLE_PROMPTS.get(style_name, "")
if style_name == "no style change":
# Just try to keep content; no heavy style
prompt = "high resolution, clean, vector-style illustration, print-on-demand ready"
strength_val = 0.3 # stay close to original
else:
prompt = f"{style_prompt}, high resolution, vector-style illustration, print-on-demand ready"
strength_val = strength
outputs = []
for _ in range(num_variants):
seed = random.randint(0, 2**32 - 1)
generator = torch.Generator(device=device).manual_seed(seed)
if device == "cuda":
with torch.autocast(device):
result = pipe(
prompt=prompt,
image=model_input,
strength=strength_val,
guidance_scale=guidance_scale,
num_inference_steps=30,
generator=generator
)
else:
with torch.no_grad():
result = pipe(
prompt=prompt,
image=model_input,
strength=strength_val,
guidance_scale=guidance_scale,
num_inference_steps=30,
generator=generator
)
out_image = result.images[0]
# Upscale for POD
out_image = upscale_to_target(out_image, target_max_side=TARGET_MAX_SIDE)
# Set DPI metadata when saving to bytes
buf = io.BytesIO()
out_image.save(buf, format="PNG", dpi=(DPI, DPI))
buf.seek(0)
pil_out = Image.open(buf)
outputs.append(pil_out)
return outputs
def process(input_image, style):
images = generate_pod_art(input_image, style)
return images
with gr.Blocks() as demo:
gr.Markdown("# POD-Ready Art Generator\nUpload an image, pick a style, get 3 high-res 350 DPI PNGs for print-on-demand.")
with gr.Row():
with gr.Column():
input_img = gr.Image(label="Input Image", type="pil")
style_dropdown = gr.Dropdown(
label="Style",
choices=list(STYLE_PROMPTS.keys()),
value="no style change"
)
generate_btn = gr.Button("Generate POD Art")
with gr.Column():
gallery = gr.Gallery(
label="Generated Images",
show_label=True,
columns=3,
height=400,
preview=True
)
generate_btn.click(
fn=process,
inputs=[input_img, style_dropdown],
outputs=[gallery]
)
if __name__ == "__main__":
demo.launch() |