npaleti2002 commited on
Commit
eef50ff
ยท
verified ยท
1 Parent(s): a33ef2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -17
app.py CHANGED
@@ -88,19 +88,34 @@ def translate_text(text, mode="Replace", replacement_rate=1.0, keep_case=True, d
88
  allow_nouns=True, allow_adjs=True):
89
  text = (text or "").strip()
90
  if not text:
91
- return "", "๐Ÿงฎ Emojis: 0", "๐Ÿ”€ Replaced words: 0"
 
92
  tokens = tokenize(text)
93
  tagged = simple_tag(tokens, allow_nouns, allow_adjs)
94
- out, replaced = [], 0
95
- for tok, tag in tagged:
 
 
 
 
96
  eligible = (tag in ELIGIBLE) and re.search(r"[A-Za-z]", tok)
97
  if eligible and random.random() <= replacement_rate:
98
  em = WORD2EMOJI.get(tok.lower()) or try_emojize_alias(tok.lower())
99
  if em:
100
- replaced += 1
101
- out.append(em if mode == "Replace" else f"{tok} {em}")
102
- continue
 
 
 
 
 
 
 
 
 
103
  out.append(tok)
 
104
  # spacing: no space before , . ! ? ; : ) ] }
105
  result = ""
106
  for i, t in enumerate(out):
@@ -109,9 +124,13 @@ def translate_text(text, mode="Replace", replacement_rate=1.0, keep_case=True, d
109
  nxt = out[i + 1]
110
  if not re.match(r"^[,\.!\?;:\)\]\}]+$", nxt):
111
  result += " "
 
112
  if not keep_case:
113
  result = re.sub(r"[A-Za-z]+", lambda m: m.group(0).lower(), result)
114
- return result, f"๐Ÿงฎ Emojis: {emoji_count(result)}", f"๐Ÿ”€ Replaced words: {replaced}"
 
 
 
115
 
116
  # -------------------------
117
  # UI
@@ -125,8 +144,8 @@ EXAMPLES = [
125
  "This is a strong team with a cool product.",
126
  ]
127
 
128
- with gr.Blocks(title="Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€") as demo:
129
- gr.Markdown("## Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€\nType a sentence โ€” Iโ€™ll replace **nouns/adjectives** with matching emojis.")
130
 
131
  with gr.Row():
132
  # LEFT: inputs
@@ -135,7 +154,7 @@ with gr.Blocks(title="Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€") as demo:
135
  mode = gr.Radio(choices=["Replace", "Append"], value="Replace", label="Mode")
136
  rate = gr.Slider(0, 1, 1, 0.05, label="Replacement rate")
137
 
138
- # Minimal labels; explanation goes into `info=`
139
  keep_case = gr.Checkbox(
140
  value=True, label="",
141
  info="Keeps original letter casing (e.g., 'Dog' stays 'Dog'). Emojis are case-agnostic."
@@ -145,7 +164,7 @@ with gr.Blocks(title="Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€") as demo:
145
  info="Apply emojis to adjectives (e.g., happy โ†’ ๐Ÿ˜Š). Uncheck to ignore adjectives."
146
  )
147
 
148
- # Advanced options tucked away with WHY in info
149
  with gr.Accordion("Advanced options", open=False):
150
  dedupe = gr.Checkbox(
151
  value=True, label="",
@@ -163,7 +182,7 @@ with gr.Blocks(title="Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€") as demo:
163
  with gr.Column(scale=6):
164
  out = gr.Textbox(label="Emoji-fied text", lines=6, show_copy_button=True, interactive=False)
165
  emoji_stat = gr.Markdown("๐Ÿงฎ Emojis: 0")
166
- repl_stat = gr.Markdown("๐Ÿ”€ Replaced words: 0")
167
  gr.Markdown("**Examples**")
168
  gr.Examples(EXAMPLES, inputs=inp)
169
 
@@ -173,12 +192,13 @@ with gr.Blocks(title="Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€") as demo:
173
  btn.click(
174
  run_translate,
175
  [inp, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs],
176
- [out, emoji_stat, repl_stat]
177
  )
178
 
179
- def clear_all():
180
- return "", "๐Ÿงฎ Emojis: 0", "๐Ÿ”€ Replaced words: 0", ""
181
- clear_btn.click(clear_all, None, [out, emoji_stat, repl_stat, inp])
 
182
 
183
  if __name__ == "__main__":
184
- demo.launch()
 
88
  allow_nouns=True, allow_adjs=True):
89
  text = (text or "").strip()
90
  if not text:
91
+ return "", "๐Ÿงฎ Emojis: 0", ("๐Ÿ”€ Replaced words: 0" if mode == "Replace" else "โž• Appended emojis: 0")
92
+
93
  tokens = tokenize(text)
94
  tagged = simple_tag(tokens, allow_nouns, allow_adjs)
95
+
96
+ out = []
97
+ replaced = 0
98
+ appended = 0
99
+
100
+ for idx, (tok, tag) in enumerate(tagged):
101
  eligible = (tag in ELIGIBLE) and re.search(r"[A-Za-z]", tok)
102
  if eligible and random.random() <= replacement_rate:
103
  em = WORD2EMOJI.get(tok.lower()) or try_emojize_alias(tok.lower())
104
  if em:
105
+ if mode == "Replace":
106
+ replaced += 1
107
+ out.append(em)
108
+ continue
109
+ else: # Append mode
110
+ # Optional dedupe: don't append if the next raw token already is the same emoji
111
+ if dedupe and idx + 1 < len(tagged) and tagged[idx + 1][0] == em:
112
+ out.append(tok)
113
+ continue
114
+ appended += 1
115
+ out.append(f"{tok} {em}")
116
+ continue
117
  out.append(tok)
118
+
119
  # spacing: no space before , . ! ? ; : ) ] }
120
  result = ""
121
  for i, t in enumerate(out):
 
124
  nxt = out[i + 1]
125
  if not re.match(r"^[,\.!\?;:\)\]\}]+$", nxt):
126
  result += " "
127
+
128
  if not keep_case:
129
  result = re.sub(r"[A-Za-z]+", lambda m: m.group(0).lower(), result)
130
+
131
+ emojis = emoji_count(result)
132
+ action_stat = f"๐Ÿ”€ Replaced words: {replaced}" if mode == "Replace" else f"โž• Appended emojis: {appended}"
133
+ return result, f"๐Ÿงฎ Emojis: {emojis}", action_stat
134
 
135
  # -------------------------
136
  # UI
 
144
  "This is a strong team with a cool product.",
145
  ]
