Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,10 @@ import gradio as gr
|
|
| 2 |
import random
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# Choose a larger model
|
| 6 |
-
MODEL_NAME = "gpt2-medium"
|
| 7 |
-
MAX_NEW_TOKENS = 150
|
| 8 |
-
|
| 9 |
# Load the model once when the app starts
|
| 10 |
-
generator = pipeline(
|
| 11 |
-
"text-generation",
|
| 12 |
-
model=MODEL_NAME,
|
| 13 |
-
device_map="auto",
|
| 14 |
-
torch_dtype="auto"
|
| 15 |
-
)
|
| 16 |
|
|
|
|
| 17 |
SPECIAL_WORDS = [
|
| 18 |
'movie', 'excited', 'waiting', 'long', 'time', 'production', 'real', 'coded', 'digital', 'favorite',
|
| 19 |
'asking', 'doing', 'basketball', 'soccer', 'football', 'baseball', 'soup', 'food', 'burgers', 'pizza',
|
|
@@ -48,56 +40,67 @@ SPECIAL_WORDS = [
|
|
| 48 |
# Global variables
|
| 49 |
initial_word_design = ""
|
| 50 |
special_word = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
def generate_initial_design(word):
|
| 53 |
"""Generate initial design for the special word in black color."""
|
| 54 |
fonts = [
|
| 55 |
-
"'VT323', monospace",
|
| 56 |
-
"'
|
| 57 |
-
"'
|
| 58 |
-
"'
|
| 59 |
-
"'
|
| 60 |
-
"'Patrick Hand', cursive",
|
| 61 |
-
"'Nothing You Could Do', cursive",
|
| 62 |
-
"'Reenie Beanie', cursive",
|
| 63 |
-
"'Orbitron', sans-serif",
|
| 64 |
-
"'Raleway', sans-serif",
|
| 65 |
-
"'Open Sans Condensed', sans-serif",
|
| 66 |
-
"'Poiret One', cursive",
|
| 67 |
-
"'Indie Flower', cursive",
|
| 68 |
-
"'Pacifico', cursive",
|
| 69 |
-
"'Teko', sans-serif"
|
| 70 |
]
|
| 71 |
-
font_sizes = ["18px", "19px", "20px"]
|
| 72 |
-
font_tops = ["0px", "1px", "-1px"]
|
| 73 |
-
letter_spacings = ["-1px", "0px", "1px"]
|
| 74 |
text_shadows = [
|
| 75 |
-
"0px 0px 1px",
|
| 76 |
-
"0px 0px 2px",
|
| 77 |
-
"1px 0px 0px",
|
| 78 |
-
"0px 0px 0px",
|
| 79 |
-
"0px 1px 0px",
|
| 80 |
-
"0px 2px 0px",
|
| 81 |
-
"0px 1px 1px",
|
| 82 |
-
"1px 1px 0px",
|
| 83 |
-
"1px 0px 1px"
|
| 84 |
]
|
| 85 |
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
|
|
|
| 86 |
|
| 87 |
letters = list(word)
|
| 88 |
styled_letters = []
|
| 89 |
|
| 90 |
for i, letter in enumerate(letters):
|
| 91 |
style = {
|
| 92 |
-
'font-family':
|
| 93 |
-
'line-height': '1.6',
|
| 94 |
-
'font-size':
|
| 95 |
-
'letter-spacing':
|
| 96 |
-
'text-shadow':
|
| 97 |
-
'transform': f'skew({
|
| 98 |
-
'margin-top':
|
| 99 |
'position': 'relative',
|
| 100 |
-
'top':
|
| 101 |
'color': '#000000',
|
| 102 |
'display': 'inline-block',
|
| 103 |
'margin': '0 1px',
|
|
@@ -116,35 +119,24 @@ def generate_initial_design(word):
|
|
| 116 |
{" ".join(styled_letters)}
|
| 117 |
</span>'''
|
| 118 |
|
| 119 |
-
def generate_movement_design(word):
|
| 120 |
-
"""Generate a new random design for the movement animation."""
|
|
|
|
| 121 |
fonts = [
|
| 122 |
-
"'VT323', monospace",
|
| 123 |
-
"'
|
| 124 |
-
"'
|
| 125 |
-
"'
|
| 126 |
-
"'Caveat', cursive",
|
| 127 |
-
"'Patrick Hand', cursive",
|
| 128 |
-
"'Nothing You Could Do', cursive",
|
| 129 |
-
"'Reenie Beanie', cursive",
|
| 130 |
-
"'Orbitron', sans-serif",
|
| 131 |
-
"'Raleway', sans-serif"
|
| 132 |
]
|
| 133 |
-
font_sizes = ["18px", "19px", "20px"]
|
| 134 |
-
font_tops = ["0px", "1px", "-1px"]
|
| 135 |
-
letter_spacings = ["-1px", "0px", "1px"]
|
| 136 |
text_shadows = [
|
| 137 |
-
"0px 0px 1px",
|
| 138 |
-
"0px 0px 2px",
|
| 139 |
-
"1px 0px 0px",
|
| 140 |
-
"0px 0px 0px",
|
| 141 |
-
"0px 1px 0px",
|
| 142 |
-
"0px 2px 0px",
|
| 143 |
-
"0px 1px 1px",
|
| 144 |
-
"1px 1px 0px",
|
| 145 |
-
"1px 0px 1px"
|
| 146 |
]
|
| 147 |
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
|
|
|
| 148 |
|
| 149 |
# Generate random color for the movement design
|
| 150 |
random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
|
@@ -163,18 +155,19 @@ def generate_movement_design(word):
|
|
| 163 |
|
| 164 |
letters = list(word)
|
| 165 |
styled_letters = []
|
|
|
|
| 166 |
|
| 167 |
for i, letter in enumerate(letters):
|
| 168 |
style = {
|
| 169 |
-
'font-family':
|
| 170 |
-
'line-height': '1.6',
|
| 171 |
-
'font-size':
|
| 172 |
-
'letter-spacing':
|
| 173 |
-
'text-shadow':
|
| 174 |
-
'transform': f'skew({
|
| 175 |
-
'margin-top':
|
| 176 |
'position': 'relative',
|
| 177 |
-
'top':
|
| 178 |
'color': random_color,
|
| 179 |
'display': 'inline-block',
|
| 180 |
'margin': '0 1px',
|
|
@@ -182,7 +175,7 @@ def generate_movement_design(word):
|
|
| 182 |
'animation': f'{animation_name} 0.5s ease-in-out',
|
| 183 |
'animation-delay': f'{i * 0.1}s'
|
| 184 |
}
|
| 185 |
-
|
| 186 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
| 187 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
| 188 |
styled_letters.append(styled_letter)
|
|
@@ -203,19 +196,13 @@ def generate_movement_design(word):
|
|
| 203 |
'''
|
| 204 |
|
| 205 |
def process_text(input_text):
|
|
|
|
| 206 |
global initial_word_design, special_word
|
| 207 |
|
| 208 |
-
# Generate text with
|
| 209 |
-
generated = generator(
|
| 210 |
-
input_text,
|
| 211 |
-
max_new_tokens=MAX_NEW_TOKENS,
|
| 212 |
-
do_sample=True,
|
| 213 |
-
top_p=0.9,
|
| 214 |
-
temperature=0.8
|
| 215 |
-
)
|
| 216 |
-
|
| 217 |
generated_text = generated[0]['generated_text']
|
| 218 |
-
generated_text = generated_text[:
|
| 219 |
|
| 220 |
words = generated_text.split()
|
| 221 |
|
|
@@ -223,7 +210,9 @@ def process_text(input_text):
|
|
| 223 |
clean_word = ''.join(filter(str.isalnum, word)).lower()
|
| 224 |
if clean_word in SPECIAL_WORDS:
|
| 225 |
special_word = word
|
| 226 |
-
|
|
|
|
|
|
|
| 227 |
words[i] = initial_word_design
|
| 228 |
else:
|
| 229 |
words[i] = word
|
|
@@ -261,19 +250,41 @@ def process_text(input_text):
|
|
| 261 |
</body>
|
| 262 |
</html>
|
| 263 |
"""
|
| 264 |
-
|
| 265 |
-
return
|
| 266 |
|
| 267 |
def trigger_movement(input_html):
|
|
|
|
| 268 |
global initial_word_design, special_word
|
| 269 |
|
| 270 |
if not initial_word_design or not special_word:
|
| 271 |
return input_html
|
| 272 |
|
| 273 |
-
|
|
|
|
|
|
|
| 274 |
updated_html = input_html.replace(initial_word_design, movement_design, 1)
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
# Create Gradio interface using Blocks
|
| 279 |
with gr.Blocks() as demo:
|
|
@@ -283,16 +294,16 @@ with gr.Blocks() as demo:
|
|
| 283 |
input_text = gr.Textbox(label="Input Prompt")
|
| 284 |
submit_button = gr.Button("Generate")
|
| 285 |
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
| 287 |
animate_button = gr.Button("Trigger Movement")
|
|
|
|
| 288 |
|
| 289 |
-
submit_button.click(
|
| 290 |
-
fn=process_text,
|
| 291 |
-
inputs=input_text,
|
| 292 |
-
outputs=output_html,
|
| 293 |
-
show_progress=True # Enable the loading indicator
|
| 294 |
-
)
|
| 295 |
animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
|
|
|
|
| 296 |
|
| 297 |
# Launch the app
|
| 298 |
demo.launch()
|
|
|
|
| 2 |
import random
|
| 3 |
from transformers import pipeline
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Load the model once when the app starts
|
| 6 |
+
generator = pipeline('text-generation', model='distilgpt2', max_length=25) # Reduced max_length for faster inference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Predefined words to check
|
| 9 |
SPECIAL_WORDS = [
|
| 10 |
'movie', 'excited', 'waiting', 'long', 'time', 'production', 'real', 'coded', 'digital', 'favorite',
|
| 11 |
'asking', 'doing', 'basketball', 'soccer', 'football', 'baseball', 'soup', 'food', 'burgers', 'pizza',
|
|
|
|
| 40 |
# Global variables
|
| 41 |
initial_word_design = ""
|
| 42 |
special_word = ""
|
| 43 |
+
outputs_list = []
|
| 44 |
+
last_design_styles = [] # To store styles of the last movement design for scoring
|
| 45 |
+
|
| 46 |
+
# Store design preferences with weights
|
| 47 |
+
design_preferences = {
|
| 48 |
+
'font-family': {},
|
| 49 |
+
'font-size': {},
|
| 50 |
+
'letter-spacing': {},
|
| 51 |
+
'text-shadow': {},
|
| 52 |
+
'transform': {},
|
| 53 |
+
'margin-top': {},
|
| 54 |
+
'top': {},
|
| 55 |
+
'color': {}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
def get_choice(attribute, choices, use_weights=False):
|
| 59 |
+
"""Helper to get weighted or random choice based on preferences."""
|
| 60 |
+
if use_weights and attribute in design_preferences and design_preferences[attribute]:
|
| 61 |
+
weighted_choices = design_preferences[attribute]
|
| 62 |
+
total = sum(weighted_choices.values())
|
| 63 |
+
rand_val = random.uniform(0, total)
|
| 64 |
+
cum_sum = 0
|
| 65 |
+
for choice, weight in weighted_choices.items():
|
| 66 |
+
cum_sum += weight
|
| 67 |
+
if rand_val < cum_sum:
|
| 68 |
+
return choice
|
| 69 |
+
return random.choice(choices)
|
| 70 |
|
| 71 |
+
def generate_initial_design(word, use_weights=False):
|
| 72 |
"""Generate initial design for the special word in black color."""
|
| 73 |
fonts = [
|
| 74 |
+
"'VT323', monospace", "'Josefin Sans', sans-serif", "'Rajdhani', sans-serif",
|
| 75 |
+
"'Anton', sans-serif", "'Caveat', cursive", "'Patrick Hand', cursive",
|
| 76 |
+
"'Nothing You Could Do', cursive", "'Reenie Beanie', cursive",
|
| 77 |
+
"'Orbitron', sans-serif", "'Raleway', sans-serif", "'Open Sans Condensed', sans-serif",
|
| 78 |
+
"'Poiret One', cursive", "'Indie Flower', cursive", "'Pacifico', cursive", "'Teko', sans-serif"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
]
|
| 80 |
+
font_sizes = ["18px", "19px", "20px"] # Narrower range
|
| 81 |
+
font_tops = ["0px", "1px", "-1px"] # Smaller adjustments
|
| 82 |
+
letter_spacings = ["-1px", "0px", "1px"] # Reduced range
|
| 83 |
text_shadows = [
|
| 84 |
+
"0px 0px 1px", "0px 0px 2px", "1px 0px 0px", "0px 0px 0px",
|
| 85 |
+
"0px 1px 0px", "0px 2px 0px", "0px 1px 1px", "1px 1px 0px", "1px 0px 1px"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
]
|
| 87 |
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
| 88 |
+
margins_top = ["-0.02cm", "0.00cm", "0.02cm"]
|
| 89 |
|
| 90 |
letters = list(word)
|
| 91 |
styled_letters = []
|
| 92 |
|
| 93 |
for i, letter in enumerate(letters):
|
| 94 |
style = {
|
| 95 |
+
'font-family': get_choice('font-family', fonts, use_weights),
|
| 96 |
+
'line-height': '1.6', # Consistent with body text
|
| 97 |
+
'font-size': get_choice('font-size', font_sizes, use_weights),
|
| 98 |
+
'letter-spacing': get_choice('letter-spacing', letter_spacings, use_weights),
|
| 99 |
+
'text-shadow': get_choice('text-shadow', text_shadows, use_weights),
|
| 100 |
+
'transform': f'skew({get_choice("transform", skew_angles, use_weights)})',
|
| 101 |
+
'margin-top': get_choice('margin-top', margins_top, use_weights),
|
| 102 |
'position': 'relative',
|
| 103 |
+
'top': get_choice('top', font_tops, use_weights),
|
| 104 |
'color': '#000000',
|
| 105 |
'display': 'inline-block',
|
| 106 |
'margin': '0 1px',
|
|
|
|
| 119 |
{" ".join(styled_letters)}
|
| 120 |
</span>'''
|
| 121 |
|
| 122 |
+
def generate_movement_design(word, use_weights=False):
|
| 123 |
+
"""Generate a completely new random design for the movement animation."""
|
| 124 |
+
global last_design_styles
|
| 125 |
fonts = [
|
| 126 |
+
"'VT323', monospace", "'Josefin Sans', sans-serif", "'Rajdhani', sans-serif",
|
| 127 |
+
"'Anton', sans-serif", "'Caveat', cursive", "'Patrick Hand', cursive",
|
| 128 |
+
"'Nothing You Could Do', cursive", "'Reenie Beanie', cursive",
|
| 129 |
+
"'Orbitron', sans-serif", "'Raleway', sans-serif"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
]
|
| 131 |
+
font_sizes = ["18px", "19px", "20px"] # Narrower range
|
| 132 |
+
font_tops = ["0px", "1px", "-1px"] # Smaller adjustments
|
| 133 |
+
letter_spacings = ["-1px", "0px", "1px"] # Reduced range
|
| 134 |
text_shadows = [
|
| 135 |
+
"0px 0px 1px", "0px 0px 2px", "1px 0px 0px", "0px 0px 0px",
|
| 136 |
+
"0px 1px 0px", "0px 2px 0px", "0px 1px 1px", "1px 1px 0px", "1px 0px 1px"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
]
|
| 138 |
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
| 139 |
+
margins_top = ["-0.02cm", "0.00cm", "0.02cm"]
|
| 140 |
|
| 141 |
# Generate random color for the movement design
|
| 142 |
random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
|
|
|
| 155 |
|
| 156 |
letters = list(word)
|
| 157 |
styled_letters = []
|
| 158 |
+
last_design_styles = [] # Reset the last design styles for new movement design
|
| 159 |
|
| 160 |
for i, letter in enumerate(letters):
|
| 161 |
style = {
|
| 162 |
+
'font-family': get_choice('font-family', fonts, use_weights),
|
| 163 |
+
'line-height': '1.6', # Consistent with body text
|
| 164 |
+
'font-size': get_choice('font-size', font_sizes, use_weights),
|
| 165 |
+
'letter-spacing': get_choice('letter-spacing', letter_spacings, use_weights),
|
| 166 |
+
'text-shadow': get_choice('text-shadow', text_shadows, use_weights),
|
| 167 |
+
'transform': f'skew({get_choice("transform", skew_angles, use_weights)})',
|
| 168 |
+
'margin-top': get_choice('margin-top', margins_top, use_weights),
|
| 169 |
'position': 'relative',
|
| 170 |
+
'top': get_choice('top', font_tops, use_weights),
|
| 171 |
'color': random_color,
|
| 172 |
'display': 'inline-block',
|
| 173 |
'margin': '0 1px',
|
|
|
|
| 175 |
'animation': f'{animation_name} 0.5s ease-in-out',
|
| 176 |
'animation-delay': f'{i * 0.1}s'
|
| 177 |
}
|
| 178 |
+
last_design_styles.append(style) # Save the style for scoring
|
| 179 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
| 180 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
| 181 |
styled_letters.append(styled_letter)
|
|
|
|
| 196 |
'''
|
| 197 |
|
| 198 |
def process_text(input_text):
|
| 199 |
+
"""Process text and generate the initial output with special word styled in black."""
|
| 200 |
global initial_word_design, special_word
|
| 201 |
|
| 202 |
+
# Generate text with limited length
|
| 203 |
+
generated = generator(input_text, num_return_sequences=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
generated_text = generated[0]['generated_text']
|
| 205 |
+
generated_text = generated_text[:200] # Limit output length
|
| 206 |
|
| 207 |
words = generated_text.split()
|
| 208 |
|
|
|
|
| 210 |
clean_word = ''.join(filter(str.isalnum, word)).lower()
|
| 211 |
if clean_word in SPECIAL_WORDS:
|
| 212 |
special_word = word
|
| 213 |
+
# Use weights only for subsequent generations after scoring
|
| 214 |
+
use_weights = len(outputs_list) > 0 and any(design_preferences[attr] for attr in design_preferences)
|
| 215 |
+
initial_word_design = generate_initial_design(word, use_weights=use_weights)
|
| 216 |
words[i] = initial_word_design
|
| 217 |
else:
|
| 218 |
words[i] = word
|
|
|
|
| 250 |
</body>
|
| 251 |
</html>
|
| 252 |
"""
|
| 253 |
+
outputs_list.append(final_output)
|
| 254 |
+
return "<br><br>".join(outputs_list)
|
| 255 |
|
| 256 |
def trigger_movement(input_html):
|
| 257 |
+
"""Function to trigger the movement animation."""
|
| 258 |
global initial_word_design, special_word
|
| 259 |
|
| 260 |
if not initial_word_design or not special_word:
|
| 261 |
return input_html
|
| 262 |
|
| 263 |
+
# Use weights only for subsequent generations after scoring
|
| 264 |
+
use_weights = len(outputs_list) > 0 and any(design_preferences[attr] for attr in design_preferences)
|
| 265 |
+
movement_design = generate_movement_design(special_word, use_weights=use_weights)
|
| 266 |
updated_html = input_html.replace(initial_word_design, movement_design, 1)
|
| 267 |
+
outputs_list[-1] = updated_html # Update the last output with movement design
|
| 268 |
+
return "<br><br>".join(outputs_list)
|
| 269 |
|
| 270 |
+
def update_design_preferences(score):
|
| 271 |
+
"""Update design preferences based on the score for the last movement design."""
|
| 272 |
+
global last_design_styles
|
| 273 |
+
|
| 274 |
+
if last_design_styles:
|
| 275 |
+
weight = score if score > 5 else (11 - score) * 0.5 # Higher score means more weight, lower score reduces weight
|
| 276 |
+
for style in last_design_styles:
|
| 277 |
+
for attr, value in style.items():
|
| 278 |
+
if attr in design_preferences:
|
| 279 |
+
if attr == 'transform': # Extract the skew angle from transform
|
| 280 |
+
value = value.split('(')[1].split(')')[0]
|
| 281 |
+
if attr == 'color': # We might not weight color since it’s random each time
|
| 282 |
+
continue
|
| 283 |
+
if value in design_preferences[attr]:
|
| 284 |
+
design_preferences[attr][value] += weight
|
| 285 |
+
else:
|
| 286 |
+
design_preferences[attr][value] = weight
|
| 287 |
+
return "<br><br>".join(outputs_list)
|
| 288 |
|
| 289 |
# Create Gradio interface using Blocks
|
| 290 |
with gr.Blocks() as demo:
|
|
|
|
| 294 |
input_text = gr.Textbox(label="Input Prompt")
|
| 295 |
submit_button = gr.Button("Generate")
|
| 296 |
|
| 297 |
+
with gr.Row():
|
| 298 |
+
score_slider = gr.Slider(minimum=1, maximum=10, step=1, label="Score the Movement Design")
|
| 299 |
+
score_button = gr.Button("Submit Score")
|
| 300 |
+
|
| 301 |
animate_button = gr.Button("Trigger Movement")
|
| 302 |
+
output_html = gr.HTML()
|
| 303 |
|
| 304 |
+
submit_button.click(process_text, inputs=input_text, outputs=output_html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
|
| 306 |
+
score_button.click(update_design_preferences, inputs=score_slider, outputs=output_html)
|
| 307 |
|
| 308 |
# Launch the app
|
| 309 |
demo.launch()
|