Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,10 @@ generator = pipeline('text-generation', model='distilgpt2') # Lightweight model
|
|
8 |
# Predefined words to check
|
9 |
SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat']
|
10 |
|
|
|
|
|
|
|
|
|
11 |
# Function to generate the initial design (black color)
|
12 |
def generate_initial_design(word):
|
13 |
"""Generate initial design for the special word in black color."""
|
@@ -34,7 +38,7 @@ def generate_initial_design(word):
|
|
34 |
"'Annie Use Your Telescope', cursive"
|
35 |
]
|
36 |
font_sizes = ["17px", "19px", "21px", "23px", "25px", "27px"]
|
37 |
-
font_tops = ["11px", "
|
38 |
letter_spacings = ["-6px", "-4px", "-3px", "-2px", "-1px", "0px", "1px", "2px"]
|
39 |
text_shadows = [
|
40 |
"0px 0px 1px",
|
@@ -48,7 +52,7 @@ def generate_initial_design(word):
|
|
48 |
"1px 0px 1px"
|
49 |
]
|
50 |
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
51 |
-
|
52 |
# Create HTML for each letter with random styling (black color)
|
53 |
letters = list(word)
|
54 |
styled_letters = []
|
@@ -67,28 +71,30 @@ def generate_initial_design(word):
|
|
67 |
'display': 'inline-block',
|
68 |
'margin': '0 1px'
|
69 |
}
|
70 |
-
|
71 |
# Convert style to inline CSS
|
72 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
73 |
-
|
74 |
# Wrap the letter in a span
|
75 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
76 |
styled_letters.append(styled_letter)
|
77 |
-
|
78 |
# Combine letters into a container with inline display
|
79 |
return f'<span>{" ".join(styled_letters)}</span>'
|
80 |
|
81 |
# Function to generate the movement design (random color)
|
82 |
def generate_movement_design(word):
|
83 |
"""Generate movement design for the special word in random color."""
|
84 |
-
# Controlled randomization parameters
|
85 |
-
# Reuse the same parameters or define new ranges for the movement
|
86 |
fonts = [
|
87 |
"'VT323', monospace",
|
88 |
"'Josefin Sans', sans-serif",
|
89 |
"'Rajdhani', sans-serif",
|
90 |
"'Anton', sans-serif",
|
91 |
-
|
|
|
|
|
|
|
92 |
]
|
93 |
font_sizes = ["20px", "22px", "24px", "26px", "28px", "30px"]
|
94 |
font_tops = ["10px", "12px", "14px", "16px"]
|
@@ -96,14 +102,13 @@ def generate_movement_design(word):
|
|
96 |
text_shadows = [
|
97 |
"0px 0px 5px #ff0000", # Red glow
|
98 |
"0px 0px 5px #00ff00", # Green glow
|
99 |
-
"0px 0px 5px #0000ff"
|
100 |
-
# Add more if desired
|
101 |
]
|
102 |
skew_angles = ["-15deg", "-10deg", "0deg", "10deg", "15deg"]
|
103 |
-
|
104 |
# Generate a unique animation name for CSS
|
105 |
animation_name = f"animate_{random.randint(0, 10000)}"
|
106 |
-
|
107 |
# Create CSS keyframes for the animation
|
108 |
keyframes = f"""
|
109 |
@keyframes {animation_name} {{
|
@@ -112,9 +117,10 @@ def generate_movement_design(word):
|
|
112 |
100% {{ transform: translateY(0px); }}
|
113 |
}}
|
114 |
"""
|
|
|
115 |
# Generate random color
|
116 |
random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
117 |
-
|
118 |
# Create HTML for each letter with random styling and animation (random color)
|
119 |
letters = list(word)
|
120 |
styled_letters = []
|
@@ -134,14 +140,14 @@ def generate_movement_design(word):
|
|
134 |
'margin': '0 1px',
|
135 |
'animation': f'{animation_name} 1s ease-in-out forwards'
|
136 |
}
|
137 |
-
|
138 |
# Convert style to inline CSS
|
139 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
140 |
-
|
141 |
# Wrap the letter in a span
|
142 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
143 |
styled_letters.append(styled_letter)
|
144 |
-
|
145 |
# Combine letters into a container with inline display and include keyframes
|
146 |
return f'''
|
147 |
<style>
|
@@ -150,37 +156,35 @@ def generate_movement_design(word):
|
|
150 |
<span>{" ".join(styled_letters)}</span>
|
151 |
'''
|
152 |
|
153 |
-
# Global variable to store the initial design
|
154 |
-
initial_word_design = ""
|
155 |
-
|
156 |
def process_text(input_text):
|
157 |
"""Process text and generate the initial output with special word styled in black."""
|
158 |
-
global initial_word_design
|
159 |
-
|
160 |
# Generate text with limited length
|
161 |
generated = generator(input_text, max_length=50, num_return_sequences=1)
|
162 |
generated_text = generated[0]['generated_text']
|
163 |
-
|
164 |
# Limit output length to prevent overflow
|
165 |
generated_text = generated_text[:300] # Limit to first 300 characters
|
166 |
-
|
167 |
# Split text into words
|
168 |
words = generated_text.split()
|
169 |
-
|
170 |
# Check for special words and apply initial styling
|
171 |
for i, word in enumerate(words):
|
172 |
# Clean the word for matching
|
173 |
clean_word = ''.join(filter(str.isalnum, word)).lower()
|
174 |
if clean_word in SPECIAL_WORDS:
|
|
|
175 |
# Store the initial design
|
176 |
initial_word_design = generate_initial_design(word)
|
177 |
words[i] = initial_word_design
|
178 |
else:
|
179 |
words[i] = word
|
180 |
-
|
181 |
# Combine words back into HTML-formatted text
|
182 |
output_html = ' '.join(words)
|
183 |
-
|
184 |
# Build the complete HTML
|
185 |
final_output = f"""
|
186 |
<html>
|
@@ -214,6 +218,9 @@ def process_text(input_text):
|
|
214 |
line-height: 1.6;
|
215 |
font-family: "Josefin Sans", sans-serif;
|
216 |
}}
|
|
|
|
|
|
|
217 |
</style>
|
218 |
</head>
|
219 |
<body>
|
@@ -223,68 +230,66 @@ def process_text(input_text):
|
|
223 |
</body>
|
224 |
</html>
|
225 |
"""
|
226 |
-
|
227 |
return final_output
|
228 |
|
229 |
def trigger_movement(input_html):
|
230 |
"""Function to trigger the movement animation by updating the special word design."""
|
231 |
-
|
232 |
-
|
233 |
-
global initial_word_design
|
234 |
-
|
235 |
# Edge case: if initial_word_design is empty, return the input_html
|
236 |
-
if not initial_word_design:
|
237 |
return input_html # No special word was found previously
|
238 |
-
|
239 |
-
# Extract the special word from the initial design
|
240 |
-
start_marker = '<span class="'
|
241 |
-
start_index = initial_word_design.find(start_marker)
|
242 |
-
if start_index == -1:
|
243 |
-
return input_html # Can't find the initial design
|
244 |
-
|
245 |
-
# Get the word from the initial_design
|
246 |
-
special_word = ''.join([c for c in initial_word_design if c.isalpha()])
|
247 |
-
|
248 |
# Generate the movement design
|
249 |
movement_design = generate_movement_design(special_word)
|
250 |
-
|
251 |
# Replace the initial design with the movement design in the input_html
|
252 |
updated_html = input_html.replace(initial_word_design, movement_design, 1)
|
253 |
-
|
254 |
-
#
|
255 |
-
|
256 |
-
# Alternatively, we can schedule the change back using CSS animations
|
257 |
-
|
258 |
-
# Append CSS to change back to black after animation (using animation-fill-mode)
|
259 |
-
updated_html = updated_html.replace('</head>', '''
|
260 |
<style>
|
|
|
|
|
|
|
|
|
261 |
.styled-letter {
|
262 |
-
animation
|
|
|
263 |
}
|
264 |
</style>
|
265 |
-
</head>
|
266 |
-
|
|
|
|
|
267 |
return updated_html
|
268 |
|
269 |
# Create Gradio interface using Blocks
|
270 |
with gr.Blocks() as demo:
|
271 |
gr.Markdown("# Circular Text Styler\nEnter a prompt to generate text with special word styling.")
|
272 |
-
|
273 |
with gr.Row():
|
274 |
input_text = gr.Textbox(label="Input Prompt")
|
275 |
submit_button = gr.Button("Generate")
|
276 |
-
|
277 |
output_html = gr.HTML()
|
278 |
animate_button = gr.Button("Trigger Movement")
|
279 |
-
|
280 |
# Connect functions
|
281 |
submit_button.click(process_text, inputs=input_text, outputs=output_html)
|
282 |
animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
|
283 |
-
|
|
|
284 |
gr.HTML("""
|
285 |
<style>
|
286 |
/* Center the buttons */
|
287 |
.gradio-container .gr-button {
|
288 |
margin-top: 10px;
|
289 |
margin-bottom: 10px;
|
290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Predefined words to check
|
9 |
SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat']
|
10 |
|
11 |
+
# Global variable to store the initial design
|
12 |
+
initial_word_design = ""
|
13 |
+
special_word = ""
|
14 |
+
|
15 |
# Function to generate the initial design (black color)
|
16 |
def generate_initial_design(word):
|
17 |
"""Generate initial design for the special word in black color."""
|
|
|
38 |
"'Annie Use Your Telescope', cursive"
|
39 |
]
|
40 |
font_sizes = ["17px", "19px", "21px", "23px", "25px", "27px"]
|
41 |
+
font_tops = ["11px", "13px", "15px"]
|
42 |
letter_spacings = ["-6px", "-4px", "-3px", "-2px", "-1px", "0px", "1px", "2px"]
|
43 |
text_shadows = [
|
44 |
"0px 0px 1px",
|
|
|
52 |
"1px 0px 1px"
|
53 |
]
|
54 |
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
55 |
+
|
56 |
# Create HTML for each letter with random styling (black color)
|
57 |
letters = list(word)
|
58 |
styled_letters = []
|
|
|
71 |
'display': 'inline-block',
|
72 |
'margin': '0 1px'
|
73 |
}
|
74 |
+
|
75 |
# Convert style to inline CSS
|
76 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
77 |
+
|
78 |
# Wrap the letter in a span
|
79 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
80 |
styled_letters.append(styled_letter)
|
81 |
+
|
82 |
# Combine letters into a container with inline display
|
83 |
return f'<span>{" ".join(styled_letters)}</span>'
|
84 |
|
85 |
# Function to generate the movement design (random color)
|
86 |
def generate_movement_design(word):
|
87 |
"""Generate movement design for the special word in random color."""
|
88 |
+
# Controlled randomization parameters
|
|
|
89 |
fonts = [
|
90 |
"'VT323', monospace",
|
91 |
"'Josefin Sans', sans-serif",
|
92 |
"'Rajdhani', sans-serif",
|
93 |
"'Anton', sans-serif",
|
94 |
+
"'Caveat', cursive",
|
95 |
+
"'Patrick Hand', cursive",
|
96 |
+
"'Nothing You Could Do', cursive",
|
97 |
+
"'Reenie Beanie', cursive"
|
98 |
]
|
99 |
font_sizes = ["20px", "22px", "24px", "26px", "28px", "30px"]
|
100 |
font_tops = ["10px", "12px", "14px", "16px"]
|
|
|
102 |
text_shadows = [
|
103 |
"0px 0px 5px #ff0000", # Red glow
|
104 |
"0px 0px 5px #00ff00", # Green glow
|
105 |
+
"0px 0px 5px #0000ff" # Blue glow
|
|
|
106 |
]
|
107 |
skew_angles = ["-15deg", "-10deg", "0deg", "10deg", "15deg"]
|
108 |
+
|
109 |
# Generate a unique animation name for CSS
|
110 |
animation_name = f"animate_{random.randint(0, 10000)}"
|
111 |
+
|
112 |
# Create CSS keyframes for the animation
|
113 |
keyframes = f"""
|
114 |
@keyframes {animation_name} {{
|
|
|
117 |
100% {{ transform: translateY(0px); }}
|
118 |
}}
|
119 |
"""
|
120 |
+
|
121 |
# Generate random color
|
122 |
random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
123 |
+
|
124 |
# Create HTML for each letter with random styling and animation (random color)
|
125 |
letters = list(word)
|
126 |
styled_letters = []
|
|
|
140 |
'margin': '0 1px',
|
141 |
'animation': f'{animation_name} 1s ease-in-out forwards'
|
142 |
}
|
143 |
+
|
144 |
# Convert style to inline CSS
|
145 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
146 |
+
|
147 |
# Wrap the letter in a span
|
148 |
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
149 |
styled_letters.append(styled_letter)
|
150 |
+
|
151 |
# Combine letters into a container with inline display and include keyframes
|
152 |
return f'''
|
153 |
<style>
|
|
|
156 |
<span>{" ".join(styled_letters)}</span>
|
157 |
'''
|
158 |
|
|
|
|
|
|
|
159 |
def process_text(input_text):
|
160 |
"""Process text and generate the initial output with special word styled in black."""
|
161 |
+
global initial_word_design, special_word
|
162 |
+
|
163 |
# Generate text with limited length
|
164 |
generated = generator(input_text, max_length=50, num_return_sequences=1)
|
165 |
generated_text = generated[0]['generated_text']
|
166 |
+
|
167 |
# Limit output length to prevent overflow
|
168 |
generated_text = generated_text[:300] # Limit to first 300 characters
|
169 |
+
|
170 |
# Split text into words
|
171 |
words = generated_text.split()
|
172 |
+
|
173 |
# Check for special words and apply initial styling
|
174 |
for i, word in enumerate(words):
|
175 |
# Clean the word for matching
|
176 |
clean_word = ''.join(filter(str.isalnum, word)).lower()
|
177 |
if clean_word in SPECIAL_WORDS:
|
178 |
+
special_word = word # Store the special word
|
179 |
# Store the initial design
|
180 |
initial_word_design = generate_initial_design(word)
|
181 |
words[i] = initial_word_design
|
182 |
else:
|
183 |
words[i] = word
|
184 |
+
|
185 |
# Combine words back into HTML-formatted text
|
186 |
output_html = ' '.join(words)
|
187 |
+
|
188 |
# Build the complete HTML
|
189 |
final_output = f"""
|
190 |
<html>
|
|
|
218 |
line-height: 1.6;
|
219 |
font-family: "Josefin Sans", sans-serif;
|
220 |
}}
|
221 |
+
.styled-letter {{
|
222 |
+
transition: all 1s ease;
|
223 |
+
}}
|
224 |
</style>
|
225 |
</head>
|
226 |
<body>
|
|
|
230 |
</body>
|
231 |
</html>
|
232 |
"""
|
233 |
+
|
234 |
return final_output
|
235 |
|
236 |
def trigger_movement(input_html):
|
237 |
"""Function to trigger the movement animation by updating the special word design."""
|
238 |
+
global initial_word_design, special_word
|
239 |
+
|
|
|
|
|
240 |
# Edge case: if initial_word_design is empty, return the input_html
|
241 |
+
if not initial_word_design or not special_word:
|
242 |
return input_html # No special word was found previously
|
243 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
# Generate the movement design
|
245 |
movement_design = generate_movement_design(special_word)
|
246 |
+
|
247 |
# Replace the initial design with the movement design in the input_html
|
248 |
updated_html = input_html.replace(initial_word_design, movement_design, 1)
|
249 |
+
|
250 |
+
# Append CSS to reset to initial design after animation
|
251 |
+
css_reset = """
|
|
|
|
|
|
|
|
|
252 |
<style>
|
253 |
+
@keyframes reset_animation {
|
254 |
+
from { color: inherit; }
|
255 |
+
to { color: #000000; }
|
256 |
+
}
|
257 |
.styled-letter {
|
258 |
+
animation: reset_animation 0s forwards;
|
259 |
+
animation-delay: 1s;
|
260 |
}
|
261 |
</style>
|
262 |
+
</head>
|
263 |
+
"""
|
264 |
+
updated_html = updated_html.replace('</head>', css_reset)
|
265 |
+
|
266 |
return updated_html
|
267 |
|
268 |
# Create Gradio interface using Blocks
|
269 |
with gr.Blocks() as demo:
|
270 |
gr.Markdown("# Circular Text Styler\nEnter a prompt to generate text with special word styling.")
|
271 |
+
|
272 |
with gr.Row():
|
273 |
input_text = gr.Textbox(label="Input Prompt")
|
274 |
submit_button = gr.Button("Generate")
|
275 |
+
|
276 |
output_html = gr.HTML()
|
277 |
animate_button = gr.Button("Trigger Movement")
|
278 |
+
|
279 |
# Connect functions
|
280 |
submit_button.click(process_text, inputs=input_text, outputs=output_html)
|
281 |
animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
|
282 |
+
|
283 |
+
# Add custom CSS
|
284 |
gr.HTML("""
|
285 |
<style>
|
286 |
/* Center the buttons */
|
287 |
.gradio-container .gr-button {
|
288 |
margin-top: 10px;
|
289 |
margin-bottom: 10px;
|
290 |
+
}
|
291 |
+
</style>
|
292 |
+
""")
|
293 |
+
|
294 |
+
# Launch the app
|
295 |
+
demo.launch()
|