PromptEnhancer: Rewrite Any Prompt for Stunning AI Images

9月13日 Published inPrompt Engineering Tools

PromptEnhancer is a prompt-refinement utility specifically designed for Tencent's Hunyuan models. It applies chain-of-thought reasoning to organize and clarify user inputs, creating more effective instructions for text-to-image models. Rather than adding superficial "fluff," it maintains the original subject, action, count, and style, but reorganizes them into a "global-detail-summary" structure that better aligns with the model's processing logic.

Core Element Retention The rewriting process ensures that essential elements—subject, action, quantity, style, and spatial relationships—remain unchanged. The tool strictly respects the user's original intent while optimizing the phrasing for the model.

Structured Framework Instead of processing prompts as a simple list of keywords, the tool creates a logical flow. It prioritizes the primary subject and action, layers in background details or secondary elements, and concludes with style notes. This structured approach minimizes ambiguity for image generators.

Robust Output Parsing The system is designed to handle varied output formats without failing. It primarily looks for content within <|FunctionCallEnd|> markers. If these are missing or improperly formatted, the parser strips away extraneous symbols to extract the remaining text. If no valid content is found, it falls back to the original user input to ensure a result is always returned.

Customizable Inference Users can fine-tune the output by adjusting parameters like temperature and top_p for varied results. Setting temperature to 0 ensures deterministic, consistent rewrites, while max_new_tokens allows for precise control over the output length.

Installation

Install the necessary dependencies via pip:

pip install -r requirements.txt

Model Download

Use the Hugging Face CLI to download the model to your local machine:

huggingface-cli download tencent/HunyuanImage-2.1/reprompt --local-dir ./models/

Quick Start

The following example demonstrates how to refine a prompt. The tool supports both English and Chinese inputs.

from inference.prompt_enhancer import HunyuanPromptEnhancer

# 1) Define the local path or repo ID
models_root_path = "./models/"

# 2) Initialize the enhancer (device_map adapts to your hardware)
enhancer = HunyuanPromptEnhancer(models_root_path=models_root_path, device_map="auto")

# 3) Improve the input prompt
user_prompt = "第三人称视角,一辆赛车在城市赛道上飞驰..."  # "Third-person view, a race car speeding through a city circuit..."

new_prompt = enhancer.predict(
    prompt_cot=user_prompt,
    # The default system prompt is pre-configured for image prompt rewriting.
    temperature=0.7,   # > 0 enables sampling; 0 for deterministic generation
    top_p=0.9,
    max_new_tokens=256,
)

print("Optimized prompt:", new_prompt)

Key Parameters

Parameter Description
models_root_path Local directory or Hugging Face repo ID. Supports models requiring trust_remote_code.
device_map Strategy for device placement. "auto" adapts to available hardware.
prompt_cot (in predict) The raw input string to be rewritten.
sys_prompt (in predict) Optional. Overrides the default system prompt tailored for image generation.
temperature (in predict) Controls randomness. 0 for deterministic results; higher values enable variety.
top_p (in predict) Nucleus sampling threshold; active only when sampling mode is enabled.
max_new_tokens (in predict) Caps the number of newly generated tokens in the output.