easrng commited on
Commit
d9ac5ad
1 Parent(s): abf89cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import emoji_data_python
2
+ import pickle
3
+ from tqdm import tqdm
4
+ from sentence_transformers import SentenceTransformer
5
+ import numpy as np
6
+ model = SentenceTransformer('all-mpnet-base-v2')
7
+ try:
8
+ with open('embeddings_list.pkl', 'rb') as f:
9
+ embeddings_list = pickle.load(f)
10
+ except:
11
+ embeddings_list = []
12
+ emojis_to_compute = [e for e in emoji_data_python.emoji_data if e.unified not in [e[0] for e in embeddings_list]]
13
+ if emojis_to_compute:
14
+ for e in tqdm(emojis_to_compute, desc='Computing embeddings'):
15
+ strings = [n.replace('_', ' ').strip() for n in e.short_names] + [e.name.lower()]
16
+ for s in strings:
17
+ embedding = model.encode(s)
18
+ embeddings_list.append((e.unified, embedding))
19
+ with open('embeddings_list.pkl', 'wb') as f:
20
+ pickle.dump(embeddings_list, f)
21
+ def closest_emoji(text):
22
+ text_embedding = model.encode(text)
23
+ closest_emoji = None
24
+ closest_distance = np.inf
25
+ for emoji, emoji_embedding in embeddings_list:
26
+ distance = np.linalg.norm(text_embedding - emoji_embedding)
27
+ if distance < closest_distance:
28
+ closest_distance = distance
29
+ closest_emoji = emoji
30
+ return emoji_data_python.unified_to_char(closest_emoji)
31
+ import gradio as gr
32
+ emoji_input = gr.inputs.Textbox(label='text in')
33
+ emoji_output = gr.outputs.Textbox(label='emoji out')
34
+ iface = gr.Interface(fn=closest_emoji, inputs=emoji_input, outputs=emoji_output,
35
+ title='text to emoji')
36
+ iface.launch()