Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Predefined words to check
|
| 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 (with some saturation)
|
| 16 |
+
base_color = f'#{random.randint(0, 0xFFFFFF):06x}'
|
| 17 |
+
|
| 18 |
+
# Fonts with a controlled set
|
| 19 |
+
fonts = [
|
| 20 |
+
"'Dosis', sans-serif",
|
| 21 |
+
"'Josefin Sans', sans-serif",
|
| 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),
|
| 33 |
+
'line-height': '138%',
|
| 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, 0deg)',
|
| 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 id="ddiv{i+1}" class="CIRCDosis_SZ18_SP-3_SH1_SK0_MA0" style="{style_str}">{letter}</div>'
|
| 48 |
+
styled_letters.append(styled_letter)
|
| 49 |
+
|
| 50 |
+
# Wrap styled letters in a flex container with movement script
|
| 51 |
+
movement_script = f"""
|
| 52 |
+
<script>
|
| 53 |
+
function timedText2x() {{
|
| 54 |
+
{' '.join([f"""
|
| 55 |
+
setTimeout(() => {{
|
| 56 |
+
document.getElementById('ddiv{i+1}').style.transform = 'rotate({random.randint(-20, 20)}deg) scale(1.2)';
|
| 57 |
+
document.getElementById('ddiv{i+1}').style.fontSize = '{random.choice(font_sizes)+5}px';
|
| 58 |
+
document.getElementById('ddiv{i+1}').style.color = '#{random.randint(0, 0xFFFFFF):06x}';
|
| 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=100)[0]['generated_text']
|
| 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 word.lower() in SPECIAL_WORDS:
|
| 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>
|
| 90 |
+
<head>
|
| 91 |
+
<link href="https://fonts.googleapis.com/css2?family=Dosis&family=Josefin+Sans&family=Orbitron&family=Roboto+Condensed&display=swap" rel="stylesheet">
|
| 92 |
+
<style>
|
| 93 |
+
.CIRCDosis_SZ18_SP-3_SH1_SK0_MA0 {{
|
| 94 |
+
float: left;
|
| 95 |
+
position: relative;
|
| 96 |
+
transition: all 0.3s ease;
|
| 97 |
+
}}
|
| 98 |
+
body {{
|
| 99 |
+
display: flex;
|
| 100 |
+
justify-content: center;
|
| 101 |
+
align-items: center;
|
| 102 |
+
height: 100vh;
|
| 103 |
+
margin: 0;
|
| 104 |
+
font-family: 'Josefin Sans', sans-serif;
|
| 105 |
+
}}
|
| 106 |
+
</style>
|
| 107 |
+
</head>
|
| 108 |
+
<body>
|
| 109 |
+
<h1>{' '.join(words)}</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
|
| 126 |
+
iface.launch()
|