farukbera commited on
Commit
518ea8e
·
1 Parent(s): 990c7b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -1
app.py CHANGED
@@ -46,6 +46,15 @@ else:
46
  def hex_to_rgb(hex_code):
47
  hex_code = hex_code.lstrip('#')
48
  return tuple(int(hex_code[i:i+2], 16) for i in (0, 2, 4))
 
 
 
 
 
 
 
 
 
49
 
50
  @app.get("/")
51
  async def root():
@@ -107,11 +116,15 @@ async def generate_new_img(hex_code: str, prompt: str = Query(..., description="
107
  ad_prompt = f"""Your system prompt is this: {prompt} Consider your system prompt first.
108
  Then from the initial image create a new image that will attract customers to put in an (ad template)
109
  Also, use this RGB color {hex_to_rgb(hex_code)} as a tone in the image while image is still recognized as it is original."""
 
 
 
110
  if device=="cpu":
111
  controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float32)
112
  pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
113
  "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float32
114
  ).to(device)
 
115
  else:
116
  controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float16)
117
  pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
@@ -130,7 +143,7 @@ async def generate_new_img(hex_code: str, prompt: str = Query(..., description="
130
  print(f"init_image type: {type(init_image)}")
131
 
132
  image = pipe(
133
- ad_prompt, image=init_image
134
  ).images[0]
135
  print(f"image type: {type(image)}")
136
 
 
46
  def hex_to_rgb(hex_code):
47
  hex_code = hex_code.lstrip('#')
48
  return tuple(int(hex_code[i:i+2], 16) for i in (0, 2, 4))
49
+
50
+ def get_depth_map(image, depth_estimator):
51
+ image = depth_estimator(image)["depth"]
52
+ image = np.array(image)
53
+ image = image[:, :, None]
54
+ image = np.concatenate([image, image, image], axis=2)
55
+ detected_map = torch.from_numpy(image).float() / 255.0
56
+ depth_map = detected_map.permute(2, 0, 1)
57
+ return depth_map
58
 
59
  @app.get("/")
60
  async def root():
 
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_code)} as a tone in the image while image is still recognized as it is original."""
119
+ print(f"uploaded image type: {type(uploaded_image)}")
120
+ depth_estimator = pipeline("depth-estimation")
121
+ depth_map = get_depth_map(uploaded_image, depth_estimator).unsqueeze(0).half().to(device)
122
  if device=="cpu":
123
  controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float32)
124
  pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
125
  "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float32
126
  ).to(device)
127
+
128
  else:
129
  controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float16)
130
  pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
 
143
  print(f"init_image type: {type(init_image)}")
144
 
145
  image = pipe(
146
+ ad_prompt, image=init_image, control_image=depth_map
147
  ).images[0]
148
  print(f"image type: {type(image)}")
149