Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,15 @@ from transformers import pipeline
|
|
| 6 |
SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat']
|
| 7 |
|
| 8 |
def generate_circular_text_design(word):
|
| 9 |
-
"""Generate a consistent but randomized text design for a word"""
|
| 10 |
# Controlled randomization parameters
|
| 11 |
font_sizes = [18, 20, 22, 24, 26]
|
| 12 |
letter_spacings = [-3, -2, -4]
|
| 13 |
skew_angles = [-25, -20, -15, -10, 0, 10, 15, 20, 25]
|
| 14 |
-
|
| 15 |
-
# Random single color
|
| 16 |
base_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
| 17 |
-
|
| 18 |
# Fonts with a controlled set
|
| 19 |
fonts = [
|
| 20 |
"'Dosis', sans-serif",
|
|
@@ -22,11 +22,32 @@ def generate_circular_text_design(word):
|
|
| 22 |
"'Orbitron', sans-serif",
|
| 23 |
"'Roboto Condensed', sans-serif"
|
| 24 |
]
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
# Create HTML for each letter with dynamic styling
|
| 27 |
letters = list(word)
|
| 28 |
styled_letters = []
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
for i, letter in enumerate(letters):
|
| 31 |
style = {
|
| 32 |
'font-family': random.choice(fonts),
|
|
@@ -34,56 +55,46 @@ def generate_circular_text_design(word):
|
|
| 34 |
'font-size': f'{random.choice(font_sizes)}px',
|
| 35 |
'letter-spacing': f'{random.choice(letter_spacings)}px',
|
| 36 |
'text-shadow': '1px 2px 1px',
|
| 37 |
-
'transform': f'skew({random.choice(skew_angles)}deg
|
| 38 |
'margin-top': f'{random.uniform(-0.1, 0.1):.2f}cm',
|
| 39 |
'position': 'relative',
|
| 40 |
'top': f'{random.randint(3, 11)}px',
|
| 41 |
-
'color': base_color
|
|
|
|
| 42 |
}
|
| 43 |
-
|
| 44 |
# Convert style to inline CSS
|
| 45 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
| 46 |
-
|
| 47 |
-
styled_letter = f'<div
|
| 48 |
styled_letters.append(styled_letter)
|
| 49 |
-
|
| 50 |
-
# Wrap styled letters in a flex container
|
| 51 |
-
|
| 52 |
-
<
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
}}, {100 * (i+1)});
|
| 60 |
-
setTimeout(() => {{
|
| 61 |
-
document.getElementById('ddiv{i+1}').style.transform = 'rotate(0deg) scale(1)';
|
| 62 |
-
document.getElementById('ddiv{i+1}').style.fontSize = '{random.choice(font_sizes)}px';
|
| 63 |
-
}}, {500 + 100 * (i+1)});
|
| 64 |
-
""" for i in range(len(letters))])}
|
| 65 |
-
}}
|
| 66 |
-
window.onload = timedText2x;
|
| 67 |
-
</script>
|
| 68 |
-
"""
|
| 69 |
-
|
| 70 |
-
return f'<div style="display: flex; align-items: baseline;">{" ".join(styled_letters)}</div>{movement_script}'
|
| 71 |
|
| 72 |
def process_text(input_text):
|
| 73 |
"""Process text and apply special styling to matching words"""
|
| 74 |
# Use inference model
|
| 75 |
generator = pipeline('text-generation', model='gpt2')
|
| 76 |
-
generated_text = generator(input_text, max_length=
|
| 77 |
-
|
| 78 |
# Split text into words
|
| 79 |
words = generated_text.split()
|
| 80 |
-
|
| 81 |
# Check for special words
|
| 82 |
for i, word in enumerate(words):
|
| 83 |
-
if
|
|
|
|
| 84 |
# Apply circular text design
|
| 85 |
words[i] = generate_circular_text_design(word)
|
| 86 |
-
|
| 87 |
# Combine words with HTML
|
| 88 |
output_html = f"""
|
| 89 |
<html>
|
|
@@ -102,24 +113,35 @@ def process_text(input_text):
|
|
| 102 |
height: 100vh;
|
| 103 |
margin: 0;
|
| 104 |
font-family: 'Josefin Sans', sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
}}
|
| 106 |
</style>
|
| 107 |
</head>
|
| 108 |
<body>
|
| 109 |
-
<h1>{
|
| 110 |
</body>
|
| 111 |
</html>
|
| 112 |
"""
|
| 113 |
-
|
| 114 |
return output_html
|
| 115 |
|
| 116 |
# Create Gradio interface
|
| 117 |
iface = gr.Interface(
|
| 118 |
fn=process_text,
|
| 119 |
inputs="text",
|
| 120 |
-
outputs=gr.HTML(),
|
| 121 |
title="Circular Text Styler",
|
| 122 |
-
description="Enter a prompt to generate text with special word styling"
|
| 123 |
)
|
| 124 |
|
| 125 |
# Launch the interface
|
|
|
|
| 6 |
SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat']
|
| 7 |
|
| 8 |
def generate_circular_text_design(word):
|
| 9 |
+
"""Generate a consistent but randomized text design for a word using CSS animations"""
|
| 10 |
# Controlled randomization parameters
|
| 11 |
font_sizes = [18, 20, 22, 24, 26]
|
| 12 |
letter_spacings = [-3, -2, -4]
|
| 13 |
skew_angles = [-25, -20, -15, -10, 0, 10, 15, 20, 25]
|
| 14 |
+
|
| 15 |
+
# Random single color
|
| 16 |
base_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
| 17 |
+
|
| 18 |
# Fonts with a controlled set
|
| 19 |
fonts = [
|
| 20 |
"'Dosis', sans-serif",
|
|
|
|
| 22 |
"'Orbitron', sans-serif",
|
| 23 |
"'Roboto Condensed', sans-serif"
|
| 24 |
]
|
| 25 |
+
|
| 26 |
+
# Generate a unique animation name for CSS
|
| 27 |
+
animation_name = f"animation_{random.randint(0, 10000)}"
|
| 28 |
+
|
| 29 |
# Create HTML for each letter with dynamic styling
|
| 30 |
letters = list(word)
|
| 31 |
styled_letters = []
|
| 32 |
+
|
| 33 |
+
# Prepare CSS keyframes for animation
|
| 34 |
+
keyframes = f"""
|
| 35 |
+
@keyframes {animation_name} {{
|
| 36 |
+
0% {{
|
| 37 |
+
transform: scale(1) rotate(0deg);
|
| 38 |
+
color: {base_color};
|
| 39 |
+
}}
|
| 40 |
+
50% {{
|
| 41 |
+
transform: scale(1.2) rotate({random.randint(-20, 20)}deg);
|
| 42 |
+
color: #{random.randint(0, 0xFFFFFF):06x};
|
| 43 |
+
}}
|
| 44 |
+
100% {{
|
| 45 |
+
transform: scale(1) rotate(0deg);
|
| 46 |
+
color: {base_color};
|
| 47 |
+
}}
|
| 48 |
+
}}
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
for i, letter in enumerate(letters):
|
| 52 |
style = {
|
| 53 |
'font-family': random.choice(fonts),
|
|
|
|
| 55 |
'font-size': f'{random.choice(font_sizes)}px',
|
| 56 |
'letter-spacing': f'{random.choice(letter_spacings)}px',
|
| 57 |
'text-shadow': '1px 2px 1px',
|
| 58 |
+
'transform': f'skew({random.choice(skew_angles)}deg)',
|
| 59 |
'margin-top': f'{random.uniform(-0.1, 0.1):.2f}cm',
|
| 60 |
'position': 'relative',
|
| 61 |
'top': f'{random.randint(3, 11)}px',
|
| 62 |
+
'color': base_color,
|
| 63 |
+
'animation': f'{animation_name} 2s ease-in-out infinite',
|
| 64 |
}
|
| 65 |
+
|
| 66 |
# Convert style to inline CSS
|
| 67 |
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
| 68 |
+
|
| 69 |
+
styled_letter = f'<div class="CIRCDosis_SZ18_SP-3_SH1_SK0_MA0" style="{style_str}">{letter}</div>'
|
| 70 |
styled_letters.append(styled_letter)
|
| 71 |
+
|
| 72 |
+
# Wrap styled letters in a flex container and include keyframes in a style tag
|
| 73 |
+
return f'''
|
| 74 |
+
<style>
|
| 75 |
+
{keyframes}
|
| 76 |
+
</style>
|
| 77 |
+
<div style="display: flex; align-items: baseline;">
|
| 78 |
+
{" ".join(styled_letters)}
|
| 79 |
+
</div>
|
| 80 |
+
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def process_text(input_text):
|
| 83 |
"""Process text and apply special styling to matching words"""
|
| 84 |
# Use inference model
|
| 85 |
generator = pipeline('text-generation', model='gpt2')
|
| 86 |
+
generated_text = generator(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
| 87 |
+
|
| 88 |
# Split text into words
|
| 89 |
words = generated_text.split()
|
| 90 |
+
|
| 91 |
# Check for special words
|
| 92 |
for i, word in enumerate(words):
|
| 93 |
+
clean_word = ''.join(e for e in word if e.isalnum()).lower()
|
| 94 |
+
if clean_word in SPECIAL_WORDS:
|
| 95 |
# Apply circular text design
|
| 96 |
words[i] = generate_circular_text_design(word)
|
| 97 |
+
|
| 98 |
# Combine words with HTML
|
| 99 |
output_html = f"""
|
| 100 |
<html>
|
|
|
|
| 113 |
height: 100vh;
|
| 114 |
margin: 0;
|
| 115 |
font-family: 'Josefin Sans', sans-serif;
|
| 116 |
+
background-color: black;
|
| 117 |
+
color: silver;
|
| 118 |
+
text-align: center;
|
| 119 |
+
padding: 10px;
|
| 120 |
+
box-sizing: border-box;
|
| 121 |
+
}}
|
| 122 |
+
h1 {{
|
| 123 |
+
display: flex;
|
| 124 |
+
align-items: baseline;
|
| 125 |
+
flex-wrap: wrap;
|
| 126 |
+
font-size: 24px;
|
| 127 |
}}
|
| 128 |
</style>
|
| 129 |
</head>
|
| 130 |
<body>
|
| 131 |
+
<h1>{" ".join(words)}</h1>
|
| 132 |
</body>
|
| 133 |
</html>
|
| 134 |
"""
|
| 135 |
+
|
| 136 |
return output_html
|
| 137 |
|
| 138 |
# Create Gradio interface
|
| 139 |
iface = gr.Interface(
|
| 140 |
fn=process_text,
|
| 141 |
inputs="text",
|
| 142 |
+
outputs=gr.HTML(label="Generated Text"),
|
| 143 |
title="Circular Text Styler",
|
| 144 |
+
description="Enter a prompt to generate text with special word styling. If the generated text contains specific words, they'll be styled with dynamic animations."
|
| 145 |
)
|
| 146 |
|
| 147 |
# Launch the interface
|