Spaces:
Build error
Build error
FridaKahlosArtCenter commited on
Commit ·
90e022a
1
Parent(s): 2740cfb
colab working script
Browse filesFiles changed (7)
- app.py+154-50
- assets/Montserrat-Bold.ttfAdded
- assets/Montserrat-Regular.ttfAdded
- assets/city_image.jpg
- assets/logo.jpgDeleted
- assets/logo.pngAdded
- requirements.txt+1-1
3 unmodified lines456789101112131415161718192021222324252627282930313233343536373839404142434445464748493 unmodified lines535455565758596061626364656667686970717273747576777879808182838485863 unmodified linesimport requestsfrom io import BytesIOimport gradio as gr# log gpu availabilituprint(f"Is CUDA available: {torch.cuda.is_available()}")print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")def image_to_template(generated_image, logo, button_text, punchline, theme_color):# Resize logo if neededlogo = logo.resize((100, 100)) # Example size, adjust as needed# Create a blank canvas with extra space for logo, punchline, and buttoncanvas_width = max(generated_image.width, logo.width) * 2canvas_height = generated_image.height + logo.height + 100canvas = Image.new('RGB', (canvas_width, canvas_height), 'white')# Paste the logo and the generated image onto the canvascanvas.paste(logo, (10, 10)) # Adjust position as neededcanvas.paste(generated_image, (0, logo.height + 20))# Add punchline and buttondraw = ImageDraw.Draw(canvas)font = ImageFont.load_default() # Or use a custom fonttext_color = theme_color# Draw punchlinedraw.text((10, logo.height + generated_image.height + 30), punchline, fill=text_color, font=font)# Draw buttonbutton_position = (10, logo.height + generated_image.height + 60) # Adjust as neededdraw.rectangle([button_position, (canvas_width - 10, canvas_height - 10)], outline=theme_color, fill=text_color)draw.text(button_position, button_text, font=font)return canvasdef generate_template(initial_image, logo, prompt, button_text, punchline, image_color, theme_color):pipeline = AutoPipelineForImage2Image.from_pretrained("./models/kandinsky-2-2-decoder",torch_dtype=torch.float16,variant="fp16",use_safetensors=True)# pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True)3 unmodified linesnegative_prompt = "low quality, bad quality, blurry, unprofessional"generated_image = pipeline(prompt=prompt,negative_prompt=negative_prompt,image=initial_image,height=256,width=256).images[0]template_image = image_to_template(generated_image, logo, button_text, punchline, theme_color)return template_image# Set up Gradio interfaceiface = gr.Interface(fn=generate_template,inputs=[gr.inputs.Image(type="pil", label="Initial Image"),gr.inputs.Image(type="pil", label="Logo"),gr.inputs.Textbox(label="Prompt"),gr.inputs.Textbox(label="Button Text"),gr.inputs.Textbox(label="Punchline"),gr.inputs.ColorPicker(label="Image Color"),gr.inputs.ColorPicker(label="Theme Color")],outputs=[gr.outputs.Image(type="pil")],title="Ad Template Generation Using Diffusion Models Demo",description="Generate ad template based on your inputs using a trained model.",concurrency_limit=2,# examples=[# []# ])# Run the interfaceiface.launch()3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331343 unmodified lines1381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891903 unmodified linesimport requestsfrom io import BytesIOimport gradio as grimport gcimport textwrap# log gpu availabilityprint(f"Is CUDA available: {torch.cuda.is_available()}")print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")def image_to_template(generated_image, logo, button_text, punchline, theme_color):template_width = 540button_font_size = 10punchline_font_size = 30decoration_height = 10margin = 20# wrap punchline textpunchline = textwrap.wrap(punchline, width=35)n_of_lines_punchline = len(punchline)generated_image = generated_image.convert("RGBA")logo = logo.convert("RGBA")# image shapeimage_width = template_width // 2image_height = image_width * generated_image.height // generated_image.widthimage_shape = (image_width, image_height)# logo shapelogo_width = image_width // 3logo_height = logo_width * logo.height // logo.widthlogo_shape = (logo_width, logo_height)# Define fontsbutton_font = ImageFont.truetype("Montserrat-Bold.ttf", button_font_size)punchline_font = ImageFont.truetype("Montserrat-Bold.ttf", punchline_font_size)# button shapebutton_width = template_width // 3button_height = button_font_size * 3# template height calculationtemplate_height = (image_height+ logo_height+ button_height+ n_of_lines_punchline * punchline_font_size+ (5 * margin)+ (2 * decoration_height))# Calculate positions for the centered layoutlogo_pos = ((template_width - logo_width) // 2, margin + decoration_height)image_pos = ((template_width - image_width) // 2,logo_pos[1] + logo_height + margin,)# Decoration positionstop_decoration_pos = [margin,-decoration_height // 2,template_width - margin,decoration_height // 2,]bottom_decoration_pos = [margin,template_height - decoration_height // 2,template_width - margin,template_height + decoration_height // 2,]# Generate Componentsgenerated_image.thumbnail(image_shape, Image.ANTIALIAS)logo.thumbnail(logo_shape, Image.ANTIALIAS)background = Image.new("RGBA", (template_width, template_height), "WHITE")# round the corners of generated imagemask = Image.new("L", generated_image.size, 0)draw = ImageDraw.Draw(mask)draw.rounded_rectangle((0, 0) + generated_image.size, 20, fill=255)generated_image.putalpha(mask)# Paste the logo and the generated image onto the backgroundbackground.paste(logo, logo_pos, logo)background.paste(generated_image, image_pos, generated_image)# Draw the decorations, punchline, and buttondraw = ImageDraw.Draw(background)# Decorations on top and bottomdraw.rounded_rectangle(bottom_decoration_pos, radius=20, fill=theme_color)draw.rounded_rectangle(top_decoration_pos, radius=20, fill=theme_color)# Punchline texttext_heights = []for line in punchline:text_width, text_height = draw.textsize(line, font=punchline_font)punchline_pos = ((template_width - text_width) // 2,image_pos[1] + generated_image.height + margin + sum(text_heights),)draw.text(punchline_pos, line, fill=theme_color, font=punchline_font)text_heights.append(text_height)# Button with rounded cornersbutton_text_width, button_text_height = draw.textsize(button_text, font=button_font)button_shape = [((template_width - button_width) // 2, punchline_pos[1] + text_height + margin),((template_width + button_width) // 2,punchline_pos[1] + text_height + margin + button_height,),]draw.rounded_rectangle(button_shape, radius=20, fill=theme_color)# Button textbutton_text_pos = ((template_width - button_text_width) // 2,button_shape[0][1] + (button_height - button_text_height) // 2,)draw.text(button_text_pos, button_text, fill="white", font=button_font)return backgrounddef generate_template(initial_image, logo, prompt, button_text, punchline, image_color, theme_color):pipeline = AutoPipelineForImage2Image.from_pretrained("./models/kandinsky-2-2-decoder",torch_dtype=torch.float16,use_safetensors=True,)# pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True)3 unmodified linesnegative_prompt = "low quality, bad quality, blurry, unprofessional"generated_image = pipeline(prompt=prompt,negative_prompt=negative_prompt,image=initial_image,height=256,width=256,).images[0]template_image = image_to_template(generated_image, logo, button_text, punchline, theme_color)# free cpu and gpu memorydel pipelinegc.collect()torch.cuda.empty_cache()return template_image# Set up Gradio interfaceiface = gr.Interface(fn=generate_template,inputs=[gr.Image(type="pil", label="Initial Image"),gr.Image(type="pil", label="Logo"),gr.Textbox(label="Prompt"),gr.Textbox(label="Button Text"),gr.Textbox(label="Punchline"),gr.ColorPicker(label="Image Color"),gr.ColorPicker(label="Theme Color"),],outputs=[gr.Image(type="pil")],title="Ad Template Generation Using Diffusion Models Demo",description="Generate ad template based on your inputs using a trained model.",concurrency_limit=2,examples=[["/path/to/example_initial_image1.jpg", # Initial Image"/path/to/example_logo1.png", # Logo"A scenic mountain landscape", # Prompt"Discover More", # Button Text"Escape into Nature", # Punchline"#00FF00", # Image Color"#0000FF", # Theme Color]],)# Run the interfaceiface.launch(debug=True)
2 unmodified lines3456782 unmodified lines--extra-index-url https://download.pytorch.org/whl/cu113torchgradioPILrequestssafetensors2 unmodified lines3456782 unmodified lines--extra-index-url https://download.pytorch.org/whl/cu113torchgradiopillowrequestssafetensors
app.py+154-50
assets/Montserrat-Bold.ttfAdded
assets/Montserrat-Regular.ttfAdded
assets/city_image.jpg


assets/logo.jpgDeleted
assets/logo.pngAdded

requirements.txt+1-1