Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -89,6 +89,7 @@ def generate_initial_design(word, use_weights=False):
|
|
89 |
|
90 |
letters = list(word)
|
91 |
styled_letters = []
|
|
|
92 |
|
93 |
for i, letter in enumerate(letters):
|
94 |
style = {
|
@@ -106,7 +107,7 @@ def generate_initial_design(word, use_weights=False):
|
|
106 |
'margin': '0 1px',
|
107 |
'vertical-align': 'middle'
|
108 |
}
|
109 |
-
|
110 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
111 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
112 |
styled_letters.append(styled_letter)
|
@@ -117,7 +118,7 @@ def generate_initial_design(word, use_weights=False):
|
|
117 |
vertical-align: middle;
|
118 |
margin: 0 2px;">
|
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."""
|
@@ -192,12 +193,11 @@ def generate_movement_design(word, use_weights=False):
|
|
192 |
vertical-align: middle;
|
193 |
margin: 0 2px;">
|
194 |
{" ".join(styled_letters)}
|
195 |
-
</span>
|
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)
|
@@ -210,9 +210,9 @@ def process_text(input_text):
|
|
210 |
clean_word = ''.join(filter(str.isalnum, word)).lower()
|
211 |
if clean_word in SPECIAL_WORDS:
|
212 |
special_word = word
|
213 |
-
# Use weights only
|
214 |
-
use_weights =
|
215 |
-
initial_word_design = generate_initial_design(word, use_weights=use_weights)
|
216 |
words[i] = initial_word_design
|
217 |
else:
|
218 |
words[i] = word
|
@@ -254,21 +254,25 @@ def process_text(input_text):
|
|
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
|
262 |
|
263 |
-
# Use weights only
|
264 |
-
use_weights =
|
265 |
-
movement_design = generate_movement_design(special_word, use_weights=use_weights)
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
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
|
272 |
global last_design_styles
|
273 |
|
274 |
if last_design_styles:
|
@@ -278,7 +282,7 @@ def update_design_preferences(score):
|
|
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
|
@@ -295,7 +299,7 @@ with gr.Blocks() as demo:
|
|
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
|
299 |
score_button = gr.Button("Submit Score")
|
300 |
|
301 |
animate_button = gr.Button("Trigger Movement")
|
|
|
89 |
|
90 |
letters = list(word)
|
91 |
styled_letters = []
|
92 |
+
current_design_styles = []
|
93 |
|
94 |
for i, letter in enumerate(letters):
|
95 |
style = {
|
|
|
107 |
'margin': '0 1px',
|
108 |
'vertical-align': 'middle'
|
109 |
}
|
110 |
+
current_design_styles.append(style)
|
111 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
112 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
113 |
styled_letters.append(styled_letter)
|
|
|
118 |
vertical-align: middle;
|
119 |
margin: 0 2px;">
|
120 |
{" ".join(styled_letters)}
|
121 |
+
</span>''', current_design_styles
|
122 |
|
123 |
def generate_movement_design(word, use_weights=False):
|
124 |
"""Generate a completely new random design for the movement animation."""
|
|
|
193 |
vertical-align: middle;
|
194 |
margin: 0 2px;">
|
195 |
{" ".join(styled_letters)}
|
196 |
+
</span>''', last_design_styles
|
|
|
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, last_design_styles
|
201 |
|
202 |
# Generate text with limited length
|
203 |
generated = generator(input_text, num_return_sequences=1)
|
|
|
210 |
clean_word = ''.join(filter(str.isalnum, word)).lower()
|
211 |
if clean_word in SPECIAL_WORDS:
|
212 |
special_word = word
|
213 |
+
# Use weights only if there are existing preferences
|
214 |
+
use_weights = any(design_preferences[attr] for attr in design_preferences)
|
215 |
+
initial_word_design, last_design_styles = generate_initial_design(word, use_weights=use_weights)
|
216 |
words[i] = initial_word_design
|
217 |
else:
|
218 |
words[i] = word
|
|
|
254 |
return "<br><br>".join(outputs_list)
|
255 |
|
256 |
def trigger_movement(input_html):
|
257 |
+
"""Function to trigger the movement animation without duplicating content."""
|
258 |
+
global initial_word_design, special_word, last_design_styles
|
259 |
|
260 |
+
if not initial_word_design or not special_word or not outputs_list:
|
261 |
+
return "<br><br>".join(outputs_list)
|
262 |
|
263 |
+
# Use weights only if there are existing preferences
|
264 |
+
use_weights = any(design_preferences[attr] for attr in design_preferences)
|
265 |
+
movement_design, last_design_styles = generate_movement_design(special_word, use_weights=use_weights)
|
266 |
+
|
267 |
+
# Update only the last output in the list with the movement design
|
268 |
+
last_output = outputs_list[-1]
|
269 |
+
updated_last_output = last_output.replace(initial_word_design, movement_design, 1)
|
270 |
+
outputs_list[-1] = updated_last_output # Update the last output without appending a new one
|
271 |
+
|
272 |
return "<br><br>".join(outputs_list)
|
273 |
|
274 |
def update_design_preferences(score):
|
275 |
+
"""Update design preferences based on the score for the last design (initial or movement)."""
|
276 |
global last_design_styles
|
277 |
|
278 |
if last_design_styles:
|
|
|
282 |
if attr in design_preferences:
|
283 |
if attr == 'transform': # Extract the skew angle from transform
|
284 |
value = value.split('(')[1].split(')')[0]
|
285 |
+
if attr == 'color': # We might not weight color since it’s random each time for movement
|
286 |
continue
|
287 |
if value in design_preferences[attr]:
|
288 |
design_preferences[attr][value] += weight
|
|
|
299 |
submit_button = gr.Button("Generate")
|
300 |
|
301 |
with gr.Row():
|
302 |
+
score_slider = gr.Slider(minimum=1, maximum=10, step=1, label="Score the Current Design")
|
303 |
score_button = gr.Button("Submit Score")
|
304 |
|
305 |
animate_button = gr.Button("Trigger Movement")
|