Spaces:
Build error
Build error
Download app.py from She-gbt/PODoptimizer: direct link, hf CLI and curl.
- Browser
- Download file 5.07 kB
-
https://huggingface.co/spaces/She-gbt/PODoptimizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/She-gbt/PODoptimizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/She-gbt/PODoptimizer/resolve/main/app.py
5.07 kB
| 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() |