circulartext commited on
Commit
c85452c
·
verified ·
1 Parent(s): e8ff1c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -31
app.py CHANGED
@@ -3,7 +3,7 @@ import random
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)
7
 
8
  # Predefined words to check
9
  SPECIAL_WORDS = [
@@ -27,8 +27,12 @@ SPECIAL_WORDS = [
27
  'legal', 'law', 'firm', 'crowd', 'winner', 'winter', 'smoking', 'green', 'purple', 'blue', 'pink',
28
  'orange', 'black', 'white', 'yellow', 'gold', 'weather', 'sun', 'middle', 'summer', 'heat', 'spring'
29
  ]
30
- def generate_design(word, color='#000000', animate=False):
31
- """Generate design for the special word."""
 
 
 
 
32
  fonts = [
33
  "'VT323', monospace",
34
  "'Josefin Sans', sans-serif",
@@ -39,10 +43,16 @@ def generate_design(word, color='#000000', animate=False):
39
  "'Nothing You Could Do', cursive",
40
  "'Reenie Beanie', cursive",
41
  "'Orbitron', sans-serif",
42
- "'Raleway', sans-serif"
 
 
 
 
 
43
  ]
44
- font_sizes = ["18px", "19px", "20px"]
45
- letter_spacings = ["-1px", "0px", "1px"]
 
46
  text_shadows = [
47
  "0px 0px 1px",
48
  "0px 0px 2px",
@@ -54,34 +64,105 @@ def generate_design(word, color='#000000', animate=False):
54
  "1px 1px 0px",
55
  "1px 0px 1px"
56
  ]
57
- skew_angles = ["-10deg", "0deg", "10deg"]
58
 
59
  letters = list(word)
60
  styled_letters = []
61
 
62
- animation_name = f"animate_{random.randint(0, 10000)}" if animate else ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  keyframes = f"""
64
  @keyframes {animation_name} {{
65
  0% {{ transform: scale(1) rotate(0deg); }}
66
  50% {{ transform: scale(1.2) rotate(10deg); }}
67
  100% {{ transform: scale(1) rotate(0deg); }}
68
  }}
69
- """ if animate else ""
70
 
 
 
 
71
  for i, letter in enumerate(letters):
72
  style = {
73
  'font-family': random.choice(fonts),
74
- 'line-height': '1.6',
75
  'font-size': random.choice(font_sizes),
76
  'letter-spacing': random.choice(letter_spacings),
77
  'text-shadow': random.choice(text_shadows),
78
  'transform': f'skew({random.choice(skew_angles)})',
79
- 'color': color,
 
 
 
80
  'display': 'inline-block',
81
  'margin': '0 1px',
82
  'vertical-align': 'middle',
83
- 'animation': f'{animation_name} 0.5s ease-in-out' if animate else '',
84
- 'animation-delay': f'{i * 0.1}s' if animate else ''
85
  }
86
 
87
  style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
@@ -95,28 +176,35 @@ def generate_design(word, color='#000000', animate=False):
95
  transition: all 0.3s ease;
96
  }}
97
  </style>
98
- <span style="display: inline-flex; align-items: baseline; vertical-align: middle; margin: 0 2px;">
 
 
 
99
  {" ".join(styled_letters)}
100
  </span>
