circulartext commited on
Commit
de5a24b
·
verified ·
1 Parent(s): 5180106

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -116
app.py CHANGED
@@ -3,19 +3,17 @@ import random
3
  from transformers import pipeline
4
 
5
  # Load the model once when the app starts
6
- generator = pipeline('text-generation', model='distilgpt2') # Lightweight model
7
 
8
  # Predefined words to check
9
- SPECIAL_WORDS = ['weather', 'sun', 'middle', 'spring', '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."""
18
- # Controlled randomization parameters
19
  fonts = [
20
  "'VT323', monospace",
21
  "'Josefin Sans', sans-serif",
@@ -31,11 +29,7 @@ def generate_initial_design(word):
31
  "'Poiret One', cursive",
32
  "'Indie Flower', cursive",
33
  "'Pacifico', cursive",
34
- "'Teko', sans-serif",
35
- "'Abril Fatface', cursive",
36
- "'Gloria Hallelujah', cursive",
37
- "'Righteous', cursive",
38
- "'Annie Use Your Telescope', cursive"
39
  ]
40
  font_sizes = ["17px", "19px", "21px", "23px", "25px", "27px"]
41
  font_tops = ["11px", "13px", "15px"]
@@ -53,9 +47,9 @@ def generate_initial_design(word):
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 = []
 
59
  for i, letter in enumerate(letters):
60
  style = {
61
  'font-family': random.choice(fonts),
@@ -71,21 +65,15 @@ def generate_initial_design(word):
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",
@@ -94,36 +82,44 @@ def generate_movement_design(word):
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"]
101
- letter_spacings = ["-3px", "-2px", "-1px", "0px", "1px", "2px", "3px"]
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} {{
115
- 0% {{ transform: translateY(0px); }}
116
- 50% {{ transform: translateY(-10px); }}
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 = []
 
127
  for i, letter in enumerate(letters):
128
  style = {
129
  'font-family': random.choice(fonts),
@@ -132,28 +128,30 @@ def generate_movement_design(word):
132
  'letter-spacing': random.choice(letter_spacings),
133
  'text-shadow': random.choice(text_shadows),
134
  'transform': f'skew({random.choice(skew_angles)})',
135
- 'margin-top': random.choice(["-0.03cm", "0.00cm", "0.03cm"]),
136
  'position': 'relative',
137
  'top': random.choice(font_tops),
138
- 'color': random_color, # Random color
139
  'display': 'inline-block',
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>
154
  {keyframes}
 
 
 
155
  </style>
156
- <span>{" ".join(styled_letters)}</span>
 
 
157
  '''
158
 
159
  def process_text(input_text):
@@ -161,35 +159,26 @@ def process_text(input_text):
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>
191
  <head>
192
- <!-- Include all necessary fonts -->
193
  <link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet">
194
  <link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@100&display=swap" rel="stylesheet">
195
  <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@300&display=swap" rel="stylesheet">
@@ -199,17 +188,7 @@ def process_text(input_text):
199
  <link href="https://fonts.googleapis.com/css2?family=Nothing+You+Could+Do&display=swap" rel="stylesheet">
200
  <link href="https://fonts.googleapis.com/css2?family=Reenie+Beanie&display=swap" rel="stylesheet">
201
  <link href="https://fonts.googleapis.com/css2?family=Orbitron&display=swap" rel="stylesheet">
202
- <link href="https://fonts.googleapis.com/css?family=Raleway:500" rel="stylesheet" type="text/css">
203
- <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:700|Oswald:300" rel="stylesheet" type="text/css">
204
- <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:700|Oswald:200" rel="stylesheet" type="text/css">
205
- <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300" rel="stylesheet" type="text/css">
206
- <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet" type="text/css">
207
- <link href="https://fonts.googleapis.com/css?family=Poiret+One|Dosis:300|Fjalla+One" rel="stylesheet" type="text/css">
208
- <link href="https://fonts.googleapis.com/css?family=Indie+Flower|Lobster" rel="stylesheet" type="text/css">
209
- <link href="https://fonts.googleapis.com/css?family=Pacifico|Shadows+Into+Light|Dancing+Script|Amatic+SC" rel="stylesheet" type="text/css">
210
- <link href="https://fonts.googleapis.com/css?family=Teko" rel="stylesheet" type="text/css">
211
- <link href="https://fonts.googleapis.com/css?family=Abril+Fatface|Josefin+Sans|Gloria+Hallelujah|Roboto+Slab|Righteous|Sacramento|Yellowtail" rel="stylesheet">
212
- <link href="https://fonts.googleapis.com/css?family=Annie+Use+Your+Telescope|Just+Me+Again+Down+Here|Nixie+One|Six+Caps|Unkempt" rel="stylesheet">
213
  <style>
214
  body {{
215
  background-color: #fff;
@@ -217,79 +196,45 @@ def process_text(input_text):
217
  font-size: 18px;
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>
227
- <div style='max-width: 800px; margin: auto; padding: 20px;'>
228
  {output_html}
229
  </div>
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()
 
3
  from transformers import pipeline
4
 
5
  # Load the model once when the app starts
6
+ generator = pipeline('text-generation', model='distilgpt2', max_length=25) # Reduced max_length for faster inference
7
 
8
  # Predefined words to check
9
+ SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat', 'spring', 'winter']
10
 
11
+ # Global variables
12
  initial_word_design = ""
13
  special_word = ""
14
 
 
15
  def generate_initial_design(word):
16
  """Generate initial design for the special word in black color."""
 
17
  fonts = [
18
  "'VT323', monospace",
19
  "'Josefin Sans', sans-serif",
 
29
  "'Poiret One', cursive",
30
  "'Indie Flower', cursive",
31
  "'Pacifico', cursive",
32
+ "'Teko', sans-serif"
 
 
 
 
33
  ]
34
  font_sizes = ["17px", "19px", "21px", "23px", "25px", "27px"]
35
  font_tops = ["11px", "13px", "15px"]
 
47
  ]
48
  skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
49
 
 
50
  letters = list(word)
51
  styled_letters = []
52
+
53
  for i, letter in enumerate(letters):
54
  style = {
55
  'font-family': random.choice(fonts),
 
65
  'display': 'inline-block',
66
  'margin': '0 1px'
67
  }
68
+
 
69
  style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
70
+ styled_letter = f'<div class="styled-letter" style="{style_str}">{letter}</div>'
 
 
71
  styled_letters.append(styled_letter)
72
 
73
+ return f'<div style="display: flex; align-items: baseline; position: relative; top: 20px;">{" ".join(styled_letters)}</div>'
 
74
 
 
75
  def generate_movement_design(word):
76
+ """Generate a completely new random design for the movement animation."""
 
77
  fonts = [
78
  "'VT323', monospace",
79
  "'Josefin Sans', sans-serif",
 
82
  "'Caveat', cursive",
83
  "'Patrick Hand', cursive",
84
  "'Nothing You Could Do', cursive",
85
+ "'Reenie Beanie', cursive",
86
+ "'Orbitron', sans-serif",
87
+ "'Raleway', sans-serif"
88
  ]
89
+ font_sizes = ["17px", "19px", "21px", "23px", "25px", "27px"]
90
+ font_tops = ["11px", "13px", "15px"]
91
+ letter_spacings = ["-6px", "-4px", "-3px", "-2px", "-1px", "0px", "1px", "2px"]
92
  text_shadows = [
93
+ "0px 0px 1px",
94
+ "0px 0px 2px",
95
+ "1px 0px 0px",
96
+ "0px 0px 0px",
97
+ "0px 1px 0px",
98
+ "0px 2px 0px",
99
+ "0px 1px 1px",
100
+ "1px 1px 0px",
101
+ "1px 0px 1px"
102
  ]
103
+ skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
104
 
105
+ # Generate random color for the movement design
106
+ random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
107
+
108
+ # Generate unique animation name
109
  animation_name = f"animate_{random.randint(0, 10000)}"
110
 
111
+ # Create keyframes for the animation sequence
112
  keyframes = f"""
113
  @keyframes {animation_name} {{
114
+ 0% {{ transform: scale(1) rotate(0deg); }}
115
+ 50% {{ transform: scale(1.2) rotate(10deg); }}
116
+ 100% {{ transform: scale(1) rotate(0deg); }}
117
  }}
118
  """
119
 
 
 
 
 
120
  letters = list(word)
121
  styled_letters = []
122
+
123
  for i, letter in enumerate(letters):
124
  style = {
125
  'font-family': random.choice(fonts),
 
128
  'letter-spacing': random.choice(letter_spacings),
129
  'text-shadow': random.choice(text_shadows),
130
  'transform': f'skew({random.choice(skew_angles)})',
131
+ 'margin-top': random.choice(["-0.06cm", "-0.03cm", "0.00cm", "0.03cm", "0.06cm"]),
132
  'position': 'relative',
133
  'top': random.choice(font_tops),
134
+ 'color': random_color,
135
  'display': 'inline-block',
136
  'margin': '0 1px',
137
+ 'animation': f'{animation_name} 0.5s ease-in-out',
138
+ 'animation-delay': f'{i * 0.1}s'
139
  }
140
+
 
141
  style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
142
+ styled_letter = f'<div class="styled-letter" style="{style_str}">{letter}</div>'
 
 
143
  styled_letters.append(styled_letter)
144
 
 
145
  return f'''
146
  <style>
147
  {keyframes}
148
+ .styled-letter {{
149
+ transition: all 0.3s ease;
150
+ }}
151
  </style>
152
+ <div style="display: flex; align-items: baseline; position: relative; top: 20px;">
153
+ {" ".join(styled_letters)}
154
+ </div>
155
  '''
156
 
157
  def process_text(input_text):
 
159
  global initial_word_design, special_word
160
 
161
  # Generate text with limited length
162
+ generated = generator(input_text, num_return_sequences=1)
163
  generated_text = generated[0]['generated_text']
164
+ generated_text = generated_text[:200] # Limit output length
165
 
 
 
 
 
166
  words = generated_text.split()
167
+
 
168
  for i, word in enumerate(words):
 
169
  clean_word = ''.join(filter(str.isalnum, word)).lower()
170
  if clean_word in SPECIAL_WORDS:
171
+ special_word = word
 
172
  initial_word_design = generate_initial_design(word)
173
  words[i] = initial_word_design
174
  else:
175
  words[i] = word
176
 
 
177
  output_html = ' '.join(words)
178
+
 
179
  final_output = f"""
180
  <html>
181
  <head>
 
182
  <link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet">
183
  <link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@100&display=swap" rel="stylesheet">
184
  <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@300&display=swap" rel="stylesheet">
 
188
  <link href="https://fonts.googleapis.com/css2?family=Nothing+You+Could+Do&display=swap" rel="stylesheet">
189
  <link href="https://fonts.googleapis.com/css2?family=Reenie+Beanie&display=swap" rel="stylesheet">
190
  <link href="https://fonts.googleapis.com/css2?family=Orbitron&display=swap" rel="stylesheet">
191
+ <link href="https://fonts.googleapis.com/css?family=Raleway:500" rel="stylesheet">
 
 
 
 
 
 
 
 
 
 
192
  <style>
193
  body {{
194
  background-color: #fff;
 
196
  font-size: 18px;
197
  line-height: 1.6;
198
  font-family: "Josefin Sans", sans-serif;
199
+ padding: 20px;
 
 
200
  }}
201
  </style>
202
  </head>
203
  <body>
204
+ <div style='max-width: 800px; margin: auto;'>
205
  {output_html}
206
  </div>
207
  </body>
208
  </html>
209
  """
210
+
211
  return final_output
212
 
213
  def trigger_movement(input_html):
214
+ """Function to trigger the movement animation."""
215
  global initial_word_design, special_word
216
+
 
217
  if not initial_word_design or not special_word:
218
+ return input_html
219
 
 
220
  movement_design = generate_movement_design(special_word)
 
 
221
  updated_html = input_html.replace(initial_word_design, movement_design, 1)
222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  return updated_html
224
 
225
  # Create Gradio interface using Blocks
226
  with gr.Blocks() as demo:
227
  gr.Markdown("# Circular Text Styler\nEnter a prompt to generate text with special word styling.")
228
+
229
  with gr.Row():
230
  input_text = gr.Textbox(label="Input Prompt")
231
  submit_button = gr.Button("Generate")
232
+
233
  output_html = gr.HTML()
234
  animate_button = gr.Button("Trigger Movement")
235
+
 
236
  submit_button.click(process_text, inputs=input_text, outputs=output_html)
237
  animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
238
 
 
 
 
 
 
 
 
 
 
 
 
239
  # Launch the app
240
  demo.launch()