johnpaulbin commited on
Commit
38cf82b
1 Parent(s): ed0424c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -7
app.py CHANGED
@@ -1,16 +1,119 @@
1
  import gradio as gr
2
- from setfit import SetFitModel
 
 
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- model = SetFitModel.from_pretrained("johnpaulbin/toxic-gte-small-3")
6
 
7
  def inf(inpt):
8
- out = model.predict_proba([inpt])
9
-
10
- if out[0][0] > out[0][1]:
11
- return "Not toxic " + str(out[0][0])
 
 
 
 
 
12
  else:
13
- return "Toxic! " + str(out[0][1])
14
 
15
  iface = gr.Interface(fn=inf, inputs="text", outputs="text")
16
  iface.queue(concurrency_count=500).launch()
 
1
  import gradio as gr
2
+ import asyncio
3
+ import torch.nn.functional as F
4
+ from torch import nn
5
+ import os
6
+ os.environ['CURL_CA_BUNDLE'] = ''
7
 
8
+ app = Flask(__name__)
9
+
10
+
11
+ from sentence_transformers import SentenceTransformer
12
+ sentencemodel = SentenceTransformer('johnpaulbin/toxic-gte-small-3')
13
+
14
+ USE_GPU = False
15
+
16
+
17
+ """ Use torchMoji to predict emojis from a single text input
18
+ """
19
+
20
+ import numpy as np
21
+ import emoji, json
22
+ from torchmoji.global_variables import PRETRAINED_PATH, VOCAB_PATH
23
+ from torchmoji.sentence_tokenizer import SentenceTokenizer
24
+ from torchmoji.model_def import torchmoji_emojis
25
+ import torch
26
+
27
+ # Emoji map in emoji_overview.png
28
+ EMOJIS = ":joy: :unamused: :weary: :sob: :heart_eyes: \
29
+ :pensive: :ok_hand: :blush: :heart: :smirk: \
30
+ :grin: :notes: :flushed: :100: :sleeping: \
31
+ :relieved: :relaxed: :raised_hands: :two_hearts: :expressionless: \
32
+ :sweat_smile: :pray: :confused: :kissing_heart: :heartbeat: \
33
+ :neutral_face: :information_desk_person: :disappointed: :see_no_evil: :tired_face: \
34
+ :v: :sunglasses: :rage: :thumbsup: :cry: \
35
+ :sleepy: :yum: :triumph: :hand: :mask: \
36
+ :clap: :eyes: :gun: :persevere: :smiling_imp: \
37
+ :sweat: :broken_heart: :yellow_heart: :musical_note: :speak_no_evil: \
38
+ :wink: :skull: :confounded: :smile: :stuck_out_tongue_winking_eye: \
39
+ :angry: :no_good: :muscle: :facepunch: :purple_heart: \
40
+ :sparkling_heart: :blue_heart: :grimacing: :sparkles:".split(' ')
41
+
42
+ def top_elements(array, k):
43
+ ind = np.argpartition(array, -k)[-k:]
44
+ return ind[np.argsort(array[ind])][::-1]
45
+
46
+
47
+ with open("vocabulary.json", 'r') as f:
48
+ vocabulary = json.load(f)
49
+
50
+ st = SentenceTokenizer(vocabulary, 100)
51
+
52
+ emojimodel = torchmoji_emojis("pytorch_model.bin")
53
+
54
+ if USE_GPU:
55
+ emojimodel.to("cuda:0")
56
+
57
+ def deepmojify(sentence, top_n=5, prob_only=False):
58
+ list_emojis = []
59
+ def top_elements(array, k):
60
+ ind = np.argpartition(array, -k)[-k:]
61
+ return ind[np.argsort(array[ind])][::-1]
62
+
63
+ tokenized, _, _ = st.tokenize_sentences([sentence])
64
+ tokenized = np.array(tokenized).astype(int) # convert to float first
65
+ if USE_GPU:
66
+ tokenized = torch.tensor(tokenized).cuda() # then convert to PyTorch tensor
67
+
68
+ prob = emojimodel.forward(tokenized)[0]
69
+ if not USE_GPU:
70
+ prob = torch.tensor(prob)
71
+ if prob_only:
72
+ return prob
73
+ emoji_ids = top_elements(prob.cpu().numpy(), top_n)
74
+ emojis = map(lambda x: EMOJIS[x], emoji_ids)
75
+ list_emojis.append(emoji.emojize(f"{' '.join(emojis)}", language='alias'))
76
+ # returning the emojis as a list named as list_emojis
77
+ return list_emojis, prob
78
+
79
+
80
+ model = nn.Sequential(
81
+ nn.Linear(448, 300), # Increase the number of neurons
82
+ nn.ReLU(),
83
+ nn.BatchNorm1d(300), # Batch normalization
84
+
85
+ nn.Linear(300, 300), # Increase the number of neurons
86
+ nn.ReLU(),
87
+ nn.BatchNorm1d(300), # Batch normalization
88
+
89
+ nn.Linear(300, 200), # Increase the number of neurons
90
+ nn.ReLU(),
91
+ nn.BatchNorm1d(200), # Batch normalization
92
+
93
+ nn.Linear(200, 125), # Increase the number of neurons
94
+ nn.ReLU(),
95
+ nn.BatchNorm1d(125), # Batch normalization
96
+
97
+ nn.Linear(125, 2),
98
+ nn.Dropout(0.05) # Dropout
99
+ )
100
+
101
+ model.load_state_dict(torch.load("large.pth"))
102
+ model.eval()
103
 
 
104
 
105
  def inf(inpt):
106
+
107
+ TEXT = inpt.lower()
108
+ probs = deepmojify(TEXT, prob_only=True)
109
+ embedding = sentencemodel.encode(TEXT, convert_to_tensor=True)
110
+ INPUT = torch.cat((probs, embedding))
111
+ output = F.softmax(model(INPUT.view(1, -1)), dim=1)
112
+
113
+ if output[0][0] > output[0][1]:
114
+ return "Not toxic " + str(output[0][0])
115
  else:
116
+ return "Toxic! " + str(output[0][1])
117
 
118
  iface = gr.Interface(fn=inf, inputs="text", outputs="text")
119
  iface.queue(concurrency_count=500).launch()