circulartext commited on
Commit
35d16ae
·
verified ·
1 Parent(s): 5554d5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -94
app.py CHANGED
@@ -2,107 +2,134 @@ import gradio as gr
2
  import random
3
  import time
4
 
5
- # Store original styles so we can return to them
6
- original_styles = {}
 
 
7
 
8
- fonts = [
9
- "'VT323', monospace",
10
- "'Josefin Sans', sans-serif",
11
- "'Rajdhani', sans-serif",
12
- "'Anton', sans-serif",
13
- "'Caveat', cursive",
14
- "'Patrick Hand', cursive",
15
- "'Nothing You Could Do', cursive",
16
- "'Reenie Beanie', cursive",
17
- "'Orbitron', sans-serif",
18
- "'Raleway', sans-serif"
19
- ]
20
- font_sizes = ["16px", "17px", "18px", "19px", "20px"]
21
- font_tops = ["0px", "1px", "-1px"]
22
- letter_spacings = ["-3px", "-2px", "-1px", "0px", "1px"]
23
- text_shadows = [
24
- "0px 0px 1px #000000",
25
- "0px 0px 2px #000000",
26
- "1px 0px 0px #000000",
27
- "0px 0px 0px #000000",
28
- "0px 1px 0px #000000"
29
- ]
30
- skew_angles = ["-25deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "25deg"]
 
 
 
 
 
 
31
 
32
- def render_letter(letter, style):
33
- style_str = '; '.join(f"{k}: {v}" for k, v in style.items())
34
- return f'<span style="{style_str}">{letter}</span>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- def make_base_style():
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 make_sneaky_style():
54
- return {
55
- 'font-family': random.choice(fonts),
56
- 'font-size': random.choice(font_sizes),
57
- 'letter-spacing': random.choice(letter_spacings),
58
- 'text-shadow': random.choice(text_shadows),
59
- 'transform': f'skew({random.choice(skew_angles)})',
60
- 'margin-top': random.choice(["-0.05cm", "0.00cm", "0.05cm"]),
61
- 'position': 'relative',
62
- 'top': random.choice(font_tops),
63
- 'color': '#000000',
64
- 'display': 'inline-block',
65
- 'margin': '0 1px',
66
- 'vertical-align': 'middle',
67
- 'transition': 'all 0.5s ease'
68
- }
69
 
70
- def show_normal():
71
- text = "This is a sneaky text test."
72
- styled_text = []
73
- original_styles.clear()
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- for i, letter in enumerate(text):
76
- style = make_base_style()
77
- original_styles[i] = style
78
- styled_text.append(render_letter(letter, style))
79
-
80
- return f"<div>{''.join(styled_text)}</div>"
81
-
82
- def sneak_then_back():
83
- text = "This is a sneaky text test."
84
-
85
- # Step 1: Apply sneaky style
86
- sneaky_html = []
87
- for i, letter in enumerate(text):
88
- style = make_sneaky_style()
89
- sneaky_html.append(render_letter(letter, style))
90
- yield f"<div>{''.join(sneaky_html)}</div>"
91
-
92
- # Step 2: Wait, then go back
93
- time.sleep(1.5)
94
- normal_html = []
95
- for i, letter in enumerate(text):
96
- style = make_base_style()
97
- normal_html.append(render_letter(letter, style))
98
- yield f"<div>{''.join(normal_html)}</div>"
 
 
 
 
99
 
 
100
  with gr.Blocks() as demo:
101
- output = gr.HTML()
 
 
102
  with gr.Row():
103
- btn_show = gr.Button("Show Normal")
104
- btn_sneak = gr.Button("Sneak Then Back")
105
- btn_show.click(show_normal, outputs=output)
106
- btn_sneak.click(sneak_then_back, outputs=output)
 
107
 
108
- demo.launch()
 
 
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()