File size: 2,449 Bytes
6332194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""
T5 Prompt Enhancer V0.3 Demo Script
Quick test of all four instruction types
"""

import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration

def load_model():
    """Load the T5 V0.3 model"""
    print("🤖 Loading T5 Prompt Enhancer V0.3...")
    
    tokenizer = T5Tokenizer.from_pretrained(".")
    model = T5ForConditionalGeneration.from_pretrained(".")
    
    if torch.cuda.is_available():
        model = model.cuda()
        print("✅ Model loaded on GPU")
    else:
        print("✅ Model loaded on CPU")
    
    return model, tokenizer

def enhance_prompt(model, tokenizer, text, style="clean"):
    """Generate enhanced prompt with style control"""
    
    style_prompts = {
        "clean": f"Enhance this prompt (no lora): {text}",
        "technical": f"Enhance this prompt (with lora): {text}",
        "simplify": f"Simplify this prompt: {text}",
        "standard": f"Enhance this prompt: {text}"
    }
    
    prompt = style_prompts[style]
    inputs = tokenizer(prompt, return_tensors="pt", max_length=256, truncation=True)
    
    if torch.cuda.is_available():
        inputs = {k: v.cuda() for k, v in inputs.items()}
    
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_length=80,
            num_beams=2,
            repetition_penalty=2.0,
            no_repeat_ngram_size=3,
            pad_token_id=tokenizer.pad_token_id
        )
    
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

def main():
    """Demo all four instruction types"""
    
    # Load model
    model, tokenizer = load_model()
    
    # Test prompts
    test_prompts = [
        "woman in red dress",
        "cat on chair", 
        "cyberpunk cityscape",
        "masterpiece, best quality, ultra-detailed render of a fantasy dragon with golden scales"
    ]
    
    styles = ["standard", "clean", "technical", "simplify"]
    
    print("\n🎨 T5 Prompt Enhancer V0.3 Demo")
    print("="*60)
    
    for prompt in test_prompts:
        print(f"\n📝 Input: '{prompt}'")
        print("-" * 40)
        
        for style in styles:
            try:
                result = enhance_prompt(model, tokenizer, prompt, style)
                print(f"{style:>10}: {result}")
            except Exception as e:
                print(f"{style:>10}: ERROR - {e}")
        
        print()

if __name__ == "__main__":
    main()