Spaces:
Configuration error
Configuration error
Making it better and user centric (#3)
Browse files- Added 1:1 image option. (cb28aaad9d15356c2e7bae1450bc9204f6dd9895)
- Most OP update (c5920e9e86bd07191f3eb35f95cec69113f536d9)
Co-authored-by: Nishith Jain <KingNish@users.noreply.huggingface.co>
46 unmodified lines474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923012 unmodified lines2432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912926 unmodified lines29930030130230330430530630746 unmodified linespipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)prompt = "high quality"(prompt_embeds,negative_prompt_embeds,pooled_prompt_embeds,negative_pooled_prompt_embeds,) = pipe.encode_prompt(prompt, "cuda", True)"""def fill_image(image, model_selection):margin = 256overlap = 24# Open the original imagesource = image # Changed from image["background"] to match new input format# Calculate new output sizeoutput_size = (source.width + 2*margin, source.height + 2*margin)# Create a white backgroundbackground = Image.new('RGB', output_size, (255, 255, 255))# Calculate position to paste the original imageposition = (margin, margin)# Paste the original image onto the white backgroundbackground.paste(source, position)# Create the maskmask = Image.new('L', output_size, 255) # Start with all whitemask_draw = ImageDraw.Draw(mask)mask_draw.rectangle([(position[0] + overlap, position[1] + overlap),(position[0] + source.width - overlap, position[1] + source.height - overlap)], fill=0)# Prepare the image for ControlNetcnet_image = background.copy()cnet_image.paste(0, (0, 0), mask)for image in pipe(prompt_embeds=prompt_embeds,negative_prompt_embeds=negative_prompt_embeds,pooled_prompt_embeds=pooled_prompt_embeds,negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,image=cnet_image,):yield image, cnet_imageimage = image.convert("RGBA")cnet_image.paste(image, (0, 0), mask)yield background, cnet_image"""@spaces.GPUdef infer(image, model_selection, ratio_choice, overlap_width):source = imageif ratio_choice == "16:9":target_ratio = (16, 9) # Set the new target ratio to 16:9target_width = 1280 # Adjust target width based on desired resolutionoverlap = overlap_width#fade_width = 24max_height = 720 # Adjust max height instead of width# Resize the image if it's taller than max_heightif source.height > max_height:scale_factor = max_height / source.heightnew_height = max_heightnew_width = int(source.width * scale_factor)source = source.resize((new_width, new_height), Image.LANCZOS)# Calculate the required width for the 16:9 ratiotarget_width = (source.height * target_ratio[0]) // target_ratio[1]# Calculate margins (now left and right)margin_x = (target_width - source.width) // 2# Calculate new output sizeoutput_size = (target_width, source.height)# Create a white backgroundbackground = Image.new('RGB', output_size, (255, 255, 255))# Calculate position to paste the original imageposition = (margin_x, 0)# Paste the original image onto the white backgroundbackground.paste(source, position)# Create the maskmask = Image.new('L', output_size, 255) # Start with all whitemask_draw = ImageDraw.Draw(mask)mask_draw.rectangle([(margin_x + overlap, overlap),(margin_x + source.width - overlap, source.height - overlap)], fill=0)# Prepare the image for ControlNetcnet_image = background.copy()cnet_image.paste(0, (0, 0), mask)for image in pipe(prompt_embeds=prompt_embeds,negative_prompt_embeds=negative_prompt_embeds,pooled_prompt_embeds=pooled_prompt_embeds,negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,image=cnet_image,):yield cnet_image, imageimage = image.convert("RGBA")cnet_image.paste(image, (0, 0), mask)yield background, cnet_imageelif ratio_choice == "9:16":target_ratio=(9, 16)target_height=1280overlap=overlap_width#fade_width=24max_width = 720# Resize the image if it's wider than max_widthif source.width > max_width:scale_factor = max_width / source.widthnew_width = max_widthnew_height = int(source.height * scale_factor)source = source.resize((new_width, new_height), Image.LANCZOS)# Calculate the required height for 9:16 ratiotarget_height = (source.width * target_ratio[1]) // target_ratio[0]# Calculate margins (only top and bottom)margin_y = (target_height - source.height) // 2# Calculate new output sizeoutput_size = (source.width, target_height)# Create a white backgroundbackground = Image.new('RGB', output_size, (255, 255, 255))# Calculate position to paste the original imageposition = (0, margin_y)# Paste the original image onto the white backgroundbackground.paste(source, position)# Create the maskmask = Image.new('L', output_size, 255) # Start with all whitemask_draw = ImageDraw.Draw(mask)mask_draw.rectangle([(overlap, margin_y + overlap),(source.width - overlap, margin_y + source.height - overlap)], fill=0)# Prepare the image for ControlNetcnet_image = background.copy()cnet_image.paste(0, (0, 0), mask)for image in pipe(prompt_embeds=prompt_embeds,negative_prompt_embeds=negative_prompt_embeds,pooled_prompt_embeds=pooled_prompt_embeds,negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,image=cnet_image,):yield cnet_image, imageimage = image.convert("RGBA")cnet_image.paste(image, (0, 0), mask)yield background, cnet_imagedef clear_result():return gr.update(value=None)12 unmodified lineswith gr.Blocks(css=css) as demo:with gr.Column():gr.HTML(title)with gr.Row():with gr.Column():input_image = gr.Image(type="pil",label="Input Image",sources=["upload"],)with gr.Row():ratio = gr.Radio(label="Expected ratio",choices=["9:16", "16:9"],value = "9:16")model_selection = gr.Dropdown(choices=list(MODELS.keys()),value="RealVisXL V5.0 Lightning",label="Model",)overlap_width = gr.Slider(label="Mask overlap width",minimum = 1,maximum = 50,value = 42,step = 1)run_button = gr.Button("Generate")gr.Examples(examples = [["./examples/example_1.webp", "RealVisXL V5.0 Lightning", "16:9"],["./examples/example_2.jpg", "RealVisXL V5.0 Lightning", "16:9"],["./examples/example_3.jpg", "RealVisXL V5.0 Lightning", "9:16"]],inputs = [input_image, model_selection, ratio])with gr.Column():result = ImageSlider(interactive=False,6 unmodified linesoutputs=result,).then(fn=infer,inputs=[input_image, model_selection, ratio, overlap_width],outputs=result,)demo.launch(share=False)46 unmodified lines474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411512 unmodified lines1281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871886 unmodified lines19519619719819920020120220320420520620720820921021121246 unmodified linespipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)@spaces.GPUdef infer(image, model_selection, width, height, overlap_width, num_inference_steps, prompt_input=None):source = imagetarget_size = (width, height)target_ratio = (width, height) # Calculate aspect ratio from width and heightoverlap = overlap_width# Upscale if source is smaller than target in both dimensionsif source.width < target_size[0] and source.height < target_size[1]:scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)new_width = int(source.width * scale_factor)new_height = int(source.height * scale_factor)source = source.resize((new_width, new_height), Image.LANCZOS)if source.width > target_size[0] or source.height > target_size[1]:scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)new_width = int(source.width * scale_factor)new_height = int(source.height * scale_factor)source = source.resize((new_width, new_height), Image.LANCZOS)margin_x = (target_size[0] - source.width) // 2margin_y = (target_size[1] - source.height) // 2background = Image.new('RGB', target_size, (255, 255, 255))background.paste(source, (margin_x, margin_y))mask = Image.new('L', target_size, 255)mask_draw = ImageDraw.Draw(mask)mask_draw.rectangle([(margin_x + overlap, margin_y + overlap),(margin_x + source.width - overlap, margin_y + source.height - overlap)], fill=0)cnet_image = background.copy()cnet_image.paste(0, (0, 0), mask)final_prompt = "high quality"if prompt_input.strip() != "":final_prompt += ", " + prompt_input(prompt_embeds,negative_prompt_embeds,pooled_prompt_embeds,negative_pooled_prompt_embeds,) = pipe.encode_prompt(final_prompt, "cuda", True)for image in pipe(prompt_embeds=prompt_embeds,negative_prompt_embeds=negative_prompt_embeds,pooled_prompt_embeds=pooled_prompt_embeds,negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,image=cnet_image,num_inference_steps=num_inference_steps):yield cnet_image, imageimage = image.convert("RGBA")cnet_image.paste(image, (0, 0), mask)yield background, cnet_imagedef clear_result():return gr.update(value=None)12 unmodified lineswith gr.Blocks(css=css) as demo:with gr.Column():gr.HTML(title)with gr.Row():with gr.Column():input_image = gr.Image(type="pil",label="Input Image",sources=["upload"],)with gr.Row():with gr.Column(scale=2):prompt_input = gr.Textbox(label="Prompt (Optional)")with gr.Column(scale=1):run_button = gr.Button("Generate")with gr.Row():width_slider = gr.Slider(label="Width",minimum=720,maximum=1440,step=8,value=1440, # Set a default value)height_slider = gr.Slider(label="Height",minimum=720,maximum=1440,step=8,value=1024, # Set a default value)model_selection = gr.Dropdown(choices=list(MODELS.keys()),value="RealVisXL V5.0 Lightning",label="Model",)num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8 )overlap_width = gr.Slider(label="Mask overlap width",minimum=1,maximum=50,value=42,step=1)gr.Examples(examples=[["./examples/example_1.webp", "RealVisXL V5.0 Lightning", 1280, 720],["./examples/example_2.jpg", "RealVisXL V5.0 Lightning", 720, 1280],["./examples/example_3.jpg", "RealVisXL V5.0 Lightning", 1024, 1024],],inputs=[input_image, model_selection, width_slider, height_slider],)with gr.Column():result = ImageSlider(interactive=False,6 unmodified linesoutputs=result,).then(fn=infer,inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input],outputs=result,)prompt_input.submit(fn=clear_result,inputs=None,outputs=result,).then(fn=infer,inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input],outputs=result,)demo.launch(share=False)
app.py+85-180