Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,210 +1,6 @@
|
|
| 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_word = ""
|
| 51 |
-
|
| 52 |
-
def generate_initial_design(word):
|
| 53 |
-
"""Generate initial design for the special word in black color."""
|
| 54 |
-
fonts = [
|
| 55 |
-
"'VT323', monospace",
|
| 56 |
-
"'Josefin Sans', sans-serif",
|
| 57 |
-
"'Rajdhani', sans-serif",
|
| 58 |
-
"'Anton', sans-serif",
|
| 59 |
-
"'Caveat', cursive",
|
| 60 |
-
"'Patrick Hand', cursive",
|
| 61 |
-
"'Nothing You Could Do', cursive",
|
| 62 |
-
"'Reenie Beanie', cursive",
|
| 63 |
-
"'Orbitron', sans-serif",
|
| 64 |
-
"'Raleway', sans-serif",
|
| 65 |
-
"'Open Sans Condensed', sans-serif",
|
| 66 |
-
"'Poiret One', cursive",
|
| 67 |
-
"'Indie Flower', cursive",
|
| 68 |
-
"'Pacifico', cursive",
|
| 69 |
-
"'Teko', sans-serif"
|
| 70 |
-
]
|
| 71 |
-
font_sizes = ["18px", "19px", "20px"]
|
| 72 |
-
font_tops = ["0px", "1px", "-1px"]
|
| 73 |
-
letter_spacings = ["-1px", "0px", "1px"]
|
| 74 |
-
text_shadows = [
|
| 75 |
-
"0px 0px 1px",
|
| 76 |
-
"0px 0px 2px",
|
| 77 |
-
"1px 0px 0px",
|
| 78 |
-
"0px 0px 0px",
|
| 79 |
-
"0px 1px 0px",
|
| 80 |
-
"0px 2px 0px",
|
| 81 |
-
"0px 1px 1px",
|
| 82 |
-
"1px 1px 0px",
|
| 83 |
-
"1px 0px 1px"
|
| 84 |
-
]
|
| 85 |
-
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
| 86 |
-
|
| 87 |
-
letters = list(word)
|
| 88 |
-
styled_letters = []
|
| 89 |
-
|
| 90 |
-
for i, letter in enumerate(letters):
|
| 91 |
-
style = {
|
| 92 |
-
'font-family': random.choice(fonts),
|
| 93 |
-
'line-height': '1.6',
|
| 94 |
-
'font-size': random.choice(font_sizes),
|
| 95 |
-
'letter-spacing': random.choice(letter_spacings),
|
| 96 |
-
'text-shadow': random.choice(text_shadows),
|
| 97 |
-
'transform': f'skew({random.choice(skew_angles)})',
|
| 98 |
-
'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
|
| 99 |
-
'position': 'relative',
|
| 100 |
-
'top': random.choice(font_tops),
|
| 101 |
-
'color': '#000000',
|
| 102 |
-
'display': 'inline-block',
|
| 103 |
-
'margin': '0 1px',
|
| 104 |
-
'vertical-align': 'middle'
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
| 108 |
-
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
| 109 |
-
styled_letters.append(styled_letter)
|
| 110 |
-
|
| 111 |
-
return f'''
|
| 112 |
-
<span style="display: inline-flex;
|
| 113 |
-
align-items: baseline;
|
| 114 |
-
vertical-align: middle;
|
| 115 |
-
margin: 0 2px;">
|
| 116 |
-
{" ".join(styled_letters)}
|
| 117 |
-
</span>'''
|
| 118 |
-
|
| 119 |
-
def generate_movement_design(word):
|
| 120 |
-
"""Generate a new random design for the movement animation."""
|
| 121 |
-
fonts = [
|
| 122 |
-
"'VT323', monospace",
|
| 123 |
-
"'Josefin Sans', sans-serif",
|
| 124 |
-
"'Rajdhani', sans-serif",
|
| 125 |
-
"'Anton', sans-serif",
|
| 126 |
-
"'Caveat', cursive",
|
| 127 |
-
"'Patrick Hand', cursive",
|
| 128 |
-
"'Nothing You Could Do', cursive",
|
| 129 |
-
"'Reenie Beanie', cursive",
|
| 130 |
-
"'Orbitron', sans-serif",
|
| 131 |
-
"'Raleway', sans-serif"
|
| 132 |
-
]
|
| 133 |
-
font_sizes = ["18px", "19px", "20px"]
|
| 134 |
-
font_tops = ["0px", "1px", "-1px"]
|
| 135 |
-
letter_spacings = ["-1px", "0px", "1px"]
|
| 136 |
-
text_shadows = [
|
| 137 |
-
"0px 0px 1px",
|
| 138 |
-
"0px 0px 2px",
|
| 139 |
-
"1px 0px 0px",
|
| 140 |
-
"0px 0px 0px",
|
| 141 |
-
"0px 1px 0px",
|
| 142 |
-
"0px 2px 0px",
|
| 143 |
-
"0px 1px 1px",
|
| 144 |
-
"1px 1px 0px",
|
| 145 |
-
"1px 0px 1px"
|
| 146 |
-
]
|
| 147 |
-
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
| 148 |
-
|
| 149 |
-
# Generate random color for the movement design
|
| 150 |
-
random_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
| 151 |
-
|
| 152 |
-
# Generate unique animation name
|
| 153 |
-
animation_name = f"animate_{random.randint(0, 10000)}"
|
| 154 |
-
|
| 155 |
-
# Create keyframes for the animation sequence
|
| 156 |
-
keyframes = f"""
|
| 157 |
-
@keyframes {animation_name} {{
|
| 158 |
-
0% {{ transform: scale(1) rotate(0deg); }}
|
| 159 |
-
50% {{ transform: scale(1.2) rotate(10deg); }}
|
| 160 |
-
100% {{ transform: scale(1) rotate(0deg); }}
|
| 161 |
-
}}
|
| 162 |
-
"""
|
| 163 |
-
|
| 164 |
-
letters = list(word)
|
| 165 |
-
styled_letters = []
|
| 166 |
-
|
| 167 |
-
for i, letter in enumerate(letters):
|
| 168 |
-
style = {
|
| 169 |
-
'font-family': random.choice(fonts),
|
| 170 |
-
'line-height': '1.6',
|
| 171 |
-
'font-size': random.choice(font_sizes),
|
| 172 |
-
'letter-spacing': random.choice(letter_spacings),
|
| 173 |
-
'text-shadow': random.choice(text_shadows),
|
| 174 |
-
'transform': f'skew({random.choice(skew_angles)})',
|
| 175 |
-
'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
|
| 176 |
-
'position': 'relative',
|
| 177 |
-
'top': random.choice(font_tops),
|
| 178 |
-
'color': random_color,
|
| 179 |
-
'display': 'inline-block',
|
| 180 |
-
'margin': '0 1px',
|
| 181 |
-
'vertical-align': 'middle',
|
| 182 |
-
'animation': f'{animation_name} 0.5s ease-in-out',
|
| 183 |
-
'animation-delay': f'{i * 0.1}s'
|
| 184 |
-
}
|
| 185 |
-
|
| 186 |
-
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
| 187 |
-
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
| 188 |
-
styled_letters.append(styled_letter)
|
| 189 |
-
|
| 190 |
-
return f'''
|
| 191 |
-
<style>
|
| 192 |
-
{keyframes}
|
| 193 |
-
.styled-letter {{
|
| 194 |
-
transition: all 0.3s ease;
|
| 195 |
-
}}
|
| 196 |
-
</style>
|
| 197 |
-
<span style="display: inline-flex;
|
| 198 |
-
align-items: baseline;
|
| 199 |
-
vertical-align: middle;
|
| 200 |
-
margin: 0 2px;">
|
| 201 |
-
{" ".join(styled_letters)}
|
| 202 |
-
</span>
|
| 203 |
-
'''
|
| 204 |
-
|
| 205 |
def process_text(input_text):
|
| 206 |
global initial_word_design, special_word
|
| 207 |
-
|
| 208 |
# Generate text with more tokens
|
| 209 |
generated = generator(
|
| 210 |
input_text,
|
|
@@ -219,14 +15,14 @@ def process_text(input_text):
|
|
| 219 |
|
| 220 |
words = generated_text.split()
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
|
| 231 |
output_html = ' '.join(words)
|
| 232 |
|
|
@@ -263,36 +59,3 @@ def process_text(input_text):
|
|
| 263 |
"""
|
| 264 |
|
| 265 |
return final_output
|
| 266 |
-
|
| 267 |
-
def trigger_movement(input_html):
|
| 268 |
-
global initial_word_design, special_word
|
| 269 |
-
|
| 270 |
-
if not initial_word_design or not special_word:
|
| 271 |
-
return input_html
|
| 272 |
-
|
| 273 |
-
movement_design = generate_movement_design(special_word)
|
| 274 |
-
updated_html = input_html.replace(initial_word_design, movement_design, 1)
|
| 275 |
-
|
| 276 |
-
return updated_html
|
| 277 |
-
|
| 278 |
-
# Create Gradio interface using Blocks
|
| 279 |
-
with gr.Blocks() as demo:
|
| 280 |
-
gr.Markdown("# CircularText Styler\nEnter a prompt to generate text with special word styling.")
|
| 281 |
-
|
| 282 |
-
with gr.Row():
|
| 283 |
-
input_text = gr.Textbox(label="Input Prompt")
|
| 284 |
-
submit_button = gr.Button("Generate")
|
| 285 |
-
|
| 286 |
-
output_html = gr.HTML()
|
| 287 |
-
animate_button = gr.Button("Trigger Movement")
|
| 288 |
-
|
| 289 |
-
submit_button.click(
|
| 290 |
-
fn=process_text,
|
| 291 |
-
inputs=input_text,
|
| 292 |
-
outputs=output_html,
|
| 293 |
-
show_progress=True # Enable the loading indicator
|
| 294 |
-
)
|
| 295 |
-
animate_button.click(trigger_movement, inputs=output_html, outputs=output_html)
|
| 296 |
-
|
| 297 |
-
# Launch the app
|
| 298 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
| 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 |
"""
|
| 60 |
|
| 61 |
return final_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|