CoolFace
Apppublic

Shopify/background-replacement

sourceHugging Faceupdated 2y agoView on Hugging Face
457likes
app.py238 linesDownload Raw Back to root
1import os2 3import gradio as gr4 5from background_replacer import replace_background6 7developer_mode = os.getenv('DEV_MODE', False)8 9DEFAULT_POSITIVE_PROMPT = "on the pavement, poolside, idyllic infinity pool, Hawaiian hilltops, commercial product photography"10DEFAULT_NEGATIVE_PROMPT = ""11 12EXAMPLES = [13    [14        "examples/black-sneakers-with-white-sole.jpg",15        "on the grass in Central Park, gorgeous summer day with Bethesda fountain in the background, commercial footwear product photography",16        "people, litter, trash, crowds, messy",17    ],18    [19        "examples/DIY-beard-balm.jpg",20        "on a mossy rock, white wood anemone blossoms, Loch Ken, Scotland",21        "purple, wrong proportions",22    ],23    [24        "examples/dj-making-music-on-mixer.jpg",25        "on the turntables with a packed dance floor, epic midnight edm party in Miami Beach, colorful nightlife photography",26        "disfigured, dismembered, mangled, marred",27    ],28    [29        "examples/jean-shorts-woman.jpg",30        "on the beach in Malibu, a five-star beachfront hotel in the background, stark late afternoon light near the dunes, lifestyle photography",31        "blurry background, ripples, soft focus, bokeh",32    ],33]34 35INTRO = """36# Shopify Image Background Replacement37 38[![Duplicate this Space](https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-md.svg)](https://huggingface.co/spaces/Shopify/background-replacement?duplicate=true)39Minimum recommended hardware: Nvidia A10G large (46 GB RAM, 24 GB VRAM)40 41## Status42🏝️ Since the publication of this prototype, we've devoted our efforts to developing an enhanced version within Shopify's admin interface, which is now accessible to all Shopify merchants across all subscription plans. This original space is no longer maintained and runs on a CPU-only free tier. Please duplicate this space and utilize your own GPUs.43 44<hr>45 46Building an online store requires lots of high quality product and marketing images. This is an early demo of a background replacement tool built with Stable Diffusion XL that makes it easy to use your existing product images to make something new. Please be patient during peak demand. πŸ˜…47 48To use it, upload your product photo (.jpg or .png), then describe the background you’d like to see in place of the original. For best results follow the general pattern in the examples below:491. ❌ _Do not_ describe your product in the prompt (ex: black sneakers)502. βœ… Do describe the "grounding" for your product (ex: placed on a table)513. βœ… Do describe the scene you want (ex: in a greek cottage)524. βœ… Do describe a style of image (ex: side view commercial product photography)535. πŸ€” Optionally, describe what you want to avoid πŸ™… in the negative prompt field54"""55 56MORE_INFO = """57### More information58- You can check our [FAQs here](https://huggingface.co/spaces/Shopify/background-replacement/blob/main/README.md#faqs)!59- We are also gathering resources from the community and sharing ideas [here](https://huggingface.co/spaces/Shopify/background-replacement/discussions).60- Shopify is on a mission to redefine commerce with AI. If you’re an AI or ML engineer looking to build the future of commerce, [join us](https://www.shopify.com/careers)!61"""62 63 64def generate(65    image,66    positive_prompt,67    negative_prompt,68    seed,69    depth_map_feather_threshold,70    depth_map_dilation_iterations,71    depth_map_blur_radius,72    progress=gr.Progress(track_tqdm=True)73):74    if image is None:75        return [None, None, None, None]76 77    options = {78        'seed': seed,79        'depth_map_feather_threshold': depth_map_feather_threshold,80        'depth_map_dilation_iterations': depth_map_dilation_iterations,81        'depth_map_blur_radius': depth_map_blur_radius,82    }83 84    return replace_background(image, positive_prompt, negative_prompt, options)85 86 87custom_css = """88    #image-upload {89        flex-grow: 1;90    }91    #params .tabs {92        display: flex;93        flex-direction: column;94        flex-grow: 1;95    }96    #params .tabitem[style="display: block;"] {97        flex-grow: 1;98        display: flex !important;99    }100    #params .gap {101        flex-grow: 1;102    }103    #params .form {104        flex-grow: 1 !important;105    }106    #params .form > :last-child{107        flex-grow: 1;108    }109    .md ol, .md ul {110        margin-left: 1rem;111    }112    .md img {113        margin-bottom: 1rem;114    }115"""116 117with gr.Blocks(css=custom_css) as iface:118    gr.Markdown(INTRO)119 120    with gr.Row():121        with gr.Column():122            image_upload = gr.Image(123                label="Product image",124                type="pil",125                elem_id="image-upload"126            )127            caption = gr.Label(128                label="Caption",129                visible=developer_mode130            )131        with gr.Column(elem_id="params"):132            with gr.Tab('Prompts'):133                positive_prompt = gr.Textbox(134                    label="Positive Prompt: describe what you'd like to see",135                    lines=3,136                    value=DEFAULT_POSITIVE_PROMPT137                )138                negative_prompt = gr.Textbox(139                    label="Negative Prompt: describe what you want to avoid",140                    lines=3,141                    value=DEFAULT_NEGATIVE_PROMPT142                )143            if developer_mode:144                with gr.Tab('Options'):145                    seed = gr.Number(146                        label="Seed",147                        precision=0,148                        value=0,149                        elem_id="seed",150                        visible=developer_mode151                    )152                    depth_map_feather_threshold = gr.Slider(153                        label="Depth map feather threshold",154                        value=128,155                        minimum=0,156                        maximum=255,157                        visible=developer_mode158                    )159                    depth_map_dilation_iterations = gr.Number(160                        label="Depth map dilation iterations",161                        precision=0,162                        value=10,163                        minimum=0,164                        visible=developer_mode165                    )166                    depth_map_blur_radius = gr.Number(167                        label="Depth map blur radius",168                        precision=0,169                        value=10,170                        minimum=0,171                        visible=developer_mode172                    )173            else:174                seed = gr.Number(value=-1, visible=False)175                depth_map_feather_threshold = gr.Slider(176                    value=128, visible=False)177                depth_map_dilation_iterations = gr.Number(178                    precision=0, value=10, visible=False)179                depth_map_blur_radius = gr.Number(180                    precision=0, value=10, visible=False)181 182    # Enable this button!183    gen_button = gr.Button(184        value="Generate!", variant="primary", interactive=False)185 186    with gr.Tab('Results'):187        results = gr.Gallery(188            show_label=False,189            object_fit="contain",190            columns=4191        )192 193    if developer_mode:194        with gr.Tab('Generated'):195            generated = gr.Gallery(196                show_label=False,197                object_fit="contain",198                columns=4199            )200 201        with gr.Tab('Pre-processing'):202            pre_processing = gr.Gallery(203                show_label=False,204                object_fit="contain",205                columns=4206            )207    else:208        generated = gr.Gallery(visible=False)209        pre_processing = gr.Gallery(visible=False)210 211    gr.Examples(212        examples=EXAMPLES,213        inputs=[image_upload, positive_prompt, negative_prompt],214    )215 216    gr.Markdown(MORE_INFO)217 218    gen_button.click(219        fn=generate,220        inputs=[221            image_upload,222            positive_prompt,223            negative_prompt,224            seed,225            depth_map_feather_threshold,226            depth_map_dilation_iterations,227            depth_map_blur_radius228        ],229        outputs=[230            results,231            generated,232            pre_processing,233            caption234        ],235    )236 237iface.queue(max_size=10, api_open=False).launch(show_api=False)238