Update app.py
Browse files
app.py
CHANGED
@@ -2,107 +2,134 @@ import gradio as gr
|
|
2 |
import random
|
3 |
import time
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
"
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
"
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
return {
|
38 |
-
'font-family': "'Josefin Sans', sans-serif",
|
39 |
-
'font-size': '16px',
|
40 |
-
'letter-spacing': '0px',
|
41 |
-
'text-shadow': '0px 0px 0px #000000',
|
42 |
-
'transform': 'skew(0deg)',
|
43 |
-
'margin-top': '0cm',
|
44 |
-
'position': 'relative',
|
45 |
-
'top': '0px',
|
46 |
-
'color': '#000000',
|
47 |
-
'display': 'inline-block',
|
48 |
-
'margin': '0 1px',
|
49 |
-
'vertical-align': 'middle',
|
50 |
-
'transition': 'all 0.5s ease' # Smooth transition
|
51 |
-
}
|
52 |
|
53 |
-
def
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
'color': '#000000',
|
64 |
-
'display': 'inline-block',
|
65 |
-
'margin': '0 1px',
|
66 |
-
'vertical-align': 'middle',
|
67 |
-
'transition': 'all 0.5s ease'
|
68 |
-
}
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
99 |
|
|
|
100 |
with gr.Blocks() as demo:
|
101 |
-
|
|
|
|
|
102 |
with gr.Row():
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
107 |
|
108 |
-
|
|
|
|
2 |
import random
|
3 |
import time
|
4 |
|
5 |
+
# Global variables
|
6 |
+
current_text = ""
|
7 |
+
special_words = []
|
8 |
+
special_word_positions = []
|
9 |
|
10 |
+
def generate_random_style(word):
|
11 |
+
"""Generate random styling for a word but keep it black."""
|
12 |
+
fonts = [
|
13 |
+
"'VT323', monospace",
|
14 |
+
"'Josefin Sans', sans-serif",
|
15 |
+
"'Rajdhani', sans-serif",
|
16 |
+
"'Anton', sans-serif",
|
17 |
+
"'Caveat', cursive",
|
18 |
+
"'Patrick Hand', cursive",
|
19 |
+
"'Nothing You Could Do', cursive",
|
20 |
+
"'Reenie Beanie', cursive",
|
21 |
+
"'Orbitron', sans-serif",
|
22 |
+
"'Raleway', sans-serif"
|
23 |
+
]
|
24 |
+
font_sizes = ["16px", "17px", "18px" , "19px", "20px"]
|
25 |
+
font_tops = ["0px", "1px", "-1px"]
|
26 |
+
letter_spacings = ["-3px", "-2px", "-1px", "1px", "-2px", "-3px"]
|
27 |
+
text_shadows = [
|
28 |
+
"0px 0px 1px",
|
29 |
+
"0px 0px 2px",
|
30 |
+
"1px 0px 0px",
|
31 |
+
"0px 0px 0px",
|
32 |
+
"0px 1px 0px",
|
33 |
+
"0px 2px 0px",
|
34 |
+
"0px 1px 1px",
|
35 |
+
"1px 1px 0px",
|
36 |
+
"1px 0px 1px"
|
37 |
+
]
|
38 |
+
skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
|
39 |
|
40 |
+
letters = list(word)
|
41 |
+
styled_letters = []
|
42 |
+
|
43 |
+
for i, letter in enumerate(letters):
|
44 |
+
style = {
|
45 |
+
'font-family': random.choice(fonts),
|
46 |
+
'line-height': '1.6',
|
47 |
+
'font-size': random.choice(font_sizes),
|
48 |
+
'letter-spacing': random.choice(letter_spacings),
|
49 |
+
'text-shadow': random.choice(text_shadows),
|
50 |
+
'transform': f'skew({random.choice(skew_angles)})',
|
51 |
+
'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
|
52 |
+
'position': 'relative',
|
53 |
+
'top': random.choice(font_tops),
|
54 |
+
'color': '#000000', # Keep black
|
55 |
+
'display': 'inline-block',
|
56 |
+
'margin': '0 1px',
|
57 |
+
'vertical-align': 'middle'
|
58 |
+
}
|
59 |
+
|
60 |
+
style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
|
61 |
+
styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>'
|
62 |
+
styled_letters.append(styled_letter)
|
63 |
|
64 |
+
return f'<span class="special-word">{" ".join(styled_letters)}</span>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
def display_normal_text():
|
67 |
+
"""Display the text normally without any styling."""
|
68 |
+
global current_text, special_words, special_word_positions
|
69 |
+
|
70 |
+
paragraphs = [
|
71 |
+
'How did we get here December 30th 2124 "Good morning, Benshiro. Wake up! We\'ve got A.I. training ahead," urged Benshiro\'s partner as they prepared for their day in sector A1 - A1000, one of the elite development sectors serving the World One government. It was the year 2124.',
|
72 |
+
'2033 The police were targeting the economically disadvantaged community, and if you were living in poverty in One Country, you were looked down upon. In 2033, a civil war broke out in Philadelphia, New York, Chicago, and a few other big cities. Citizens, fed up with losing jobs to A.I. and enduring systemic mistreatment, initiated the conflict. After the long fight, substantial changes and more restrictive rules were implemented in communities, along with increased travel restrictions for all citizens. Although schools and other institutions remained, life had fundamentally changed for everyone in America.',
|
73 |
+
'2034 In 2034, Anonymous developed an A.I. hack that no bank or institution could counter. This forced all American citizens to join One Country and secure their finances through One Country banking accounts. These accounts were managed by the One Country government and intended to provide security and fairness. Some even thought that the government was behind the attack in the first place.',
|
74 |
+
'2036 By 2036, the government implemented a new policy for One Country accounts. If individuals committed a crime and did not turn themselves in, their accounts would be frozen. This rule was highly controversial, as it curtailed personal freedoms. In a bold initiative, the One Country President announced plans to establish a 99% robotic police force by 2045. The program will begin immediately, aiming to have 30% of the force composed of active robots within the first phase. Human officers would collaborate with these machines in decision-making processes. To support this vision, a vast 10,000-acre automation facility—equivalent to the size of Manhattan—was constructed. This facility aimed to produce advanced robots and flying vehicles, initially reserved for government officials and the wealthy, signaling a significant shift towards automation in society.'
|
75 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
# Select one random word from each paragraph
|
78 |
+
special_words = []
|
79 |
+
for paragraph in paragraphs:
|
80 |
+
words = paragraph.split()
|
81 |
+
special_word = random.choice(words)
|
82 |
+
special_words.append(special_word)
|
83 |
+
|
84 |
+
# Store normal text
|
85 |
+
current_text = '<br><br>'.join(paragraphs)
|
86 |
+
|
87 |
+
return f"""
|
88 |
+
<div style="font-family: 'Josefin Sans', sans-serif; font-size: 16px; line-height: 1.6; padding: 20px; max-width: 800px;">
|
89 |
+
{current_text}
|
90 |
+
</div>
|
91 |
+
"""
|
92 |
|
93 |
+
def trigger_movement():
|
94 |
+
"""Apply random styling to the selected words."""
|
95 |
+
global current_text, special_words
|
96 |
+
|
97 |
+
if not special_words:
|
98 |
+
return current_text
|
99 |
+
|
100 |
+
paragraphs = current_text.split('<br><br>')
|
101 |
+
styled_paragraphs = []
|
102 |
+
|
103 |
+
for i, paragraph in enumerate(paragraphs):
|
104 |
+
if i < len(special_words):
|
105 |
+
special_word = special_words[i]
|
106 |
+
styled_word = generate_random_style(special_word)
|
107 |
+
# Replace only the first occurrence of the special word in this paragraph
|
108 |
+
styled_paragraph = paragraph.replace(special_word, styled_word, 1)
|
109 |
+
styled_paragraphs.append(styled_paragraph)
|
110 |
+
else:
|
111 |
+
styled_paragraphs.append(paragraph)
|
112 |
+
|
113 |
+
updated_text = '<br><br>'.join(styled_paragraphs)
|
114 |
+
current_text = updated_text
|
115 |
+
|
116 |
+
return f"""
|
117 |
+
<div style="font-family: 'Josefin Sans', sans-serif; font-size: 16px; line-height: 1.6; padding: 20px; max-width: 800px;">
|
118 |
+
{updated_text}
|
119 |
+
</div>
|
120 |
+
"""
|
121 |
|
122 |
+
# Create Gradio interface using Blocks
|
123 |
with gr.Blocks() as demo:
|
124 |
+
gr.Markdown("# CircularText Styler\nText with special word styling and animations.")
|
125 |
+
|
126 |
+
output_html = gr.HTML()
|
127 |
with gr.Row():
|
128 |
+
start_btn = gr.Button("▶ Show Text")
|
129 |
+
animate_btn = gr.Button("✨ Trigger Movement")
|
130 |
+
|
131 |
+
start_btn.click(display_normal_text, inputs=[], outputs=output_html)
|
132 |
+
animate_btn.click(trigger_movement, inputs=[], outputs=output_html)
|
133 |
|
134 |
+
# Launch the app
|
135 |
+
demo.launch()
|