Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,94 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import time
|
|
|
4 |
|
|
|
5 |
def style_special_word(word):
|
6 |
-
"""Apply fancy random style to a special word."""
|
7 |
fonts = [
|
8 |
-
"'
|
9 |
-
"'
|
10 |
-
"'
|
11 |
-
"'
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
]
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
"
|
18 |
-
"0px 0px
|
19 |
-
"
|
20 |
-
"
|
|
|
|
|
|
|
|
|
|
|
21 |
]
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
|
|
|
|
|
|
|
|
40 |
def typing_effect():
|
41 |
-
"""Stream paragraphs word by word with fancy styled words."""
|
42 |
paragraphs = [
|
43 |
"Proctor's voice quavered, “Where are we, Benshiro? This can't be real. We went full speed into that...”",
|
44 |
"“Proctor, I hear you! Is that you? I'm so thankful, but where are you? I can't see you.” Benshiro's words echoed in the void."
|
45 |
]
|
46 |
html_output = ""
|
47 |
-
|
48 |
for paragraph in paragraphs:
|
49 |
words = paragraph.split()
|
50 |
-
# Choose a random special word
|
51 |
special_word = random.choice(words)
|
52 |
-
styled_words = [
|
53 |
-
|
54 |
-
for w in words
|
55 |
-
]
|
56 |
for w in styled_words:
|
57 |
html_output += w + " "
|
58 |
yield html_output
|
59 |
time.sleep(0.05)
|
|
|
60 |
html_output += "<br><br>"
|
61 |
-
time.sleep(0.
|
62 |
|
|
|
63 |
def animate_letters(current_html):
|
64 |
-
"""Randomly restyle all previously styled words."""
|
65 |
-
# Just re-run the style_special_word on each styled span text
|
66 |
-
import re
|
67 |
def replace_style(match):
|
68 |
-
word = match.group(2)
|
69 |
return style_special_word(word)
|
70 |
|
71 |
-
return re.sub(r'(<span
|
72 |
-
lambda m: replace_style(m),
|
73 |
-
current_html)
|
74 |
|
75 |
with gr.Blocks() as demo:
|
76 |
-
gr.Markdown("## Styled Typing with
|
77 |
output_html = gr.HTML()
|
78 |
-
|
79 |
with gr.Row():
|
80 |
-
start_btn = gr.Button("Start Typing")
|
81 |
-
animate_btn = gr.Button("Animate Letters")
|
82 |
-
|
83 |
start_btn.click(typing_effect, outputs=output_html)
|
84 |
animate_btn.click(animate_letters, inputs=output_html, outputs=output_html)
|
85 |
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import time
|
4 |
+
import re
|
5 |
|
6 |
+
# Fancy random styling for special word
|
7 |
def style_special_word(word):
|
|
|
8 |
fonts = [
|
9 |
+
"'VT323', monospace",
|
10 |
+
"'Josefin Sans', sans-serif",
|
11 |
+
"'Rajdhani', sans-serif",
|
12 |
+
"'Anton', sans-serif",
|
13 |
+
"'Caveat', cursive",
|
14 |
+
"'Patrick Hand', cursive",
|
15 |
+
"'Nothing You Could Do', cursive",
|
16 |
+
"'Reenie Beanie', cursive",
|
17 |
+
"'Orbitron', sans-serif",
|
18 |
+
"'Raleway', sans-serif"
|
19 |
]
|
20 |
+
font_sizes = ["18px", "19px", "20px"]
|
21 |
+
font_tops = ["0px", "1px", "-1px"]
|
22 |
+
letter_spacings = ["-1px", "0px", "1px"]
|
23 |
+
text_shadows = [
|
24 |
+
"0px 0px 1px",
|
25 |
+
"0px 0px 2px",
|
26 |
+
"1px 0px 0px",
|
27 |
+
"0px 0px 0px",
|
28 |
+
"0px 1px 0px",
|
29 |
+
"0px 2px 0px",
|
30 |
+
"0px 1px 1px",
|
31 |
+
"1px 1px 0px",
|
32 |
+
"1px 0px 1px"
|
33 |
]
|
34 |
+
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
35 |
+
|
36 |
+
style = {
|
37 |
+
'font-family': random.choice(fonts),
|
38 |
+
'line-height': '1.6',
|
39 |
+
'font-size': random.choice(font_sizes),
|
40 |
+
'letter-spacing': random.choice(letter_spacings),
|
41 |
+
'text-shadow': random.choice(text_shadows),
|
42 |
+
'transform': f'skew({random.choice(skew_angles)})',
|
43 |
+
'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
|
44 |
+
'position': 'relative',
|
45 |
+
'top': random.choice(font_tops),
|
46 |
+
'color': '#000000',
|
47 |
+
'display': 'inline-block',
|
48 |
+
'margin': '0 1px',
|
49 |
+
'vertical-align': 'middle',
|
50 |
+
'cursor': 'pointer'
|
51 |
+
}
|
52 |
|
53 |
+
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
54 |
+
return f'<span class="styled-letter" style="{style_str}">{word}</span>'
|
55 |
+
|
56 |
+
# Typing effect with styled special words
|
57 |
def typing_effect():
|
|
|
58 |
paragraphs = [
|
59 |
"Proctor's voice quavered, “Where are we, Benshiro? This can't be real. We went full speed into that...”",
|
60 |
"“Proctor, I hear you! Is that you? I'm so thankful, but where are you? I can't see you.” Benshiro's words echoed in the void."
|
61 |
]
|
62 |
html_output = ""
|
63 |
+
|
64 |
for paragraph in paragraphs:
|
65 |
words = paragraph.split()
|
|
|
66 |
special_word = random.choice(words)
|
67 |
+
styled_words = [style_special_word(w) if w == special_word else w for w in words]
|
68 |
+
|
|
|
|
|
69 |
for w in styled_words:
|
70 |
html_output += w + " "
|
71 |
yield html_output
|
72 |
time.sleep(0.05)
|
73 |
+
|
74 |
html_output += "<br><br>"
|
75 |
+
time.sleep(0.3)
|
76 |
|
77 |
+
# Re-style all existing styled words when button clicked
|
78 |
def animate_letters(current_html):
|
|
|
|
|
|
|
79 |
def replace_style(match):
|
80 |
+
word = match.group(2)
|
81 |
return style_special_word(word)
|
82 |
|
83 |
+
return re.sub(r'(<span[^>]*>)([^<]+)(</span>)', lambda m: replace_style(m), current_html)
|
|
|
|
|
84 |
|
85 |
with gr.Blocks() as demo:
|
86 |
+
gr.Markdown("## 🎨 Fancy Styled Typing Effect with Animation Button")
|
87 |
output_html = gr.HTML()
|
|
|
88 |
with gr.Row():
|
89 |
+
start_btn = gr.Button("▶ Start Typing")
|
90 |
+
animate_btn = gr.Button("✨ Animate Letters")
|
91 |
+
|
92 |
start_btn.click(typing_effect, outputs=output_html)
|
93 |
animate_btn.click(animate_letters, inputs=output_html, outputs=output_html)
|
94 |
|