circulartext commited on
Commit
f5e26af
·
verified ·
1 Parent(s): f30068b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -145
app.py CHANGED
@@ -1,165 +1,86 @@
1
  import gradio as gr
2
  import random
 
3
 
4
- def generate_initial_design(word):
5
- """Generate initial design for the special word in black color."""
6
  fonts = [
7
- "'VT323', monospace",
8
- "'Josefin Sans', sans-serif",
9
- "'Rajdhani', sans-serif",
10
- "'Anton', sans-serif",
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
- font_sizes = ["18px", "19px", "20px"]
19
- font_tops = ["0px", "1px", "-1px"]
20
- letter_spacings = ["-1px", "0px", "1px"]
21
- text_shadows = [
22
- "0px 0px 1px",
23
- "0px 0px 2px",
24
- "1px 0px 0px",
25
- "0px 0px 0px",
26
- "0px 1px 0px",
27
- "0px 2px 0px",
28
- "0px 1px 1px",
29
- "1px 1px 0px",
30
- "1px 0px 1px"
 
 
31
  ]
32
- skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
33
-
34
- letters = list(word)
35
- styled_letters = []
36
 
37
- for letter in letters:
38
- style = {
39
- 'font-family': random.choice(fonts),
40
- 'line-height': '1.6',
41
- 'font-size': random.choice(font_sizes),
42
- 'letter-spacing': random.choice(letter_spacings),
43
- 'text-shadow': random.choice(text_shadows),
44
- 'transform': f'skew({random.choice(skew_angles)})',
45
- 'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
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
- return f'<span style="display:inline-flex;align-items:baseline;margin:0 2px;">{"".join(styled_letters)}</span>'
60
-
61
- def generate_output_html():
62
  paragraphs = [
63
- "Proctors's voice quavered, “Where are we, Benshiro? This can't be real. We went full speed into that...”",
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
- processed_paragraphs = []
68
  for paragraph in paragraphs:
69
  words = paragraph.split()
 
70
  special_word = random.choice(words)
71
- styled_word = generate_initial_design(special_word)
72
- styled_paragraph = ' '.join(word if word != special_word else styled_word for word in words)
73
- processed_paragraphs.append(styled_paragraph)
74
-
75
- return f"""
76
- <html>
77
- <head>
78
- <link href="https://fonts.googleapis.com/css?family=VT323|Josefin+Sans|Rajdhani|Anton|Caveat|Patrick+Hand|Nothing+You+Could+Do|Reenie+Beanie|Orbitron|Raleway" rel="stylesheet">
79
- <style>
80
- body {{
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
- function typeParagraph(index) {{
118
- return new Promise(resolve => {{
119
- if (index >= paragraphs.length) {{
120
- resolve();
121
- return;
122
- }}
123
- let container = document.getElementById('chat-area');
124
- let words = paragraphs[index].split(' ');
125
- let i = 0;
126
- let pElem = document.createElement('p');
127
- container.appendChild(pElem);
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("# Styled Text Display with Clickable Animation")
162
  output_html = gr.HTML()
163
- demo.load(fn=generate_output_html, outputs=output_html)
 
 
 
 
 
 
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()