circulartext commited on
Commit
4ee7b46
·
verified ·
1 Parent(s): 3b82090

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +239 -12
app.py CHANGED
@@ -1,7 +1,203 @@
1
- def process_text(input_text):
2
- global initial_word_design, special_word
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- # Generate text with more tokens
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  generated = generator(
6
  input_text,
7
  max_new_tokens=MAX_NEW_TOKENS,
@@ -12,17 +208,17 @@ def process_text(input_text):
12
 
13
  generated_text = generated[0]['generated_text']
14
  generated_text = generated_text[:1000]
15
-
16
  words = generated_text.split()
17
-
 
18
  if len(words) >= 2:
19
- # Mark last two words as special
20
- for i in range(-2, 0):
21
- clean_word = ''.join(filter(str.isalnum, words[i])).lower()
22
- if clean_word in SPECIAL_WORDS:
23
- special_word = words[i]
24
- initial_word_design = generate_initial_design(words[i])
25
- words[i] = initial_word_design
26
 
27
  output_html = ' '.join(words)
28
 
@@ -59,3 +255,34 @@ def process_text(input_text):
59
  """
60
 
61
  return final_output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ from transformers import pipeline
4
+
5
+ # Choose a larger model
6
+ MODEL_NAME = "gpt2-medium"
7
+ MAX_NEW_TOKENS = 150
8
+
9
+ # Load the model once when the app starts
10
+ generator = pipeline(
11
+ "text-generation",
12
+ model=MODEL_NAME,
13
+ device_map="auto",
14
+ torch_dtype="auto"
15
+ )
16
+
17
+ SPECIAL_WORDS = [
18
+ 'movie', 'excited', 'waiting', 'long', 'time', 'production', 'real', 'coded', 'digital', 'favorite',
19
+ 'asking', 'doing', 'basketball', 'soccer', 'football', 'baseball', 'soup', 'food', 'burgers', 'pizza',
20
+ 'fruit', 'pineapple', 'milk', 'jello', 'candy', 'rice', 'greens', 'lettuce', 'oatmeal', 'cereal',
21
+ 'dogs', 'cats', 'animals', 'goats', 'sheep', 'movies', 'money', 'bank', 'account', 'keeping',
22
+ 'looking', 'moving', 'boxes', 'elephants', 'movement', 'coding', 'developing', 'going', 'cruise',
23
+ 'ship', 'boat', 'bahamas', 'foods', 'healthy', 'eating', 'important', 'pennsylvania', 'atlanta',
24
+ 'north carolina', 'new york', 'france', 'paris', 'work', 'jobs', 'computers', 'computer', 'grocery',
25
+ 'glamorous', 'version', 'truck', 'pickup', 'play', 'types', 'games', 'applications', 'quantum',
26
+ 'speeds', 'advancements', 'technological', 'glimpse', 'countless', 'technology', 'future', 'walking',
27
+ 'hello', 'jordan', 'season', 'superstar', 'nba', 'championship', 'leading', 'points', 'assist',
28
+ 'career', 'chicago', 'scared', 'tongue', 'energy', 'disguise', 'business', 'older', 'grown', 'call',
29
+ 'bills', 'garden', 'house', 'fallen', 'blossoms', 'lawn', 'love', 'forever', 'most', 'fan', 'clout',
30
+ 'space', 'team', 'today', 'woke', 'work', 'relax', 'quicker', 'thicker', 'richer', 'data', 'ballet',
31
+ 'dancer', 'goat', 'post', 'lebron', 'james', 'eagles', 'rockets', 'times', 'tank', 'pencil', 'watch',
32
+ 'rolex', 'rappers', 'rockstar', 'rocket', 'rocks', 'tooth', 'teeth', 'pancake', 'breakfast', 'lunch',
33
+ 'dinner', 'zoom', 'calling', 'talking', 'rule', 'ruler', 'rick', 'morty', 'martin', 'smith', 'wild',
34
+ 'track', 'field', 'touchdown', 'basket', 'hope', 'yours', 'thank', 'olympics', 'sports', 'help',
35
+ 'legal', 'law', 'firm', 'crowd', 'winner', 'winter', 'smoking', 'green', 'purple', 'blue', 'pink',
36
+ 'orange', 'black', 'white', 'yellow', 'gold', 'weather', 'sun', 'middle', 'summer', 'heat', 'spring',
37
+ 'travel', 'explore', 'adventure', 'journey', 'vacation', 'holiday', 'destination', 'airlines', 'flight',
38
+ 'tickets', 'booking', 'passport', 'visa', 'customs', 'luggage', 'boarding', 'hotel', 'accommodation',
39
+ 'resort', 'beach', 'mountains', 'forest', 'desert', 'safari', 'wildlife', 'nature', 'beauty', 'landscape',
40
+ 'view', 'sightseeing', 'tourism', 'culture', 'heritage', 'tradition', 'festival', 'celebration', 'nightlife',
41
+ 'entertainment', 'show', 'concert', 'music', 'art', 'gallery', 'museum', 'exhibit', 'performance',
42
+ 'audience', 'theater', 'cinema', 'director', 'actor', 'actress', 'casting', 'scene', 'script', 'dialogue',
43
+ 'surreal', 'abstract', 'realistic', 'animation', 'character', 'hero', 'villain', 'plot', 'twist', 'climax',
44
+ 'resolution', 'conflict', 'tension', 'suspense', 'thrill', 'horror', 'mystery', 'drama', 'comedy', 'romance',
45
+ 'action', 'adventure', 'sci-fi', 'fantasy', 'historical', 'documentary', 'satire', 'parody', 'spoof'
46
+ ]
47
+
48
+ # Global variables
49
+ initial_word_design = ""
50
+ special_words = []
51
+
52
+ def generate_initial_design(word):
53
+ fonts = [
54
+ "'VT323', monospace",
55
+ "'Josefin Sans', sans-serif",
56
+ "'Rajdhani', sans-serif",
57
+ "'Anton', sans-serif",
58
+ "'Caveat', cursive",
59
+ "'Patrick Hand', cursive",
60
+ "'Nothing You Could Do', cursive",
61
+ "'Reenie Beanie', cursive",
62
+ "'Orbitron', sans-serif",
63
+ "'Raleway', sans-serif"
64
+ ]
65
+ font_sizes = ["18px", "19px", "20px"]
66
+ font_tops = ["0px", "1px", "-1px"]
67
+ letter_spacings = ["-1px", "0px", "1px"]
68
+ text_shadows = [
69
+ "0px 0px 1px",
70
+ "0px 0px 2px",
71
+ "1px 0px 0px",
72
+ "0px 0px 0px",
73
+ "0px 1px 0px",
74
+ "0px 2px 0px",
75
+ "0px 1px 1px",
76
+ "1px 1px 0px",
77
+ "1px 0px 1px"
78
+ ]
79
+ skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
80
+
81
+ letters = list(word)
82
+ styled_letters = []
83
 
84
+ for i, letter in enumerate(letters):
85
+ style = {
86
+ 'font-family': random.choice(fonts),
87
+ 'line-height': '1.6',
88
+ 'font-size': random.choice(font_sizes),
89
+ 'letter-spacing': random.choice(letter_spacings),
90
+ 'text-shadow': random.choice(text_shadows),
91
+ 'transform': f'skew({random.choice(skew_angles)})',
92
+ 'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
93
+ 'position': 'relative',
94
+ 'top': random.choice(font_tops),
95
+ 'color': '#000000',
96
+ 'display': 'inline-block',
97
+ 'margin': '0 1px',
98
+ 'vertical-align': 'middle'
99
+ }
100
+
101
+ style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
102
+ styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
103
+ styled_letters.append(styled_letter)
104
+
105
+ return f'''
106
+ <span style="display: inline-flex;
107
+ align-items: baseline;
108
+ vertical-align: middle;
109
+ margin: 0 2px;">
110
+ {" ".join(styled_letters)}
111
+ </span>'''
112
+
113
+ def generate_movement_design(words):
114
+ fonts = [
115
+ "'VT323', monospace",
116
+ "'Josefin Sans', sans-serif",
117
+ "'Rajdhani', sans-serif",
118
+ "'Anton', sans-serif",
119
+ "'Caveat', cursive",
120
+ "'Patrick Hand', cursive",
121
+ "'Nothing You Could Do', cursive",
122
+ "'Reenie Beanie', cursive",
123
+ "'Orbitron', sans-serif",
124
+ "'Raleway', sans-serif"
125
+ ]
126
+ font_sizes = ["18px", "19px", "20px"]
127
+ font_tops = ["0px", "1px", "-1px"]
128
+ letter_spacings = ["-1px", "0px", "1px"]
129
+ text_shadows = [
130
+ "0px 0px 1px",
131
+ "0px 0px 2px",
132
+ "1px 0px 0px",
133
+ "0px 0px 0px",
134
+ "0px 1px 0px",
135
+ "0px 2px 0px",
136
+ "0px 1px 1px",
137
+ "1px 1px 0px",
138
+ "1px 0px 1px"
139
+ ]
140
+ skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
141
+
142
+ random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
143
+ animation_name = f"animate_{random.randint(0, 10000)}"
144
+
145
+ keyframes = f"""
146
+ @keyframes {animation_name} {{
147
+ 0% {{ transform: scale(1) rotate(0deg); }}
148
+ 50% {{ transform: scale(1.2) rotate(10deg); }}
149
+ 100% {{ transform: scale(1) rotate(0deg); }}
150
+ }}
151
+ """
152
+
153
+ styled_words = []
154
+ for word in words:
155
+ letters = list(word)
156
+ styled_letters = []
157
+
158
+ for i, letter in enumerate(letters):
159
+ style = {
160
+ 'font-family': random.choice(fonts),
161
+ 'line-height': '1.6',
162
+ 'font-size': random.choice(font_sizes),
163
+ 'letter-spacing': random.choice(letter_spacings),
164
+ 'text-shadow': random.choice(text_shadows),
165
+ 'transform': f'skew({random.choice(skew_angles)})',
166
+ 'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
167
+ 'position': 'relative',
168
+ 'top': random.choice(font_tops),
169
+ 'color': random_color,
170
+ 'display': 'inline-block',
171
+ 'margin': '0 1px',
172
+ 'vertical-align': 'middle',
173
+ 'animation': f'{animation_name} 0.5s ease-in-out',
174
+ 'animation-delay': f'{i * 0.1}s'
175
+ }
176
+
177
+ style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
178
+ styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
179
+ styled_letters.append(styled_letter)
180
+
181
+ styled_words.append("".join(styled_letters))
182
+
183
+ return f'''
184
+ <style>
185
+ {keyframes}
186
+ .styled-letter {{
187
+ transition: all 0.3s ease;
188
+ }}
189
+ </style>
190
+ <span style="display: inline-flex;
191
+ align-items: baseline;
192
+ vertical-align: middle;
193
+ margin: 0 2px;">
194
+ {" ".join(styled_words)}
195
+ </span>
196
+ '''
197
+
198
+ def process_text(input_text):
199
+ global initial_word_design, special_words
200
+
201
  generated = generator(
202
  input_text,
203
  max_new_tokens=MAX_NEW_TOKENS,
 
208
 
209
  generated_text = generated[0]['generated_text']
210
  generated_text = generated_text[:1000]
 
211
  words = generated_text.split()
212
+
213
+ special_words = []
214
  if len(words) >= 2:
215
+ special_words = [words[-2], words[-1]] # Get last two words
216
+
217
+ for word in special_words:
218
+ clean_word = ''.join(filter(str.isalnum, word)).lower()
219
+ if clean_word in SPECIAL_WORDS:
220
+ initial_word_design = generate_initial_design(word)
221
+ words[words.index(word)] = initial_word_design
222
 
223
  output_html = ' '.join(words)
224
 
 
255
  """
256
 
257
  return final_output
258
+
259
+ def trigger_movement(input_html):
260
+ global initial_word_design, special_words
261
+
262
+ if not initial_word_design or not special_words:
263
+ return input_html
264
+
265
+ movement_design = generate_movement_design(special_words)
266
+ updated_html = input_html.replace(initial_word_design, movement_design, 1)
267
+
268
+ return updated_html
269
+
270
+ with gr.Blocks() as demo:
271
+ gr.Markdown("# CircularText Styler\nEnter a prompt to generate text with special word styling.")
272
+
273
+ with gr.Row():
274
+ input_text = gr.Textbox(label="Input Prompt")
275
+ submit_button = gr.Button("Generate")
276
+
277
+ output_html = gr.HTML()
278
+ animate_button = gr.Button("Trigger Movement")
279
+
280
+ submit_button.click(
281
+ fn=process_text,
282
+ inputs=input_text,
283
+ outputs=output_html,
284
+ show_progress=True # Enable a basic loading indicator
285
+ )
286
+ animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
287
+
288
+ demo.launch()