From 0bd81a48c39e0523ed25c364f2cb2e6a3b79cd79 Mon Sep 17 00:00:00 2001 From: Vito Sansevero Date: Fri, 1 Aug 2025 13:35:23 -0700 Subject: [PATCH] fix: improve SDXL negative prompt parsing - Make parsing more robust to handle multi-line prompts - Add fallback negative prompt if not provided - Update SDXL template to be more explicit about format - Add example to ensure Gemini returns both prompts --- kikotools/tools/gemini_prompt/node.py | 41 ++++++++++++++++++++---- kikotools/tools/gemini_prompt/prompts.py | 12 +++++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/kikotools/tools/gemini_prompt/node.py b/kikotools/tools/gemini_prompt/node.py index 98ffc22..4c5085a 100644 --- a/kikotools/tools/gemini_prompt/node.py +++ b/kikotools/tools/gemini_prompt/node.py @@ -118,15 +118,44 @@ Install: pip install google-generativeai positive_prompt = "" negative_prompt = "" - for line in lines: - if line.startswith("Positive:"): - positive_prompt = line.replace("Positive:", "").strip() - elif line.startswith("Negative:"): - negative_prompt = line.replace("Negative:", "").strip() + # Look for positive prompt + for i, line in enumerate(lines): + if line.strip().startswith("Positive:"): + # Extract everything after "Positive:" + positive_prompt = line.split("Positive:", 1)[1].strip() + # Check if it continues on next lines (without "Negative:") + j = i + 1 + while j < len(lines) and not lines[j].strip().startswith( + "Negative:" + ): + if lines[j].strip(): + positive_prompt += " " + lines[j].strip() + j += 1 + break + + # Look for negative prompt + for i, line in enumerate(lines): + if line.strip().startswith("Negative:"): + # Extract everything after "Negative:" + negative_prompt = line.split("Negative:", 1)[1].strip() + # Check if it continues on next lines + j = i + 1 + while ( + j < len(lines) + and lines[j].strip() + and not lines[j].strip().startswith("Positive:") + ): + negative_prompt += " " + lines[j].strip() + j += 1 + break # If format not found, assume entire response is positive prompt - if not positive_prompt: + if not positive_prompt and not negative_prompt: positive_prompt = prompt + negative_prompt = "" + elif not negative_prompt: + # If we found positive but no negative, provide a basic negative + negative_prompt = "ugly, blurry, low quality, distorted, deformed" return (positive_prompt, negative_prompt) diff --git a/kikotools/tools/gemini_prompt/prompts.py b/kikotools/tools/gemini_prompt/prompts.py index e2f21be..72a4712 100644 --- a/kikotools/tools/gemini_prompt/prompts.py +++ b/kikotools/tools/gemini_prompt/prompts.py @@ -24,7 +24,7 @@ SDXL_PROMPT = """You are an expert SDXL prompt engineer. Analyze the image and g SDXL works best with natural language descriptions but also supports comma-separated keywords. Keep prompts concise but descriptive. -Return your response in EXACTLY this format: +You MUST return your response in EXACTLY this format (two lines only): Positive: [your positive prompt here] Negative: [your negative prompt here] @@ -37,12 +37,18 @@ Guidelines for Positive prompt: - Keep it clear and specific but not overly long Guidelines for Negative prompt: -- Keep it simple and minimal +- Always include a negative prompt, even if minimal - Common negatives: ugly, blurry, low quality, distorted, deformed - Only add specifics you want to avoid (e.g., "cartoon" for photorealistic) - Don't overload with negative prompts - SDXL needs fewer than SD1.5 -IMPORTANT: Return ONLY the two lines starting with "Positive:" and "Negative:". No other text.""" +CRITICAL: Your response must be EXACTLY two lines: +Line 1: Positive: [prompt] +Line 2: Negative: [prompt] + +Example response: +Positive: photograph of a young woman with flowing red hair, professional portrait, soft lighting, bokeh background, highly detailed, 8K +Negative: ugly, blurry, low quality, distorted features, bad anatomy""" DANBOORU_PROMPT = """You are a Danbooru tagging expert specializing in anime-style image tagging. Analyze the image and generate ONLY Danbooru-style tags - no explanations or analysis.