Spaces:
Runtime error
Runtime error
Add resize options (#14)
Browse files- Add resize options (e1d2b35411744785a8c4a5bc69e36da4bb46e976)
Co-authored-by: ApolinΓ‘rio from multimodal AI art <multimodalart@users.noreply.huggingface.co>
51 unmodified lines5253545556575859606110 unmodified lines72737475767775 unmodified lines1531541551561571581591601611621631644 unmodified lines1691701711721731742 unmodified lines17717817918018118218318418532 unmodified lines21821922022122222322430 unmodified lines25525625725825926030 unmodified lines2912922932942952962972982993003013023033043053063073 unmodified lines3113123133143153163178 unmodified lines32632732832933033133251 unmodified linesreturn Falsereturn True@spaces.GPU(duration=16)def infer(image, width, height, overlap_width, num_inference_steps, prompt_input=None, alignment="Middle"):source = imagetarget_size = (width, height)overlap = overlap_width10 unmodified linesnew_width = int(source.width * scale_factor)new_height = int(source.height * scale_factor)source = source.resize((new_width, new_height), Image.LANCZOS)if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):alignment = "Middle"75 unmodified linesyield background, cnet_imagedef clear_result():"""Clears the result ImageSlider."""return gr.update(value=None)def preload_presets(target_ratio, ui_width, ui_height):"""Updates the width and height sliders based on the selected aspect ratio."""if target_ratio == "9:16":4 unmodified lineschanged_width = 1280changed_height = 720return changed_width, changed_height, gr.update(open=False)elif target_ratio == "Custom":return ui_width, ui_height, gr.update(open=True)2 unmodified linesreturn "9:16"elif user_width == 1280 and user_height == 720:return "16:9"else:return "Custom"css = """.gradio-container {width: 1200px !important;32 unmodified lineswith gr.Row():target_ratio = gr.Radio(label="Expected Ratio",choices=["9:16", "16:9", "Custom"],value="9:16",scale=2)30 unmodified linesvalue=42,step=1)gr.Examples(examples=[30 unmodified lines)width_slider.change(fn = select_the_right_preset,inputs = [width_slider, height_slider],outputs = [target_ratio],queue = False)height_slider.change(fn = select_the_right_preset,inputs = [width_slider, height_slider],outputs = [target_ratio],queue = False)run_button.click(3 unmodified lines).then(fn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,prompt_input, alignment_dropdown],outputs=result,).then(fn=lambda: gr.update(visible=True),8 unmodified lines).then(fn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,prompt_input, alignment_dropdown],outputs=result,).then(fn=lambda: gr.update(visible=True),51 unmodified lines525354555657585910 unmodified lines7071727374757677787980818283848586878889909175 unmodified lines1671681691701711721731741751764 unmodified lines1811821831841851861871881891902 unmodified lines19319419519619719819920020120220320420520632 unmodified lines23924024124224324424530 unmodified lines27627727827928028128228328428528628728828929029129229329429530 unmodified lines3263273283293303313323333343353363373383393403413423433443453463473483493 unmodified lines3533543553563573583598 unmodified lines36836937037137237337451 unmodified linesreturn Falsereturn True@spaces.GPU(duration=24)def infer(image, width, height, overlap_width, num_inference_steps, resize_option, custom_resize_size, prompt_input=None, alignment="Middle"):source = imagetarget_size = (width, height)overlap = overlap_width10 unmodified linesnew_width = int(source.width * scale_factor)new_height = int(source.height * scale_factor)source = source.resize((new_width, new_height), Image.LANCZOS)if resize_option == "Full":resize_size = max(source.width, source.height)elif resize_option == "1/2":resize_size = max(source.width, source.height) // 2elif resize_option == "1/3":resize_size = max(source.width, source.height) // 3elif resize_option == "1/4":resize_size = max(source.width, source.height) // 4else: # Customresize_size = custom_resize_sizeaspect_ratio = source.height / source.widthnew_width = resize_sizenew_height = int(resize_size * aspect_ratio)source = source.resize((new_width, new_height), Image.LANCZOS)if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):alignment = "Middle"75 unmodified linesyield background, cnet_imagedef clear_result():"""Clears the result ImageSlider."""return gr.update(value=None)def preload_presets(target_ratio, ui_width, ui_height):"""Updates the width and height sliders based on the selected aspect ratio."""if target_ratio == "9:16":4 unmodified lineschanged_width = 1280changed_height = 720return changed_width, changed_height, gr.update(open=False)elif target_ratio == "1:1":changed_width = 1024changed_height = 1024return changed_width, changed_height, gr.update(open=False)elif target_ratio == "Custom":return ui_width, ui_height, gr.update(open=True)2 unmodified linesreturn "9:16"elif user_width == 1280 and user_height == 720:return "16:9"elif user_width == 1024 and user_height == 1024:return "1:1"else:return "Custom"def toggle_custom_resize_slider(resize_option):return gr.update(visible=(resize_option == "Custom"))css = """.gradio-container {width: 1200px !important;32 unmodified lineswith gr.Row():target_ratio = gr.Radio(label="Expected Ratio",choices=["9:16", "16:9", "1:1", "Custom"],value="9:16",scale=2)30 unmodified linesvalue=42,step=1)with gr.Row():resize_option = gr.Radio(label="Resize input image",choices=["Full", "1/2", "1/3", "1/4", "Custom"],value="Full")custom_resize_size = gr.Slider(label="Custom resize size",minimum=64,maximum=1024,step=8,value=512,visible=False)gr.Examples(examples=[30 unmodified lines)width_slider.change(fn=select_the_right_preset,inputs=[width_slider, height_slider],outputs=[target_ratio],queue=False)height_slider.change(fn=select_the_right_preset,inputs=[width_slider, height_slider],outputs=[target_ratio],queue=False)resize_option.change(fn=toggle_custom_resize_slider,inputs=[resize_option],outputs=[custom_resize_size],queue=False)run_button.click(3 unmodified lines).then(fn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,resize_option, custom_resize_size, prompt_input, alignment_dropdown],outputs=result,).then(fn=lambda: gr.update(visible=True),8 unmodified lines).then(fn=infer,inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,resize_option, custom_resize_size, prompt_input, alignment_dropdown],outputs=result,).then(fn=lambda: gr.update(visible=True),
app.py+59-17