Views
No views yet
1from transformers import T5Tokenizer, T5ForConditionalGeneration
2import torch
3
4# Load model
5model = T5ForConditionalGeneration.from_pretrained("t5-prompt-enhancer-v03")
6tokenizer = T5Tokenizer.from_pretrained("t5-prompt-enhancer-v03")
7
8def enhance_prompt(text, style="clean"):
9 """Enhanced prompt generation with style control"""
10
11 if style == "clean":
12 prompt = f"Enhance this prompt (no lora): {text}"
13 elif style == "technical":
14 prompt = f"Enhance this prompt (with lora): {text}"
15 elif style == "simplify":
16 prompt = f"Simplify this prompt: {text}"
17 else:
18 prompt = f"Enhance this prompt: {text}"
19
20 inputs = tokenizer(prompt, return_tensors="pt", max_length=256, truncation=True)
21
22 with torch.no_grad():
23 outputs = model.generate(
24 inputs.input_ids,
25 max_length=80,
26 num_beams=2,
27 repetition_penalty=2.0,
28 no_repeat_ngram_size=3
29 )
30
31 return tokenizer.decode(outputs[0], skip_special_tokens=True)
32
33# Examples
34print(enhance_prompt("woman in red dress", "clean"))
35# Output: "a beautiful woman in a red dress with flowing hair, elegant pose, soft lighting"
36
37print(enhance_prompt("anime girl", "technical"))
38# Output: "masterpiece, best quality, 1girl, solo, anime style, detailed background"
39
40print(enhance_prompt("A majestic dragon with golden scales soaring through stormy clouds", "simplify"))
41# Output: "dragon flying through clouds"| Input | Output Style | Result |
|---|---|---|
| "woman in red dress" | Clean | "a beautiful woman in a red dress with flowing hair, elegant pose, soft lighting" |
| "woman in red dress" | Technical | "masterpiece, best quality, 1girl, solo, red dress, detailed background, high resolution" |
| "Complex Victorian description..." | Simplify | "woman in red dress in ballroom" |
| "cat" | Standard | "cat sitting peacefully, photorealistic, detailed fur texture" |
1# Four supported instruction types:
2"Enhance this prompt: {basic_prompt}" # Balanced enhancement
3"Enhance this prompt (no lora): {basic_prompt}" # Clean, artifact-free
4"Enhance this prompt (with lora): {basic_prompt}" # Technical with LoRA tags
5"Simplify this prompt: {complex_prompt}" # Complexity reduction1# Simplify complex prompts for broader audiences
2enhance_prompt("masterpiece, ultra-detailed render of cyberpunk scene...", "simplify")
3# → "cyberpunk city street at night"1# Clean enhancement for professional work
2enhance_prompt("sunset landscape", "clean")
3# → "breathtaking sunset over rolling hills with golden light and dramatic clouds"
4
5# Technical enhancement for specific workflows
6enhance_prompt("anime character", "technical")
7# → "masterpiece, best quality, 1girl, solo, anime style, detailed background"1# Bidirectional optimization
2basic = "cat on chair"
3enhanced = enhance_prompt(basic, "clean")
4simplified = enhance_prompt(enhanced, "simplify")
5# Optimize prompt complexity iteratively1def generate_with_control(text, style="clean", creativity=0.7):
2 """Advanced generation with creativity control"""
3
4 style_prompts = {
5 "clean": f"Enhance this prompt (no lora): {text}",
6 "technical": f"Enhance this prompt (with lora): {text}",
7 "simplify": f"Simplify this prompt: {text}",
8 "standard": f"Enhance this prompt: {text}"
9 }
10
11 inputs = tokenizer(style_prompts[style], return_tensors="pt")
12
13 if creativity > 0.5:
14 # Creative mode
15 outputs = model.generate(
16 inputs.input_ids,
17 max_length=100,
18 do_sample=True,
19 temperature=creativity,
20 top_p=0.9,
21 repetition_penalty=1.5
22 )
23 else:
24 # Deterministic mode
25 outputs = model.generate(
26 inputs.input_ids,
27 max_length=80,
28 num_beams=2,
29 repetition_penalty=2.0,
30 no_repeat_ngram_size=3
31 )
32
33 return tokenizer.decode(outputs[0], skip_special_tokens=True)1def batch_enhance(prompts, style="clean"):
2 """Process multiple prompts efficiently"""
3
4 prefixed_prompts = [f"Enhance this prompt ({style}): {prompt}" if style in ["no lora", "with lora"]
5 else f"Enhance this prompt: {prompt}" for prompt in prompts]
6
7 inputs = tokenizer(prefixed_prompts, return_tensors="pt", padding=True, truncation=True)
8
9 outputs = model.generate(
10 inputs.input_ids,
11 max_length=80,
12 num_beams=2,
13 repetition_penalty=2.0,
14 pad_token_id=tokenizer.pad_token_id
15 )
16
17 return [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]| Feature | V0.1 | V0.2 | V0.3 |
|---|---|---|---|
| Training Data | 48K | 174K | 297K |
| Instructions | Enhancement only | Simplify + Enhance | Quad-instruction |
| LoRA Handling | Contaminated | Contaminated | Controlled |
| Artifact Control | None | None | Explicit |
| Platform Coverage | Limited | Good | Comprehensive |
| User Control | Basic | Moderate | Complete |
<simplify>, <enhance>, <no_lora>, <with_lora>1generation_config = {
2 "max_length": 80,
3 "num_beams": 2,
4 "repetition_penalty": 2.0,
5 "no_repeat_ngram_size": 3
6}1creative_config = {
2 "max_length": 100,
3 "do_sample": True,
4 "temperature": 0.7,
5 "top_p": 0.9,
6 "repetition_penalty": 1.3
7}1# Start with basic idea
2idea = "fantasy castle"
3
4# Create clean version for general audience
5clean_version = enhance_prompt(idea, "clean")
6# → "A majestic fantasy castle with towering spires and magical aura"
7
8# Create detailed version for AI art generation
9detailed_version = enhance_prompt(idea, "technical")
10# → "masterpiece, fantasy castle, detailed architecture, magical atmosphere, high quality"1# Iterative refinement
2original = "A complex, detailed description of a beautiful woman..."
3simplified = enhance_prompt(original, "simplify")
4# → "beautiful woman portrait"
5
6refined = enhance_prompt(simplified, "clean")
7# → "elegant woman portrait with soft lighting and natural beauty"1@model{t5_prompt_enhancer_v03,
2 title={T5 Prompt Enhancer V0.3: Quad-Instruction AI Art Prompt Enhancement},
3 author={AI Art Prompt Enhancement Project},
4 year={2025},
5 url={https://huggingface.co/t5-prompt-enhancer-v03},
6 note={T5-base model fine-tuned for quad-instruction AI art prompt enhancement with LoRA control},
7 training_data={297K samples from 6 AI art platforms},
8 capabilities={simplification, enhancement, lora_control, artifact_cleaning}
9}text2text-generation prompt-enhancement ai-art stable-diffusion midjourney dall-e prompt-engineering lora-control bidirectional artifact-cleaning