circulartext commited on
Commit
4e61a3c
·
verified ·
1 Parent(s): 8e74908

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -94
app.py CHANGED
@@ -2,138 +2,101 @@ 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 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",
21
  "'Josefin Sans', 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),
54
- 'line-height': '138%',
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>
101
  <head>
102
- <link href="https://fonts.googleapis.com/css2?family=Dosis&family=Josefin+Sans&family=Orbitron&family=Roboto+Condensed&display=swap" rel="stylesheet">
103
- <style>
104
- .CIRCDosis_SZ18_SP-3_SH1_SK0_MA0 {{
105
- float: left;
106
- position: relative;
107
- transition: all 0.3s ease;
108
- }}
109
- body {{
110
- display: flex;
111
- justify-content: center;
112
- align-items: center;
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(
@@ -141,8 +104,8 @@ iface = gr.Interface(
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
148
- iface.launch()
 
2
  import random
3
  from transformers import pipeline
4
 
5
+ # Load the model once when the app starts
6
+ generator = pipeline('text-generation', model='distilgpt2') # Lightweight model
7
+
8
  # Predefined words to check
9
  SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat']
10
 
11
  def generate_circular_text_design(word):
12
+ """Generate a simple animated design for a word using CSS animations."""
13
  # Controlled randomization parameters
 
 
 
 
 
 
 
 
14
  fonts = [
15
  "'Dosis', sans-serif",
16
  "'Josefin Sans', sans-serif",
17
  "'Orbitron', sans-serif",
18
  "'Roboto Condensed', sans-serif"
19
  ]
20
+ font_size = random.choice([18, 20, 22, 24])
21
+ skew_angle = random.choice([-15, -10, 0, 10, 15])
22
+ base_color = f'#{random.randint(0, 0xFFFFFF):06x}'
23
 
24
+ # Unique animation name
25
  animation_name = f"animation_{random.randint(0, 10000)}"
26
 
27
+ # CSS Keyframes
 
 
 
 
28
  keyframes = f"""
29
  @keyframes {animation_name} {{
30
+ 0% {{ transform: skew({skew_angle}deg); color: {base_color}; }}
31
+ 50% {{ transform: skew({-skew_angle}deg); color: {base_color}; }}
32
+ 100% {{ transform: skew({skew_angle}deg); color: {base_color}; }}
 
 
 
 
 
 
 
 
 
33
  }}
34
  """
35
 
36
+ # Style properties
37
+ style = {
38
+ 'font-family': random.choice(fonts),
39
+ 'font-size': f'{font_size}px',
40
+ 'color': base_color,
41
+ 'display': 'inline-block',
42
+ 'animation': f'{animation_name} 2s infinite',
43
+ 'margin': '0 2px'
44
+ }
45
+
46
+ # Convert style to inline CSS
47
+ style_str = '; '.join([f'{k}: {v}' for k, v in style.items()])
48
+
49
+ # Construct HTML with embedded style and animation
50
+ styled_word = f'''
 
 
 
 
 
 
 
 
51
  <style>
52
  {keyframes}
53
  </style>
54
+ <span style="{style_str}">{word}</span>
 
 
55
  '''
56
+ return styled_word
57
 
58
  def process_text(input_text):
59
+ """Process text and apply special styling to matching words."""
60
+ # Limit input length to prevent long processing times
61
+ max_input_length = 20 # Adjust as needed
62
+ input_text = ' '.join(input_text.split()[:max_input_length])
63
+
64
+ # Generate text with limited length
65
+ generated = generator(input_text, max_length=50, num_return_sequences=1)
66
+ generated_text = generated[0]['generated_text']
67
+
68
+ # Limit output length to prevent overflow
69
+ generated_text = generated_text[:300] # Limit to first 300 characters
70
 
71
  # Split text into words
72
  words = generated_text.split()
73
 
74
+ # Check for special words and apply styling
75
  for i, word in enumerate(words):
76
+ # Clean the word for matching
77
+ clean_word = ''.join(filter(str.isalnum, word)).lower()
78
  if clean_word in SPECIAL_WORDS:
 
79
  words[i] = generate_circular_text_design(word)
80
+ else:
81
+ words[i] = word
82
+
83
+ # Combine words back into HTML-formatted text
84
+ output_html = ' '.join(words)
85
 
86
+ # Build the complete HTML
87
+ final_output = f"""
88
  <html>
89
  <head>
90
+ <!-- Include only necessary fonts -->
91
+ <link href='https://fonts.googleapis.com/css2?family=Dosis&family=Josefin+Sans&family=Orbitron&family=Roboto+Condensed&display=swap' rel='stylesheet'>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  </head>
93
+ <body style='background-color: #000; color: #fff; padding: 20px; font-size: 18px; line-height: 1.6; font-family: "Josefin Sans", sans-serif;'>
94
+ <div style='max-width: 800px; margin: auto;'>{output_html}</div>
95
  </body>
96
  </html>
97
  """
98
 
99
+ return final_output
100
 
101
  # Create Gradio interface
102
  iface = gr.Interface(
 
104
  inputs="text",
105
  outputs=gr.HTML(label="Generated Text"),
106
  title="Circular Text Styler",
107
+ description="Enter a prompt to generate text with special word styling."
108
  )
109
 
110
  # Launch the interface
111
+ iface.launch()