File size: 6,876 Bytes
48edc07
 
 
bce946a
48edc07
 
 
bce946a
 
 
 
6212336
 
 
bce946a
48edc07
bce946a
 
48edc07
bce946a
 
48edc07
 
 
bce946a
 
48edc07
 
bce946a
48edc07
bce946a
6212336
48edc07
bce946a
48edc07
 
 
 
 
 
bce946a
48edc07
bce946a
48edc07
 
 
 
bce946a
48edc07
 
 
 
 
 
bce946a
 
 
48edc07
 
 
 
 
 
 
 
 
 
bce946a
48edc07
 
 
 
 
bce946a
48edc07
 
 
 
6212336
 
 
48edc07
 
 
6212336
 
48edc07
 
 
 
 
 
bce946a
48edc07
bce946a
48edc07
bce946a
48edc07
 
 
 
bce946a
48edc07
 
bce946a
48edc07
 
 
 
 
 
 
bce946a
48edc07
 
 
 
 
bce946a
48edc07
 
6212336
48edc07
6212336
48edc07
 
 
bce946a
48edc07
 
 
6212336
 
48edc07
 
bce946a
48edc07
bce946a
48edc07
 
 
5d48bab
48edc07
bce946a
48edc07
 
bce946a
48edc07
 
 
 
 
 
 
 
bce946a
48edc07
 
 
 
 
 
 
 
bce946a
48edc07
 
bce946a
48edc07
bce946a
48edc07
bce946a
48edc07
 
 
 
bce946a
48edc07
 
bce946a
48edc07
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import spaces

MODEL_NAME = "retowyss/PromptBridge-0.6b-Alpha"

# Detect device
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
DTYPE = torch.bfloat16 if DEVICE == "cuda" else torch.float32

DEFAULT_TEMPERATURE = 0.2
DEFAULT_MAX_TOKENS = 512

print(f"Using device: {DEVICE}")
print("Loading model...")

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)

# Load model with appropriate settings
model = AutoModelForCausalLM.from_pretrained(
    MODEL_NAME,
    trust_remote_code=True,
    torch_dtype=DTYPE,
    device_map="auto" if DEVICE == "cuda" else "cpu"
)
model.eval()
print(f"Model loaded on {DEVICE}!")

@spaces.GPU(duration=60)  # Allocate GPU for 60 seconds
def generate_prompt(mode: str, user_prompt: str, temperature: float = DEFAULT_TEMPERATURE, max_tokens: int = DEFAULT_MAX_TOKENS):
    """Generate prompt transformation."""

    # Map mode to system prompt
    system_prompts = {
        "Expand": "Expand the prompt.",
        "Compress to Sentence": "Compress the prompt into one sentence.",
        "Compress to Keywords": "Compress the prompt into keyword format."
    }

    system_prompt = system_prompts[mode]

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_prompt}
    ]

    # Apply chat template
    text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )

    inputs = tokenizer(text, return_tensors="pt").to(model.device)

    # Generate
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=max_tokens,
            do_sample=True,
            temperature=temperature,
            pad_token_id=tokenizer.pad_token_id,
            eos_token_id=tokenizer.eos_token_id,
        )

    # Decode only the new tokens
    response = tokenizer.decode(
        outputs[0][inputs['input_ids'].shape[1]:],
        skip_special_tokens=True
    )

    return response.strip()

# Example prompts
expansion_examples = [
    ["Expand", "woman, flowing red dress, standing, sunset beach", DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS],
    ["Expand", "man, leather jacket, motorcycle, urban alley", DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS],
    ["Expand", "dancer, ballet pose, stage lighting", DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS],
]

compression_examples = [
    ["Compress to Sentence", "A young woman with shoulder-length wavy auburn hair stands in a sunlit garden, her emerald green sundress flowing gently in the warm breeze. She wears delicate gold hoop earrings and a thin matching bracelet on her left wrist. Her bare feet rest on soft grass dotted with small white daisies. Behind her, climbing roses in shades of pink and coral wind up a weathered wooden trellis. The late afternoon sunlight filters through the leaves, casting dappled shadows across her face and creating a warm, golden glow that highlights her peaceful smile.", DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS],
    ["Compress to Keywords", "A young woman with shoulder-length wavy auburn hair stands in a sunlit garden, her emerald green sundress flowing gently in the warm breeze. She wears delicate gold hoop earrings and a thin matching bracelet on her left wrist. Her bare feet rest on soft grass dotted with small white daisies. Behind her, climbing roses in shades of pink and coral wind up a weathered wooden trellis. The late afternoon sunlight filters through the leaves, casting dappled shadows across her face and creating a warm, golden glow that highlights her peaceful smile.", DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS],
]

# Create Gradio interface
with gr.Blocks(title="PromptBridge-0.6b-Alpha") as demo:
    gr.Markdown("""
    # 🌉 PromptBridge-0.6b-Alpha

    A specialized model for bidirectional prompt transformation for text-to-image generation.

    **Trained exclusively on prompts featuring single, adult humanoid subjects.**

    ### Modes:
    - **Expand**: Convert brief keywords into detailed image generation prompts
    - **Compress to Sentence**: Condense detailed prompts into a single flowing sentence
    - **Compress to Keywords**: Convert prompts into comma-separated keywords

    ⚠️ **Note**: May generate sensitive content (PG to R-rated). You are responsible for the output.
    """)

    with gr.Row():
        with gr.Column():
            mode = gr.Radio(
                choices=["Expand", "Compress to Sentence", "Compress to Keywords"],
                value="Expand",
                label="Mode"
            )

            user_input = gr.Textbox(
                lines=5,
                placeholder="Enter your prompt here...",
                label="Input Prompt"
            )

            with gr.Row():
                temperature = gr.Slider(
                    minimum=0.0,
                    maximum=1.5,
                    value=DEFAULT_TEMPERATURE,
                    step=0.1,
                    label="Temperature"
                )

                max_tokens = gr.Slider(
                    minimum=64,
                    maximum=1024,
                    value=DEFAULT_MAX_TOKENS,
                    step=32,
                    label="Max Tokens"
                )

            submit_btn = gr.Button("Generate", variant="primary")

        with gr.Column():
            output = gr.Textbox(
                lines=15,
                label="Output"
            )

    # Examples
    gr.Markdown("### Examples")

    with gr.Tab("Expansion Examples"):
        gr.Examples(
            examples=expansion_examples,
            inputs=[mode, user_input, temperature, max_tokens],
            outputs=output,
            fn=generate_prompt,
            cache_examples=False
        )

    with gr.Tab("Compression Examples"):
        gr.Examples(
            examples=compression_examples,
            inputs=[mode, user_input, temperature, max_tokens],
            outputs=output,
            fn=generate_prompt,
            cache_examples=False
        )

    gr.Markdown("""
    ### About

    **Model**: [retowyss/PromptBridge-0.6b-Alpha](https://huggingface.co/retowyss/PromptBridge-0.6b-Alpha)

    **Training Data**: ~300K synthetic prompt pairs (PG to R-rated, X-rated content removed)

    **Limitations**:
    - Optimized for human subjects only (performance on other subjects untested)
    - May generate prompts exceeding typical image model token limits
    - Cannot perform general instruction-following or reasoning tasks

    **License**: Apache 2.0
    """)

    # Connect the button
    submit_btn.click(
        fn=generate_prompt,
        inputs=[mode, user_input, temperature, max_tokens],
        outputs=output
    )

if __name__ == "__main__":
    demo.launch()