Emoji_Translator / app1.2.py
npaleti2002's picture
Rename app.py to app1.2.py
5ee75a5 verified
import random
import re
import gradio as gr
import emoji
# -------------------------
# Config
# -------------------------
ELIGIBLE = ("NOUN", "ADJ")
WORD2EMOJI = {
"heart":"โค๏ธ","love":"โค๏ธ","car":"๐Ÿš—","bike":"๐Ÿšฒ","bus":"๐ŸšŒ","train":"๐Ÿš†","plane":"โœˆ๏ธ",
"pizza":"๐Ÿ•","burger":"๐Ÿ”","coffee":"โ˜•","tea":"๐Ÿต","book":"๐Ÿ“š","laptop":"๐Ÿ’ป",
"phone":"๐Ÿ“ฑ","camera":"๐Ÿ“ท","money":"๐Ÿ’ฐ","rain":"๐ŸŒง๏ธ","sun":"โ˜€๏ธ","moon":"๐ŸŒ™","star":"โญ",
"fire":"๐Ÿ”ฅ","party":"๐ŸŽ‰","gift":"๐ŸŽ","music":"๐ŸŽต","dog":"๐Ÿถ","cat":"๐Ÿฑ","bird":"๐Ÿฆ","flower":"๐ŸŒธ",
"tree":"๐ŸŒณ","house":"๐Ÿ ","school":"๐Ÿซ","office":"๐Ÿข","hospital":"๐Ÿฅ","food":"๐Ÿฝ๏ธ",
"cake":"๐Ÿฐ","cookie":"๐Ÿช","chocolate":"๐Ÿซ","icecream":"๐Ÿจ","beach":"๐Ÿ–๏ธ","mountain":"โ›ฐ๏ธ","city":"๐Ÿ™๏ธ",
"rocket":"๐Ÿš€","earth":"๐ŸŒ","world":"๐ŸŒ","mail":"โœ‰๏ธ","football":"๐Ÿˆ","soccer":"โšฝ","basketball":"๐Ÿ€",
"tennis":"๐ŸŽพ","medal":"๐Ÿ…","trophy":"๐Ÿ†","alarm":"โฐ","idea":"๐Ÿ’ก","check":"โœ…","cross":"โŒ",
"warning":"โš ๏ธ",
# adjectives
"happy":"๐Ÿ˜Š","sad":"๐Ÿ˜ข","angry":"๐Ÿ˜ก","tired":"๐Ÿ˜ด","sleepy":"๐Ÿ˜ด","cool":"๐Ÿ˜Ž",
"lovely":"๐Ÿฅฐ","beautiful":"๐Ÿ˜","pretty":"๐Ÿ˜","handsome":"๐Ÿคต","smart":"๐Ÿง ","brilliant":"๐Ÿง ",
"fast":"โšก","quick":"โšก","slow":"๐Ÿข","hot":"๐Ÿ”ฅ","cold":"โ„๏ธ","sweet":"๐Ÿฌ","salty":"๐Ÿง‚",
"spicy":"๐ŸŒถ๏ธ","fresh":"๐Ÿซง","strong":"๐Ÿ’ช","weak":"๐Ÿซค","shiny":"โœจ","clean":"๐Ÿซง","dirty":"๐Ÿงน",
"busy":"๐Ÿ“ˆ","rich":"๐Ÿ’ฐ","poor":"๐Ÿช™","lucky":"๐Ÿ€","fun":"๐ŸŽ‰","romantic":"๐Ÿ’˜","scary":"๐Ÿ˜ฑ"
}
ADJECTIVE_WORDS = {
"happy","sad","angry","tired","sleepy","cool","lovely","beautiful","pretty",
"handsome","smart","brilliant","fast","quick","slow","hot","cold","sweet","salty","spicy",
"fresh","strong","weak","shiny","clean","dirty","busy","rich","poor","lucky","fun","romantic","scary"
}
_ADJ_SUFFIXES = ("y","ive","ful","less","ous","al","ic","able","ible","ish")
_NOUN_SUFFIXES = ("tion","ment","ness","ity","er","or","ship","age","ance","ence","ism","ist")
# -------------------------
# Helpers
# -------------------------
def try_emojize_alias(word):
lemma = word.lower()
for cand in (f":{lemma}:", f":{lemma}_face:", f":{lemma}_emoji:", f":{lemma}_button:"):
try:
em = emoji.emojize(cand, language="alias")
except TypeError:
em = emoji.emojize(cand)
if em != cand:
return em
return None
def tokenize(text):
return re.findall(r"\w+|[^\w\s]", text, flags=re.UNICODE)
def simple_tag(tokens, allow_nouns=True, allow_adjs=True):
tagged = []
for t in tokens:
if not re.search(r"[A-Za-z]", t):
tagged.append((t, "OTHER"))
continue
low = t.lower()
if low in WORD2EMOJI:
tag = "ADJ" if low in ADJECTIVE_WORDS else "NOUN"
if (tag == "NOUN" and not allow_nouns) or (tag == "ADJ" and not allow_adjs):
tag = "OTHER"
tagged.append((t, tag))
continue
tag = "OTHER"
if low.endswith(_ADJ_SUFFIXES):
tag = "ADJ"
elif low.endswith(_NOUN_SUFFIXES) or t[:1].isupper():
tag = "NOUN"
if (tag == "NOUN" and not allow_nouns) or (tag == "ADJ" and not allow_adjs):
tag = "OTHER"
tagged.append((t, tag))
return tagged
_EMOJI_RE = re.compile("[" "\U0001F300-\U0001FAFF" "\u2600-\u26FF" "\u2700-\u27BF" "]", flags=re.UNICODE)
def emoji_count(s): return len(_EMOJI_RE.findall(s))
# -------------------------
# Core translate
# -------------------------
def translate_text(text, mode="Replace", replacement_rate=1.0, keep_case=True, dedupe=True,
allow_nouns=True, allow_adjs=True):
text = (text or "").strip()
if not text:
return "", "๐Ÿงฎ Emojis: 0", "๐Ÿ”€ Replaced words: 0"
tokens = tokenize(text)
tagged = simple_tag(tokens, allow_nouns, allow_adjs)
out, replaced = [], 0
for tok, tag in tagged:
eligible = (tag in ELIGIBLE) and re.search(r"[A-Za-z]", tok)
if eligible and random.random() <= replacement_rate:
em = WORD2EMOJI.get(tok.lower()) or try_emojize_alias(tok.lower())
if em:
replaced += 1
out.append(em if mode == "Replace" else f"{tok} {em}")
continue
out.append(tok)
# spacing: no space before , . ! ? ; : ) ] }
result = ""
for i, t in enumerate(out):
result += t
if i < len(out) - 1:
nxt = out[i + 1]
if not re.match(r"^[,\.!\?;:\)\]\}]+$", nxt):
result += " "
if not keep_case:
result = re.sub(r"[A-Za-z]+", lambda m: m.group(0).lower(), result)
return result, f"๐Ÿงฎ Emojis: {emoji_count(result)}", f"๐Ÿ”€ Replaced words: {replaced}"
# -------------------------
# UI
# -------------------------
EXAMPLES = [
"I love spicy pizza and hot coffee on a rainy day.",
"The smart dog chased a happy cat around the house.",
"We had a beautiful view of the city from the mountain.",
"She wrote a brilliant idea in her notebook.",
"This is a strong team with a cool product.",
]
with gr.Blocks(title="Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€") as demo:
gr.Markdown("## Emoji Translator ๐Ÿ˜Žโžก๏ธ๐Ÿ™‚๐Ÿ•๐Ÿš€\nType a sentence โ€” Iโ€™ll replace **nouns/adjectives** with matching emojis.")
with gr.Row():
with gr.Column(scale=5):
inp = gr.Textbox(label="Your sentence", lines=4, placeholder="Type hereโ€ฆ")
mode = gr.Radio(choices=["Replace", "Append"], value="Replace", label="Mode")
rate = gr.Slider(0, 1, 1, 0.05, label="Replacement rate")
with gr.Row():
keep_case = gr.Checkbox(True, "Preserve capitalization")
dedupe = gr.Checkbox(True, "Avoid duplicate emojis")
with gr.Row():
allow_nouns = gr.Checkbox(True, "Target nouns")
allow_adjs = gr.Checkbox(True, "Target adjectives")
btn = gr.Button("Translate โœจ")
clear_btn = gr.Button("Clear")
gr.Examples(EXAMPLES, inputs=inp)
with gr.Column(scale=6):
out = gr.Textbox(label="Emojiโ€‘fied text", lines=6, show_copy_button=True, interactive=False)
emoji_stat = gr.Markdown("๐Ÿงฎ Emojis: 0")
repl_stat = gr.Markdown("๐Ÿ”€ Replaced words: 0")
def run_translate(text, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs):
return translate_text(text, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs)
btn.click(run_translate, [inp, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs], [out, emoji_stat, repl_stat])
def clear_all():
return "", "๐Ÿงฎ Emojis: 0", "๐Ÿ”€ Replaced words: 0", ""
clear_btn.click(clear_all, None, [out, emoji_stat, repl_stat, inp])
if __name__ == "__main__":
demo.launch()