146
 
147
+ with gr.Blocks(title="Emoji Translator") as demo:
148
+ gr.Markdown("# Emoji Translator\nType a sentence โ€” Iโ€™ll replace **nouns/adjectives** with matching emojis.")
149
 
150
  with gr.Row():
151
  # LEFT: inputs
 
154
  mode = gr.Radio(choices=["Replace", "Append"], value="Replace", label="Mode")
155
  rate = gr.Slider(0, 1, 1, 0.05, label="Replacement rate")
156
 
157
+ # Minimal labels; why-explanations in info
158
  keep_case = gr.Checkbox(
159
  value=True, label="",
160
  info="Keeps original letter casing (e.g., 'Dog' stays 'Dog'). Emojis are case-agnostic."
 
164
  info="Apply emojis to adjectives (e.g., happy โ†’ ๐Ÿ˜Š). Uncheck to ignore adjectives."
165
  )
166
 
167
+ # Advanced options with WHY in info
168
  with gr.Accordion("Advanced options", open=False):
169
  dedupe = gr.Checkbox(
170
  value=True, label="",
 
182
  with gr.Column(scale=6):
183
  out = gr.Textbox(label="Emoji-fied text", lines=6, show_copy_button=True, interactive=False)
184
  emoji_stat = gr.Markdown("๐Ÿงฎ Emojis: 0")
185
+ action_stat = gr.Markdown("๐Ÿ”€ Replaced words: 0")
186
  gr.Markdown("**Examples**")
187
  gr.Examples(EXAMPLES, inputs=inp)
188
 
 
192
  btn.click(
193
  run_translate,
194
  [inp, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs],
195
+ [out, emoji_stat, action_stat]
196
  )
197
 
198
+ def clear_all(current_mode):
199
+ action = "๐Ÿ”€ Replaced words: 0" if current_mode == "Replace" else "โž• Appended emojis: 0"
200
+ return "", "๐Ÿงฎ Emojis: 0", action, ""
201
+ clear_btn.click(clear_all, [mode], [out, emoji_stat, action_stat, inp])
202
 
203
  if __name__ == "__main__":
204
+ demo.launch()