101
  '''
102
 
103
  def process_text(input_text):
104
  """Process text and generate the initial output with special word styled in black."""
 
 
 
105
  generated = generator(input_text, num_return_sequences=1)
106
  generated_text = generated[0]['generated_text']
107
- generated_text = generated_text[:200]
108
 
109
  words = generated_text.split()
110
- processed_words = []
111
-
112
- for word in words:
113
  clean_word = ''.join(filter(str.isalnum, word)).lower()
114
  if clean_word in SPECIAL_WORDS:
115
- processed_words.append(generate_design(word))
 
 
116
  else:
117
- processed_words.append(word)
118
 
119
- output_html = ' '.join(processed_words)
120
 
121
  final_output = f"""
122
  <html>
@@ -153,18 +241,15 @@ def process_text(input_text):
153
  return final_output
154
 
155
  def trigger_movement(input_html):
156
- """Function to trigger the movement animation for all special words."""
157
- words = input_html.split()
158
- updated_words = []
 
 
159
 
160
- for word in words:
161
- clean_word = ''.join(filter(str.isalnum, word)).lower()
162
- if clean_word in SPECIAL_WORDS:
163
- updated_words.append(generate_design(clean_word, color=f'#{random.randint(0, 0xFFFFFF):06x}', animate=True))
164
- else:
165
- updated_words.append(word)
166
 
167
- updated_html = ' '.join(updated_words)
168
  return updated_html
169
 
170
  # Create Gradio interface using Blocks
 
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 = [
 
27
  'legal', 'law', 'firm', 'crowd', 'winner', 'winter', 'smoking', 'green', 'purple', 'blue', 'pink',
28
  'orange', 'black', 'white', 'yellow', 'gold', 'weather', 'sun', 'middle', 'summer', 'heat', 'spring'
29
  ]
30
+ # Global variables
31
+ initial_word_design = ""
32
+ special_word = ""
33
+
34
+ def generate_initial_design(word):
35
+ """Generate initial design for the special word in black color."""
36
  fonts = [
37
  "'VT323', monospace",
38
  "'Josefin Sans', sans-serif",
 
43
  "'Nothing You Could Do', cursive",
44
  "'Reenie Beanie', cursive",
45
  "'Orbitron', sans-serif",
46
+ "'Raleway', sans-serif",
47
+ "'Open Sans Condensed', sans-serif",
48
+ "'Poiret One', cursive",
49
+ "'Indie Flower', cursive",
50
+ "'Pacifico', cursive",
51
+ "'Teko', sans-serif"
52
  ]
53
+ font_sizes = ["18px", "19px", "20px"] # Narrower range
54
+ font_tops = ["0px", "1px", "-1px"] # Smaller adjustments
55
+ letter_spacings = ["-1px", "0px", "1px"] # Reduced range
56
  text_shadows = [
57
  "0px 0px 1px",
58
  "0px 0px 2px",
 
64
  "1px 1px 0px",
65
  "1px 0px 1px"
66
  ]
67
+ skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
68
 
69
  letters = list(word)
70
  styled_letters = []
71
 
72
+ for i, letter in enumerate(letters):
73
+ style = {
74
+ 'font-family': random.choice(fonts),
75
+ 'line-height': '1.6', # Consistent with body text
76
+ 'font-size': random.choice(font_sizes),
77
+ 'letter-spacing': random.choice(letter_spacings),
78
+ 'text-shadow': random.choice(text_shadows),
79
+ 'transform': f'skew({random.choice(skew_angles)})',
80
+ 'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]), # Reduced range
81
+ 'position': 'relative',
82
+ 'top': random.choice(font_tops),
83
+ 'color': '#000000',
84
+ 'display': 'inline-block',
85
+ 'margin': '0 1px',
86
+ 'vertical-align': 'middle'
87
+ }
88
+
89
+ style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
90
+ styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
91
+ styled_letters.append(styled_letter)
92
+
93
+ return f'''
94
+ <span style="display: inline-flex;
95
+ align-items: baseline;
96
+ vertical-align: middle;
97
+ margin: 0 2px;">
98
+ {" ".join(styled_letters)}
99
+ </span>'''
100
+
101
+ def generate_movement_design(word):
102
+ """Generate a completely new random design for the movement animation."""
103
+ fonts = [
104
+ "'VT323', monospace",
105
+ "'Josefin Sans', sans-serif",
106
+ "'Rajdhani', sans-serif",
107
+ "'Anton', sans-serif",
108
+ "'Caveat', cursive",
109
+ "'Patrick Hand', cursive",
110
+ "'Nothing You Could Do', cursive",
111
+ "'Reenie Beanie', cursive",
112
+ "'Orbitron', sans-serif",
113
+ "'Raleway', sans-serif"
114
+ ]
115
+ font_sizes = ["18px", "19px", "20px"] # Narrower range
116
+ font_tops = ["0px", "1px", "-1px"] # Smaller adjustments
117
+ letter_spacings = ["-1px", "0px", "1px"] # Reduced range
118
+ text_shadows = [
119
+ "0px 0px 1px",
120
+ "0px 0px 2px",
121
+ "1px 0px 0px",
122
+ "0px 0px 0px",
123
+ "0px 1px 0px",
124
+ "0px 2px 0px",
125
+ "0px 1px 1px",
126
+ "1px 1px 0px",
127
+ "1px 0px 1px"
128
+ ]
129
+ skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
130
+
131
+ # Generate random color for the movement design
132
+ random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
133
+
134
+ # Generate unique animation name
135
+ animation_name = f"animate_{random.randint(0, 10000)}"
136
+
137
+ # Create keyframes for the animation sequence
138
  keyframes = f"""
139
  @keyframes {animation_name} {{
140
  0% {{ transform: scale(1) rotate(0deg); }}
141
  50% {{ transform: scale(1.2) rotate(10deg); }}
142
  100% {{ transform: scale(1) rotate(0deg); }}
143
  }}
144
+ """
145
 
146
+ letters = list(word)
147
+ styled_letters = []
148
+
149
  for i, letter in enumerate(letters):
150
  style = {
151
  'font-family': random.choice(fonts),
152
+ 'line-height': '1.6', # Consistent with body text
153
  'font-size': random.choice(font_sizes),
154
  'letter-spacing': random.choice(letter_spacings),
155
  'text-shadow': random.choice(text_shadows),
156
  'transform': f'skew({random.choice(skew_angles)})',
157
+ 'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]), # Reduced range
158
+ 'position': 'relative',
159
+ 'top': random.choice(font_tops),
160
+ 'color': random_color,
161
  'display': 'inline-block',
162
  'margin': '0 1px',
163
  'vertical-align': 'middle',
164
+ 'animation': f'{animation_name} 0.5s ease-in-out',
165
+ 'animation-delay': f'{i * 0.1}s'
166
  }
167
 
168
  style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
 
176
  transition: all 0.3s ease;
177
  }}
178
  </style>
179
+ <span style="display: inline-flex;
180
+ align-items: baseline;
181
+ vertical-align: middle;
182
+ margin: 0 2px;">
183
  {" ".join(styled_letters)}
184
  </span>
185
  '''
