farukbera commited on
Commit
8956d82
·
1 Parent(s): bc895e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -115,16 +115,16 @@ async def generate_new_img(hex_code: str, prompt: str = Query(..., description="
115
  # Generate the image using the text-to-image model
116
  ad_prompt = f"""Your system prompt is this: {prompt} Consider your system prompt first.
117
  Then from the initial image create a new image that will attract customers to put in an (ad template)
118
- Also, use this hex code {hex_to_rgb(hex_color)} as a tone in the image while image is still recognized as it is original."""
119
  if device=="cpu":
120
- controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float32, use_safetensors=True)
121
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
122
  "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float32, use_safetensors=True
123
  ).to(device)
124
  else:
125
- controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16, use_safetensors=True)
126
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
127
- "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float32, use_safetensors=True
128
  ).to(device)
129
  pipe.enable_model_cpu_offload()
130
 
@@ -156,7 +156,7 @@ async def generate_new_img(hex_code: str, prompt: str = Query(..., description="
156
  async def create_ad_template(punchline: str, punchline_color:str, button_text:str,button_color:str):
157
  if uploaded_image is not None and uploaded_logo is not None and created_image is not None:
158
  # Create LOGO
159
-
160
  #logo_width, logo_height = logo_image.size
161
  logo_width, logo_height = 100, 100 # Desired size for the logo
162
  logo_image = logo_image.resize((logo_width, logo_height))
@@ -171,16 +171,20 @@ async def create_ad_template(punchline: str, punchline_color:str, button_text:st
171
 
172
  # Create a blank canvas for the ad template
173
  ad_template = Image.new("RGB", (800, 600), "#FFFFFF")
 
174
  # Add logo
175
  ad_template.paste(logo_image, logo_image_location)
 
176
  # Add generated image
177
  ad_template.paste(generated_image, generated_image_location)
 
178
  # Add the text at the bottom of the ad template
179
  draw = ImageDraw.Draw(ad_template)
180
  font = ImageFont.truetype("arial.ttf", 24)
181
  text_width, text_height = draw.textsize(punchline, font=font)
182
  text_position = (400 - text_width / 2, 500 - text_height / 2)
183
  draw.text(text_position, punchline, font=font, fill=punchline_color)
 
184
  # Add the button at the bottom of the ad template
185
  button_width, button_height = 200, 50
186
  button_position = ((400 - button_width / 2), (550 - button_height / 2))
@@ -190,15 +194,15 @@ async def create_ad_template(punchline: str, punchline_color:str, button_text:st
190
  text_position = (button_position[0] + (button_width - text_width) / 2, button_position[1] + (button_height - text_height) / 2)
191
  rect_text_color = (255, 255, 255) # Text color within the rectangle
192
  draw.text(text_position, button_text, fill=rect_text_color, font=font)
193
-
194
  border_width = 2 # Border width
195
  border_positions = [0, 0, 800, 600]
196
  draw.rectangle(border_positions, outline="#D60D0D", width=border_width)
197
-
198
  ad_template_data = io.BytesIO()
199
  ad_template.save(ad_template_data, format="PNG")
200
  ad_template_data.seek(0)
201
-
202
  return StreamingResponse(ad_template_data, media_type="image/png")
203
 
204
  else:
 
115
  # Generate the image using the text-to-image model
116
  ad_prompt = f"""Your system prompt is this: {prompt} Consider your system prompt first.
117
  Then from the initial image create a new image that will attract customers to put in an (ad template)
118
+ Also, use this RGB color {hex_to_rgb(hex_color)} as a tone in the image while image is still recognized as it is original."""
119
  if device=="cpu":
120
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float32, use_safetensors=True)
121
+ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
122
  "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float32, use_safetensors=True
123
  ).to(device)
124
  else:
125
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float16, use_safetensors=True)
126
+ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
127
+ "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True
128
  ).to(device)
129
  pipe.enable_model_cpu_offload()
130
 
 
156
  async def create_ad_template(punchline: str, punchline_color:str, button_text:str,button_color:str):
157
  if uploaded_image is not None and uploaded_logo is not None and created_image is not None:
158
  # Create LOGO
159
+ print("Drawing")
160
  #logo_width, logo_height = logo_image.size
161
  logo_width, logo_height = 100, 100 # Desired size for the logo
162
  logo_image = logo_image.resize((logo_width, logo_height))
 
171
 
172
  # Create a blank canvas for the ad template
173
  ad_template = Image.new("RGB", (800, 600), "#FFFFFF")
174
+ print("Template created")
175
  # Add logo
176
  ad_template.paste(logo_image, logo_image_location)
177
+ print("Logo added")
178
  # Add generated image
179
  ad_template.paste(generated_image, generated_image_location)
180
+ print("Image added")
181
  # Add the text at the bottom of the ad template
182
  draw = ImageDraw.Draw(ad_template)
183
  font = ImageFont.truetype("arial.ttf", 24)
184
  text_width, text_height = draw.textsize(punchline, font=font)
185
  text_position = (400 - text_width / 2, 500 - text_height / 2)
186
  draw.text(text_position, punchline, font=font, fill=punchline_color)
187
+ print("Punchline added")
188
  # Add the button at the bottom of the ad template
189
  button_width, button_height = 200, 50
190
  button_position = ((400 - button_width / 2), (550 - button_height / 2))
 
194
  text_position = (button_position[0] + (button_width - text_width) / 2, button_position[1] + (button_height - text_height) / 2)
195
  rect_text_color = (255, 255, 255) # Text color within the rectangle
196
  draw.text(text_position, button_text, fill=rect_text_color, font=font)
197
+
198
  border_width = 2 # Border width
199
  border_positions = [0, 0, 800, 600]
200
  draw.rectangle(border_positions, outline="#D60D0D", width=border_width)
201
+ print("Button added")
202
  ad_template_data = io.BytesIO()
203
  ad_template.save(ad_template_data, format="PNG")
204
  ad_template_data.seek(0)
205
+ print("Template finished")
206
  return StreamingResponse(ad_template_data, media_type="image/png")
207
 
208
  else: