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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -96
app.py CHANGED
@@ -2,113 +2,107 @@ import gradio as gr
2
  import random
3
  import time
4
 
5
- # Global variables
6
- original_paragraphs = []
7
 
8
- def generate_sneak_style(word):
9
- """Generate styled version of the word in black color."""
10
- fonts = [
11
- "'VT323', monospace",
12
- "'Josefin Sans', sans-serif",
13
- "'Rajdhani', sans-serif",
14
- "'Anton', sans-serif",
15
- "'Caveat', cursive",
16
- "'Patrick Hand', cursive",
17
- "'Nothing You Could Do', cursive",
18
- "'Reenie Beanie', cursive",
19
- "'Orbitron', sans-serif",
20
- "'Raleway', sans-serif"
21
- ]
22
- font_sizes = ["16px", "17px", "18px" , "19px", "20px"]
23
- font_tops = ["0px", "1px", "-1px"]
24
- letter_spacings = ["-3px", "-2px", "-1px", "1px", "-2px", "-3px"]
25
- text_shadows = [
26
- "0px 0px 1px",
27
- "0px 0px 2px",
28
- "1px 0px 0px",
29
- "0px 0px 0px",
30
- "0px 1px 0px",
31
- "0px 2px 0px",
32
- "0px 1px 1px",
33
- "1px 1px 0px",
34
- "1px 0px 1px"
35
- ]
36
- skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"]
37
 
38
- letters = list(word)
39
- styled_letters = []
40
-
41
- for letter in letters:
42
- style = {
43
- 'font-family': random.choice(fonts),
44
- 'line-height': '1.6',
45
- 'font-size': random.choice(font_sizes),
46
- 'letter-spacing': random.choice(letter_spacings),
47
- 'text-shadow': random.choice(text_shadows),
48
- 'transform': f'skew({random.choice(skew_angles)})',
49
- 'margin-top': random.choice(["-0.02cm", "0.00cm", "0.02cm"]),
50
- 'position': 'relative',
51
- 'top': random.choice(font_tops),
52
- 'color': '#000000', # always black
53
- 'display': 'inline-block',
54
- 'margin': '0 1px',
55
- 'vertical-align': 'middle'
56
- }
57
-
58
- style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
59
- styled_letter = f'<span style="{style_str}">{letter}</span>'
60
- styled_letters.append(styled_letter)
61
 
62
- return f'<span>{" ".join(styled_letters)}</span>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- def show_text_normal():
65
- """Show all paragraphs with no styling."""
66
- global original_paragraphs
67
- original_paragraphs = [
68
- '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.',
69
- '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.',
70
- '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.',
71
- '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.'
72
- ]
73
- return render_html('<br><br>'.join(original_paragraphs))
 
 
 
 
 
 
74
 
75
- def sneak_style_then_reset():
76
- """Apply sneak style to one random word per paragraph, then revert."""
77
- paragraphs = original_paragraphs.copy()
78
- styled_paragraphs = []
79
 
80
- for paragraph in paragraphs:
81
- words = paragraph.split()
82
- if words:
83
- chosen = random.choice(words)
84
- styled_word = generate_sneak_style(chosen)
85
- paragraph = ' '.join(styled_word if word == chosen else word for word in words)
86
- styled_paragraphs.append(paragraph)
87
 
88
- # Show sneaky style
89
- yield render_html('<br><br>'.join(styled_paragraphs))
90
 
91
- # Wait and revert
92
- time.sleep(1.5)
93
- yield render_html('<br><br>'.join(original_paragraphs))
 
 
 
 
 
 
94
 
95
- def render_html(text):
96
- """Wrap text in simple HTML."""
97
- return f"""
98
- <div style="font-family: 'Josefin Sans', sans-serif; font-size: 16px;
99
- line-height: 1.6; padding: 20px; max-width: 800px;">
100
- {text}
101
- </div>
102
- """
103
 
104
- # Interface
105
  with gr.Blocks() as demo:
106
- gr.Markdown("# Sneaky Text Styler")
107
- output_html = gr.HTML()
108
  with gr.Row():
109
- show_btn = gr.Button("📄 Show Normal")
110
- sneak_btn = gr.Button("Sneak Style")
111
- show_btn.click(show_text_normal, outputs=output_html)
112
- sneak_btn.click(sneak_style_then_reset, outputs=output_html)
113
 
114
  demo.launch()
 
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()