From e05806982e399fb76a1be576f1480693117da33a Mon Sep 17 00:00:00 2001 From: masslevel Date: Mon, 5 Aug 2024 15:10:43 +0200 Subject: [PATCH] Fix for `Seed must be between 0 and 2**32 - 1` when using seed / int generator node This implements a seed function and fix for the error ` Seed must be between 0 and 2**32 - 1` when using a seed or int generator on the seed widget of the Florence2Run node. --- nodes.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/nodes.py b/nodes.py index 4cbae5e..3e5eb3f 100644 --- a/nodes.py +++ b/nodes.py @@ -211,7 +211,7 @@ class Florence2Run: "num_beams": ("INT", {"default": 3, "min": 1, "max": 64}), "do_sample": ("BOOLEAN", {"default": True}), "output_mask_select": ("STRING", {"default": ""}), - "seed": ("INT", {"default": 1, "min": 1, "max": 4294967295}), + "seed": ("INT", {"default": 1, "min": 1, "max": 0xffffffffffffffff}), } } @@ -220,6 +220,17 @@ class Florence2Run: FUNCTION = "encode" CATEGORY = "Florence2" + def hash_seed(self, seed): + import hashlib + # Convert the seed to a string and then to bytes + seed_bytes = str(seed).encode('utf-8') + # Create a SHA-256 hash of the seed bytes + hash_object = hashlib.sha256(seed_bytes) + # Convert the hash to an integer + hashed_seed = int(hash_object.hexdigest(), 16) + # Ensure the hashed seed is within the acceptable range for set_seed + return hashed_seed % (2**32) + def encode(self, image, text_input, florence2_model, task, fill_mask, keep_model_loaded=False, num_beams=3, max_new_tokens=1024, do_sample=True, output_mask_select="", seed=None): device = mm.get_torch_device() @@ -233,7 +244,7 @@ class Florence2Run: model.to(device) if seed: - set_seed(seed) + set_seed(self.hash_seed(seed)) colormap = ['blue','orange','green','purple','brown','pink','olive','cyan','red', 'lime','indigo','violet','aqua','magenta','gold','tan','skyblue']