From 80d5a8f2a9d88b269f1ecc048b6582868eec3476 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 11 Jun 2025 09:00:12 +0000 Subject: [PATCH] feat: Add bypass_mode to SSL_GeminiTextPrompt This commit introduces a new `bypass_mode` multiple choice input to the `SSL_GeminiTextPrompt` node. This feature allows you to choose whether to apply a special underscore padding to the "system_instruction", the "prompt", or "both" before sending them to the Gemini API. The padding format is `_c_h_a_r_a_c_t_e_r_s_`. The following changes were made: - Added `bypass_mode` to the `INPUT_TYPES` with options: "None", "system_instruction", "prompt", "both". - Implemented a helper method `_pad_text_with_underscores` to perform the character padding. - Modified the `generate` method to: - Accept `bypass_mode` as a parameter. - Conditionally apply padding to `system_instruction` and `prompt` based on the selected `bypass_mode`. - Use the (potentially) padded text in the API call to Gemini. --- gemini_nodes.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/gemini_nodes.py b/gemini_nodes.py index 0e67f8a..4919299 100644 --- a/gemini_nodes.py +++ b/gemini_nodes.py @@ -73,6 +73,7 @@ class SSL_GeminiTextPrompt(ComfyNodeABC): "top_k": (IO.INT, {"default": 40, "min": 1, "max": 100, "step": 1}), "max_output_tokens": (IO.INT, {"default": 8192, "min": 1, "max": 8192, "step": 1}), "include_images": (IO.BOOLEAN, {"default": True}), + "bypass_mode": (["None", "system_instruction", "prompt", "both"], {"default": "None"}), }, "optional": { "input_image": (IO.IMAGE,), @@ -90,6 +91,11 @@ class SSL_GeminiTextPrompt(ComfyNodeABC): FUNCTION = "generate" CATEGORY = "API/Gemini" + def _pad_text_with_underscores(self, text: str) -> str: + if not text: + return "" + return "_" + "_".join(list(text)) + "_" + def save_binary_file(self, data, mime_type): ext = ".bin" if mime_type == "image/png": @@ -113,7 +119,7 @@ class SSL_GeminiTextPrompt(ComfyNodeABC): tensor = torch.from_numpy(empty_image).unsqueeze(0) return tensor - def generate(self, config, prompt, system_instruction, model, temperature, top_p, top_k, max_output_tokens, include_images, + def generate(self, config, prompt, system_instruction, model, temperature, top_p, top_k, max_output_tokens, include_images, bypass_mode, input_image=None, input_image_2=None, use_proxy=False, proxy_host="127.0.0.1", proxy_port=7890, use_seed=True, seed=0): original_http_proxy = os.environ.get('HTTP_PROXY') @@ -123,6 +129,15 @@ class SSL_GeminiTextPrompt(ComfyNodeABC): print(f"[INFO] Starting generation, model: {model}, temperature: {temperature}") + # Pad text based on bypass_mode + padded_prompt = prompt + padded_system_instruction = system_instruction + + if bypass_mode == "prompt" or bypass_mode == "both": + padded_prompt = self._pad_text_with_underscores(prompt) + if bypass_mode == "system_instruction" or bypass_mode == "both": + padded_system_instruction = self._pad_text_with_underscores(system_instruction) + actual_seed = None if use_seed == True: if seed == 0: @@ -267,12 +282,12 @@ class SSL_GeminiTextPrompt(ComfyNodeABC): img_part = {"inline_data": {"mime_type": "image/png", "data": img_bytes}} img_parts.append(img_part) - contents = img_parts + [{"text": prompt}] + contents = img_parts + [{"text": padded_prompt}] except Exception as e: print(f"[ERROR] Error processing input image: {str(e)}") return (f"Error processing input image: {str(e)}", self.generate_empty_image(), actual_seed if actual_seed is not None else 0) else: - contents = prompt + contents = padded_prompt response_modalities = ["text"] if include_images == True: @@ -303,7 +318,7 @@ class SSL_GeminiTextPrompt(ComfyNodeABC): ], response_modalities=response_modalities, system_instruction=[ - types.Part.from_text(text=system_instruction), + types.Part.from_text(text=padded_system_instruction), ], )