circulartext commited on
Commit
c8cd9c7
·
verified ·
1 Parent(s): 46404cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -1,11 +1,8 @@
1
  import gradio as gr
2
  import random
3
 
4
- # Global variables
5
- initial_word_design = ""
6
- special_words = []
7
-
8
  def generate_initial_design(word):
 
9
  fonts = [
10
  "'VT323', monospace",
11
  "'Josefin Sans', sans-serif",
@@ -37,7 +34,7 @@ def generate_initial_design(word):
37
  letters = list(word)
38
  styled_letters = []
39
 
40
- for letter in letters:
41
  style = {
42
  'font-family': random.choice(fonts),
43
  'line-height': '1.6',
@@ -51,11 +48,12 @@ def generate_initial_design(word):
51
  'color': '#000000',
52
  'display': 'inline-block',
53
  'margin': '0 1px',
54
- 'vertical-align': 'middle'
 
55
  }
56
 
57
  style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
58
- styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
59
  styled_letters.append(styled_letter)
60
 
61
  return f'''
@@ -66,9 +64,7 @@ def generate_initial_design(word):
66
  {" ".join(styled_letters)}
67
  </span>'''
68
 
69
- def process_text():
70
- global initial_word_design, special_words
71
-
72
  paragraphs = [
73
  "Proctors's voice quavered, “Where are we, Benshiro? This can't be real. We went full speed into that...”",
74
  "“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."
@@ -82,10 +78,10 @@ def process_text():
82
  styled_word = generate_initial_design(special_word)
83
  styled_paragraph = ' '.join(word if word != special_word else styled_word for word in words)
84
  result.append(styled_paragraph)
85
-
86
  output_html = '<br><br>'.join(result)
87
 
88
- final_output = f"""
89
  <html>
90
  <head>
91
  <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">
@@ -98,25 +94,49 @@ def process_text():
98
  font-family: "Josefin Sans", sans-serif;
99
  padding: 20px;
100
  }}
 
 
 
101
  </style>
 
 
 
 
 
 
 
 
102
  </head>
103
  <body>
104
- <div style='max-width: 800px; margin: auto;'>
105
  {output_html}
106
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  </body>
108
  </html>
109
  """
110
-
111
- return final_output
112
 
113
  # Create Gradio interface using Blocks
114
  with gr.Blocks() as demo:
115
- gr.Markdown("# Styled Text Display")
116
-
117
  output_html = gr.HTML()
118
-
119
- demo.load(fn=process_text, outputs=output_html)
120
 
121
  # Launch the app
122
  demo.launch()
 
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",
 
34
  letters = list(word)
35
  styled_letters = []
36
 
37
+ for i, letter in enumerate(letters):
38
  style = {
39
  'font-family': random.choice(fonts),
40
  'line-height': '1.6',
 
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}" onclick="triggerMovement(this)">{letter}</span>'
57
  styled_letters.append(styled_letter)
58
 
59
  return f'''
 
64
  {" ".join(styled_letters)}
65
  </span>'''
66
 
67
+ def generate_output_html():
 
 
68
  paragraphs = [
69
  "Proctors's voice quavered, “Where are we, Benshiro? This can't be real. We went full speed into that...”",
70
  "“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."
 
78
  styled_word = generate_initial_design(special_word)
79
  styled_paragraph = ' '.join(word if word != special_word else styled_word for word in words)
80
  result.append(styled_paragraph)
81
+
82
  output_html = '<br><br>'.join(result)
83
 
84
+ return f"""
85
  <html>
86
  <head>
87
  <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">
 
94
  font-family: "Josefin Sans", sans-serif;
95
  padding: 20px;
96
  }}
97
+ .styled-letter {{
98
+ transition: all 0.3s ease;
99
+ }}
100
  </style>
101
+ <script>
102
+ function triggerMovement(el) {{
103
+ el.style.animation = '';
104
+ setTimeout(() => {{
105
+ el.style.animation = 'skew 0.5s ease-in-out';
106
+ }}, 10);
107
+ }}
108
+ </script>
109
  </head>
110
  <body>
111
+ <div style='max-width: 800px; margin: auto;' id="text-content">
112
  {output_html}
113
  </div>
114
+ <script>
115
+ function revealWords() {{
116
+ const textContent = document.getElementById('text-content');
117
+ const words = textContent.innerHTML.split(' ');
118
+ textContent.innerHTML = '';
119
+ let i = 0;
120
+ function displayWord() {{
121
+ if (i < words.length) {{
122
+ textContent.innerHTML += words[i] + ' ';
123
+ i++;
124
+ setTimeout(displayWord, 200);
125
+ }}
126
+ }}
127
+ displayWord();
128
+ }}
129
+ revealWords();
130
+ </script>
131
  </body>
132
  </html>
133
  """
 
 
134
 
135
  # Create Gradio interface using Blocks
136
  with gr.Blocks() as demo:
137
+ gr.Markdown("# Styled Text Display with Clickable Animation")
 
138
  output_html = gr.HTML()
139
+ demo.load(fn=generate_output_html, outputs=output_html)
 
140
 
141
  # Launch the app
142
  demo.launch()