Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,165 +1,86 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
|
|
3 |
|
4 |
-
def
|
5 |
-
"""
|
6 |
fonts = [
|
7 |
-
"'
|
8 |
-
"'
|
9 |
-
"'
|
10 |
-
"'
|
11 |
-
"'Caveat', cursive",
|
12 |
-
"'Patrick Hand', cursive",
|
13 |
-
"'Nothing You Could Do', cursive",
|
14 |
-
"'Reenie Beanie', cursive",
|
15 |
-
"'Orbitron', sans-serif",
|
16 |
-
"'Raleway', sans-serif"
|
17 |
]
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
"
|
23 |
-
"0px 0px
|
24 |
-
"
|
25 |
-
"
|
26 |
-
|
27 |
-
|
28 |
-
"
|
29 |
-
"
|
30 |
-
"
|
|
|
|
|
31 |
]
|
32 |
-
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
33 |
-
|
34 |
-
letters = list(word)
|
35 |
-
styled_letters = []
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
'position': 'relative',
|
47 |
-
'top': random.choice(font_tops),
|
48 |
-
'color': '#000000',
|
49 |
-
'display': 'inline-block',
|
50 |
-
'margin': '0 1px',
|
51 |
-
'vertical-align': 'middle',
|
52 |
-
'cursor': 'pointer'
|
53 |
-
}
|
54 |
-
|
55 |
-
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
56 |
-
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
57 |
-
styled_letters.append(styled_letter)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
def generate_output_html():
|
62 |
paragraphs = [
|
63 |
-
"
|
64 |
"“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."
|
65 |
]
|
66 |
-
|
67 |
-
|
68 |
for paragraph in paragraphs:
|
69 |
words = paragraph.split()
|
|
|
70 |
special_word = random.choice(words)
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
<
|
80 |
-
|
81 |
-
font-family: "Josefin Sans", sans-serif;
|
82 |
-
background-color: #fff;
|
83 |
-
padding: 20px;
|
84 |
-
color: #000;
|
85 |
-
line-height: 1.6;
|
86 |
-
font-size: 18px;
|
87 |
-
}}
|
88 |
-
.styled-letter {{
|
89 |
-
transition: transform 0.3s ease;
|
90 |
-
display: inline-block;
|
91 |
-
}}
|
92 |
-
.styled-letter.animate {{
|
93 |
-
transform: rotate(10deg) scale(1.3);
|
94 |
-
}}
|
95 |
-
#animate-btn {{
|
96 |
-
background-color: #4CAF50;
|
97 |
-
border: none;
|
98 |
-
color: white;
|
99 |
-
padding: 10px 20px;
|
100 |
-
font-size: 16px;
|
101 |
-
cursor: pointer;
|
102 |
-
margin-top: 15px;
|
103 |
-
border-radius: 6px;
|
104 |
-
}}
|
105 |
-
#animate-btn:hover {{
|
106 |
-
background-color: #45a049;
|
107 |
-
}}
|
108 |
-
</style>
|
109 |
-
</head>
|
110 |
-
<body>
|
111 |
-
<div id="chat-area"></div>
|
112 |
-
<button id="animate-btn" onclick="triggerMovement()">Animate Letters</button>
|
113 |
-
|
114 |
-
<script>
|
115 |
-
const paragraphs = {processed_paragraphs};
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
function addWord() {{
|
129 |
-
if (i < words.length) {{
|
130 |
-
pElem.innerHTML += words[i] + ' ';
|
131 |
-
i++;
|
132 |
-
setTimeout(addWord, 60); // typing speed
|
133 |
-
}} else {{
|
134 |
-
setTimeout(() => resolve(), 500); // pause before next paragraph
|
135 |
-
}}
|
136 |
-
}}
|
137 |
-
addWord();
|
138 |
-
}});
|
139 |
-
}}
|
140 |
-
|
141 |
-
async function startTyping() {{
|
142 |
-
for (let i = 0; i < paragraphs.length; i++) {{
|
143 |
-
await typeParagraph(i);
|
144 |
-
}}
|
145 |
-
}}
|
146 |
-
|
147 |
-
function triggerMovement() {{
|
148 |
-
document.querySelectorAll('.styled-letter').forEach(el => {{
|
149 |
-
el.classList.add('animate');
|
150 |
-
setTimeout(() => el.classList.remove('animate'), 300);
|
151 |
-
}});
|
152 |
-
}}
|
153 |
-
|
154 |
-
window.onload = startTyping;
|
155 |
-
</script>
|
156 |
-
</body>
|
157 |
-
</html>
|
158 |
-
"""
|
159 |
|
160 |
with gr.Blocks() as demo:
|
161 |
-
gr.Markdown("
|
162 |
output_html = gr.HTML()
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
demo.launch()
|
|
|
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 |
+
"'Courier New', monospace",
|
9 |
+
"'Arial', sans-serif",
|
10 |
+
"'Georgia', serif",
|
11 |
+
"'Times New Roman', serif"
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
]
|
13 |
+
colors = ["red", "blue", "green", "black", "orange", "purple", "deeppink", "cyan"]
|
14 |
+
font_weights = ["bold", "normal", "bolder"]
|
15 |
+
decorations = ["underline", "overline", "line-through", "none"]
|
16 |
+
shadows = [
|
17 |
+
"2px 2px 5px gray",
|
18 |
+
"0px 0px 10px gold",
|
19 |
+
"3px 3px 5px #000000",
|
20 |
+
"1px 1px 5px lime"
|
21 |
+
]
|
22 |
+
transforms = [
|
23 |
+
"rotate(5deg)",
|
24 |
+
"rotate(-5deg)",
|
25 |
+
"scale(1.2)",
|
26 |
+
"skewX(10deg)",
|
27 |
+
"skewY(-5deg)"
|
28 |
]
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
style = (
|
31 |
+
f"font-family:{random.choice(fonts)};"
|
32 |
+
f"color:{random.choice(colors)};"
|
33 |
+
f"font-weight:{random.choice(font_weights)};"
|
34 |
+
f"text-decoration:{random.choice(decorations)};"
|
35 |
+
f"text-shadow:{random.choice(shadows)};"
|
36 |
+
f"display:inline-block; transform:{random.choice(transforms)};"
|
37 |
+
)
|
38 |
+
return f'<span style="{style}">{word}</span>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
style_special_word(w) if w == special_word else w
|
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.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) # the inner text
|
69 |
+
return style_special_word(word)
|
70 |
+
|
71 |
+
return re.sub(r'(<span style="[^"]*">)([^<]+)(</span>)',
|
72 |
+
lambda m: replace_style(m),
|
73 |
+
current_html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
with gr.Blocks() as demo:
|
76 |
+
gr.Markdown("## Styled Typing with Clickable Animation")
|
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 |
|
86 |
demo.launch()
|