Spaces:
Running
Running
johnpaulbin
commited on
Commit
•
59da306
1
Parent(s):
fff5a8f
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,64 @@ import json
|
|
9 |
import random
|
10 |
import re
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
app = Flask(__name__)
|
14 |
|
|
|
9 |
import random
|
10 |
import re
|
11 |
|
12 |
+
import numpy as np
|
13 |
+
import emoji, json
|
14 |
+
from torchmoji.global_variables import PRETRAINED_PATH, VOCAB_PATH
|
15 |
+
from torchmoji.sentence_tokenizer import SentenceTokenizer
|
16 |
+
from torchmoji.model_def import torchmoji_emojis
|
17 |
+
import torch
|
18 |
+
|
19 |
+
# Emoji map in emoji_overview.png
|
20 |
+
EMOJIS = ":joy: :unamused: :weary: :sob: :heart_eyes: \
|
21 |
+
:pensive: :ok_hand: :blush: :heart: :smirk: \
|
22 |
+
:grin: :notes: :flushed: :100: :sleeping: \
|
23 |
+
:relieved: :relaxed: :raised_hands: :two_hearts: :expressionless: \
|
24 |
+
:sweat_smile: :pray: :confused: :kissing_heart: :heartbeat: \
|
25 |
+
:neutral_face: :information_desk_person: :disappointed: :see_no_evil: :tired_face: \
|
26 |
+
:v: :sunglasses: :rage: :thumbsup: :cry: \
|
27 |
+
:sleepy: :yum: :triumph: :hand: :mask: \
|
28 |
+
:clap: :eyes: :gun: :persevere: :smiling_imp: \
|
29 |
+
:sweat: :broken_heart: :yellow_heart: :musical_note: :speak_no_evil: \
|
30 |
+
:wink: :skull: :confounded: :smile: :stuck_out_tongue_winking_eye: \
|
31 |
+
:angry: :no_good: :muscle: :facepunch: :purple_heart: \
|
32 |
+
:sparkling_heart: :blue_heart: :grimacing: :sparkles:".split(' ')
|
33 |
+
|
34 |
+
def top_elements(array, k):
|
35 |
+
ind = np.argpartition(array, -k)[-k:]
|
36 |
+
return ind[np.argsort(array[ind])][::-1]
|
37 |
+
|
38 |
+
|
39 |
+
with open("vocabulary.json", 'r') as f:
|
40 |
+
vocabulary = json.load(f)
|
41 |
+
|
42 |
+
st = SentenceTokenizer(vocabulary, 100)
|
43 |
+
|
44 |
+
emojimodel = torchmoji_emojis("pytorch_model.bin")
|
45 |
+
|
46 |
+
if USE_GPU:
|
47 |
+
emojimodel.to("cuda:0")
|
48 |
+
|
49 |
+
def deepmojify(sentence, top_n=5, prob_only=False):
|
50 |
+
list_emojis = []
|
51 |
+
def top_elements(array, k):
|
52 |
+
ind = np.argpartition(array, -k)[-k:]
|
53 |
+
return ind[np.argsort(array[ind])][::-1]
|
54 |
+
|
55 |
+
tokenized, _, _ = st.tokenize_sentences([sentence])
|
56 |
+
tokenized = np.array(tokenized).astype(int) # convert to float first
|
57 |
+
if USE_GPU:
|
58 |
+
tokenized = torch.tensor(tokenized).cuda() # then convert to PyTorch tensor
|
59 |
+
|
60 |
+
prob = emojimodel.forward(tokenized)[0]
|
61 |
+
if not USE_GPU:
|
62 |
+
prob = torch.tensor(prob)
|
63 |
+
if prob_only:
|
64 |
+
return prob
|
65 |
+
emoji_ids = top_elements(prob.cpu().numpy(), top_n)
|
66 |
+
emojis = map(lambda x: EMOJIS[x], emoji_ids)
|
67 |
+
list_emojis.append(emoji.emojize(f"{' '.join(emojis)}", language='alias'))
|
68 |
+
# returning the emojis as a list named as list_emojis
|
69 |
+
return list_emojis, prob
|
70 |
|
71 |
app = Flask(__name__)
|
72 |
|