circulartext commited on
Commit
c9a51b3
·
verified ·
1 Parent(s): 805fc47

Create app.py

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