186
 
187
  def process_text(input_text):
188
  """Process text and generate the initial output with special word styled in black."""
189
+ global initial_word_design, special_word
190
+
191
+ # Generate text with limited length
192
  generated = generator(input_text, num_return_sequences=1)
193
  generated_text = generated[0]['generated_text']
194
+ generated_text = generated_text[:200] # Limit output length
195
 
196
  words = generated_text.split()
197
+
198
+ for i, word in enumerate(words):
 
199
  clean_word = ''.join(filter(str.isalnum, word)).lower()
200
  if clean_word in SPECIAL_WORDS:
201
+ special_word = word
202
+ initial_word_design = generate_initial_design(word)
203
+ words[i] = initial_word_design
204
  else:
205
+ words[i] = word
206
 
207
+ output_html = ' '.join(words)
208
 
209
  final_output = f"""
210
  <html>
 
241
  return final_output
242
 
243
  def trigger_movement(input_html):
244
+ """Function to trigger the movement animation."""
245
+ global initial_word_design, special_word
246
+
247
+ if not initial_word_design or not special_word:
248
+ return input_html
249
 
250
+ movement_design = generate_movement_design(special_word)
251
+ updated_html = input_html.replace(initial_word_design, movement_design, 1)
 
 
 
 
252
 
 
253
  return updated_html
254
 
255
  # Create Gradio interface using Blocks