Spaces:
Runtime error
Runtime error
File size: 7,637 Bytes
537fd2c 62eacc2 537fd2c 93e795a 537fd2c 93e795a 537fd2c 93e795a 537fd2c 93e795a 537fd2c 93e795a 537fd2c 93e795a 537fd2c 93e795a 537fd2c |
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
import os
import gradio as gr
from background_replacer import replace_background
developer_mode = os.getenv('DEV_MODE', False)
DEFAULT_POSITIVE_PROMPT = "on the pavement, poolside, idyllic infinity pool, Hawaiian hilltops, commercial product photography"
DEFAULT_NEGATIVE_PROMPT = ""
EXAMPLES = [
[
"examples/black-sneakers-with-white-sole.jpg",
"on the grass in Central Park, gorgeous summer day with Bethesda fountain in the background, commercial footwear product photography",
"people, litter, trash, crowds, messy",
],
[
"examples/DIY-beard-balm.jpg",
"on a mossy rock, white wood anemone blossoms, Loch Ken, Scotland",
"purple, wrong proportions",
],
[
"examples/dj-making-music-on-mixer.jpg",
"on the turntables with a packed dance floor, epic midnight edm party in Miami Beach, colorful nightlife photography",
"disfigured, dismembered, mangled, marred",
],
[
"examples/jean-shorts-woman.jpg",
"on the beach in Malibu, a five-star beachfront hotel in the background, stark late afternoon light near the dunes, lifestyle photography",
"blurry background, ripples, soft focus, bokeh",
],
]
INTRO = """
# SDXL Background Replacement for Product Images
_from your friends at_ 🛍️[Shopify](https://www.shopify.com/)
Building 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. 😅
To 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:
1. ❌ _Do not_ describe your product in the prompt (ex: black sneakers)
2. ✅ Do describe the "grounding" for your product (ex: placed on a table)
3. ✅ Do describe the scene you want (ex: in a greek cottage)
4. ✅ Do describe a style of image (ex: side view commercial product photography)
5. 🤔 Optionally, describe what you want to avoid 🙅 in the negative prompt field
"""
MORE_INFO = """
### More information
- You can check our [FAQs here](https://huggingface.co/spaces/Shopify/background-replacement/blob/main/README.md#faqs)!
- We are also gathering resources from the community and sharing ideas [here](https://huggingface.co/spaces/Shopify/background-replacement/discussions).
- 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)!
"""
def generate(
image,
positive_prompt,
negative_prompt,
seed,
depth_map_feather_threshold,
depth_map_dilation_iterations,
depth_map_blur_radius,
progress=gr.Progress(track_tqdm=True)
):
if image is None:
return [None, None, None, None]
options = {
'seed': seed,
'depth_map_feather_threshold': depth_map_feather_threshold,
'depth_map_dilation_iterations': depth_map_dilation_iterations,
'depth_map_blur_radius': depth_map_blur_radius,
}
return replace_background(image, positive_prompt, negative_prompt, options)
custom_css = """
#image-upload {
flex-grow: 1;
}
#params .tabs {
display: flex;
flex-direction: column;
flex-grow: 1;
}
#params .tabitem[style="display: block;"] {
flex-grow: 1;
display: flex !important;
}
#params .gap {
flex-grow: 1;
}
#params .form {
flex-grow: 1 !important;
}
#params .form > :last-child{
flex-grow: 1;
}
"""
with gr.Blocks(css=custom_css) as iface:
gr.Markdown(INTRO)
with gr.Row():
with gr.Column():
image_upload = gr.Image(
label="Product image",
type="pil",
elem_id="image-upload"
)
caption = gr.Label(
label="Caption",
visible=developer_mode
)
with gr.Column(elem_id="params"):
with gr.Tab('Prompts'):
positive_prompt = gr.Textbox(
label="Positive Prompt: describe what you'd like to see",
lines=3,
value=DEFAULT_POSITIVE_PROMPT
)
negative_prompt = gr.Textbox(
label="Negative Prompt: describe what you want to avoid",
lines=3,
value=DEFAULT_NEGATIVE_PROMPT
)
if developer_mode:
with gr.Tab('Options'):
seed = gr.Number(
label="Seed",
precision=0,
value=0,
elem_id="seed",
visible=developer_mode
)
depth_map_feather_threshold = gr.Slider(
label="Depth map feather threshold",
value=128,
minimum=0,
maximum=255,
visible=developer_mode
)
depth_map_dilation_iterations = gr.Number(
label="Depth map dilation iterations",
precision=0,
value=10,
minimum=0,
visible=developer_mode
)
depth_map_blur_radius = gr.Number(
label="Depth map blur radius",
precision=0,
value=10,
minimum=0,
visible=developer_mode
)
else:
seed = gr.Number(value=-1, visible=False)
depth_map_feather_threshold = gr.Slider(
value=128, visible=False)
depth_map_dilation_iterations = gr.Number(
precision=0, value=10, visible=False)
depth_map_blur_radius = gr.Number(
precision=0, value=10, visible=False)
gen_button = gr.Button(value="Generate!", variant="primary")
with gr.Tab('Results'):
results = gr.Gallery(
show_label=False,
object_fit="contain",
columns=4
)
if developer_mode:
with gr.Tab('Generated'):
generated = gr.Gallery(
show_label=False,
object_fit="contain",
columns=4
)
with gr.Tab('Pre-processing'):
pre_processing = gr.Gallery(
show_label=False,
object_fit="contain",
columns=4
)
else:
generated = gr.Gallery(visible=False)
pre_processing = gr.Gallery(visible=False)
gr.Examples(
examples=EXAMPLES,
inputs=[image_upload, positive_prompt, negative_prompt],
)
gr.Markdown(MORE_INFO)
gen_button.click(
fn=generate,
inputs=[
image_upload,
positive_prompt,
negative_prompt,
seed,
depth_map_feather_threshold,
depth_map_dilation_iterations,
depth_map_blur_radius
],
outputs=[
results,
generated,
pre_processing,
caption
],
)
iface.queue(api_open=False).launch(show_api=False)
|