201
202 return style, normalization, text
203
204
205 def encode_regions(clip, tokens, regions, weight_interpretation="comfy", token_normalization="none"):
206 from custom_nodes.ComfyUI_Cutoff.cutoff import CLIPSetRegion, finalize_clip_regions
207
208 clip_regions = {
209 "clip": clip,
210 "base_tokens": tokens,
211 "regions": [],
212 "targets": [],
213 "weights": [],
214 }
215
216 strict_mask = 1.0
217 start_from_masked = 1.0
218 mask_token = ""
219
220 for region in regions:
221 region_text, target_text, w, sm, sfm, mt = region
222 if w is not None:
223 w = safe_float(w, 0)
224 else:
225 w = 1.0
226 if sm is not None:
227 strict_mask = safe_float(sm, 1.0)
228 if sfm is not None:
229 start_from_masked = safe_float(sfm, 1.0)
230 if mt is not None:
231 mask_token = mt
232 log.info("Region: text %s, target %s, weight %s", region_text.strip(), target_text.strip(), w)
233 (clip_regions,) = CLIPSetRegion.add_clip_region(None, clip_regions, region_text, target_text, w)
234 log.info("Regions: mask_token=%s strict_mask=%s start_from_masked=%s", mask_token, strict_mask, start_from_masked)
235
236 (r,) = finalize_clip_regions(
237 clip_regions, mask_token, strict_mask, start_from_masked, token_normalization, weight_interpretation
238 )
239 cond, pooled = r[0][0], r[0][1].get("pooled_output")
240 return cond, pooled
241
242
243 SHUFFLE_GEN = torch.Generator(device="cpu")