circulartext commited on
Commit
2309f60
·
verified ·
1 Parent(s): 833c324

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +270 -0
app.py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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) # Reduced max_length for faster inference
7
+
8
+ # Predefined words to check
9
+ SPECIAL_WORDS = [
10
+ 'movie', 'excited', 'waiting', 'long', 'time', 'production', 'real', 'coded', 'digital', 'favorite',
11
+ 'asking', 'doing', 'basketball', 'soccer', 'football', 'baseball', 'soup', 'food', 'burgers', 'pizza',
12
+ 'fruit', 'pineapple', 'milk', 'jello', 'candy', 'rice', 'greens', 'lettuce', 'oatmeal', 'cereal',
13
+ 'dogs', 'cats', 'animals', 'goats', 'sheep', 'movies', 'money', 'bank', 'account', 'keeping',
14
+ 'looking', 'moving', 'boxes', 'elephants', 'movement', 'coding', 'developing', 'going', 'cruise',
15
+ 'ship', 'boat', 'bahamas', 'foods', 'healthy', 'eating', 'important', 'pennsylvania', 'atlanta',
16
+ 'north carolina', 'new york', 'france', 'paris', 'work', 'jobs', 'computers', 'computer', 'grocery',
17
+ 'glamorous', 'version', 'truck', 'pickup', 'play', 'types', 'games', 'applications', 'quantum',
18
+ 'speeds', 'advancements', 'technological', 'glimpse', 'countless', 'technology', 'future', 'walking',
19
+ 'hello', 'jordan', 'season', 'superstar', 'nba', 'championship', 'leading', 'points', 'assist',
20
+ 'career', 'chicago', 'scared', 'tongue', 'energy', 'disguise', 'business', 'older', 'grown', 'call',
21
+ 'bills', 'garden', 'house', 'fallen', 'blossoms', 'lawn', 'love', 'forever', 'most', 'fan', 'clout',
22
+ 'space', 'team', 'today', 'woke', 'work', 'relax', 'quicker', 'thicker', 'richer', 'data', 'ballet',
23
+ 'dancer', 'goat', 'post', 'lebron', 'james', 'eagles', 'rockets', 'times', 'tank', 'pencil', 'watch',
24
+ 'rolex', 'rappers', 'rockstar', 'rocket', 'rocks', 'tooth', 'teeth', 'pancake', 'breakfast', 'lunch',
25
+ 'dinner', 'zoom', 'calling', 'talking', 'rule', 'ruler', 'rick', 'morty', 'martin', 'smith', 'wild',
26
+ 'track', 'field', 'touchdown', 'basket', 'hope', 'yours', 'thank', 'olympics', 'sports', 'help',
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",
39
+ "'Rajdhani', sans-serif",
40
+ "'Anton', sans-serif",
41
+ "'Caveat', cursive",
42
+ "'Patrick Hand', cursive",
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",
59
+ "1px 0px 0px",
60
+ "0px 0px 0px",
61
+ "0px 1px 0px",
62
+ "0px 2px 0px",
63
+ "0px 1px 1px",
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()])
169
+ styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
170
+ styled_letters.append(styled_letter)
171
+
172
+ return f'''
173
+ <style>
174
+ {keyframes}
175
+ .styled-letter {{
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>
211
+ <head>
212
+ <link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet">
213
+ <link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@100&display=swap" rel="stylesheet">
214
+ <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@300&display=swap" rel="stylesheet">
215
+ <link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
216
+ <link href="https://fonts.googleapis.com/css2?family=Caveat&display=swap" rel="stylesheet">
217
+ <link href="https://fonts.googleapis.com/css2?family=Patrick+Hand&display=swap" rel="stylesheet">
218
+ <link href="https://fonts.googleapis.com/css2?family=Nothing+You+Could+Do&display=swap" rel="stylesheet">
219
+ <link href="https://fonts.googleapis.com/css2?family=Reenie+Beanie&display=swap" rel="stylesheet">
220
+ <link href="https://fonts.googleapis.com/css2?family=Orbitron&display=swap" rel="stylesheet">
221
+ <link href="https://fonts.googleapis.com/css?family=Raleway:500" rel="stylesheet">
222
+ <style>
223
+ body {{
224
+ background-color: #fff;
225
+ color: #000;
226
+ font-size: 18px;
227
+ line-height: 1.6;
228
+ font-family: "Josefin Sans", sans-serif;
229
+ padding: 20px;
230
+ }}
231
+ </style>
232
+ </head>
233
+ <body>
234
+ <div style='max-width: 800px; margin: auto;'>
235
+ {output_html}
236
+ </div>
237
+ </body>
238
+ </html>
239
+ """
240
+
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
256
+ with gr.Blocks() as demo:
257
+ gr.Markdown("# CircularText Styler\nEnter a prompt to generate text with special word styling.")
258
+
259
+ with gr.Row():
260
+ input_text = gr.Textbox(label="Input Prompt")
261
+ submit_button = gr.Button("Generate")
262
+
263
+ output_html = gr.HTML()
264
+ animate_button = gr.Button("Trigger Movement")
265
+
266
+ submit_button.click(process_text, inputs=input_text, outputs=output_html)
267
+ animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
268
+
269
+ # Launch the app
270
+ demo.launch()