Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import torch
|
| 3 |
from torch import nn
|
| 4 |
import re
|
|
@@ -10,214 +11,40 @@ from collections import OrderedDict
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
self.input_seq_index, self.target_seq_index = self.get_seq(self.char2int, self.input_seq_char, self.target_seq_char, len(self.text))
|
| 51 |
-
self.dict_size = len(self.char2int)
|
| 52 |
-
self.seq_len = self.maxlen - 1
|
| 53 |
-
self.batch_size = len(self.text)
|
| 54 |
-
self.input_seq = self.one_hot_encode(self.input_seq_index, self.dict_size, self.seq_len, self.batch_size)
|
| 55 |
-
|
| 56 |
-
def one_hot_encode(self, sequence, dict_size, seq_len, batch_size):
|
| 57 |
-
# Creating a multi-dimensional array of zeros with the desired output shape
|
| 58 |
-
features = np.zeros((batch_size, seq_len, dict_size), dtype=np.float32)
|
| 59 |
-
|
| 60 |
-
# Replacing the 0 at the relevant character index with a 1 to represent that character
|
| 61 |
-
for i in range(batch_size):
|
| 62 |
-
for u in range(seq_len):
|
| 63 |
-
features[i, u, sequence[i][u]] = 1
|
| 64 |
-
return features
|
| 65 |
-
|
| 66 |
-
def get_seq(self, char2int, input_seq_char, target_seq_char,n):
|
| 67 |
-
x=[]
|
| 68 |
-
y=[]
|
| 69 |
-
for i in range(n):
|
| 70 |
-
x.append([char2int[character] for character in input_seq_char[i]])
|
| 71 |
-
y.append([char2int[character] for character in target_seq_char[i]])
|
| 72 |
-
return x,y
|
| 73 |
-
|
| 74 |
-
def get_seq_char(self, text):
|
| 75 |
-
input_seq = []
|
| 76 |
-
target_seq = []
|
| 77 |
-
|
| 78 |
-
for i in range(len(text)):
|
| 79 |
-
# Remove last character for input sequence
|
| 80 |
-
input_seq.append(text[i][:-1])
|
| 81 |
-
# Remove first character for target sequence
|
| 82 |
-
target_seq.append(text[i][1:])
|
| 83 |
-
return input_seq, target_seq
|
| 84 |
-
|
| 85 |
-
def unique_chars(self, chars_all):
|
| 86 |
-
chars = []
|
| 87 |
-
for letter in chars_all:
|
| 88 |
-
if letter not in chars:
|
| 89 |
-
chars.append(letter)
|
| 90 |
-
# chars = sorted(chars)
|
| 91 |
-
if ' ' not in chars:
|
| 92 |
-
chars.append(' ')
|
| 93 |
-
return sorted(chars)
|
| 94 |
-
|
| 95 |
-
def update_text(self):
|
| 96 |
-
for i in range(len(self.text)):
|
| 97 |
-
while len(self.text[i])<self.maxlen:
|
| 98 |
-
self.text[i] += ' '
|
| 99 |
-
|
| 100 |
-
def description(self):
|
| 101 |
-
text = {}
|
| 102 |
-
for word in self.text:
|
| 103 |
-
char = word[0]
|
| 104 |
-
if char not in text:
|
| 105 |
-
text[char] = []
|
| 106 |
-
text[char].append(word.strip())
|
| 107 |
-
for k,v in (sorted(text.items())):
|
| 108 |
-
print(f'{k} : {sorted(v)}')
|
| 109 |
-
|
| 110 |
-
def lengt_analysis(self):
|
| 111 |
-
text = {}
|
| 112 |
-
words = set(self.text_all)
|
| 113 |
-
for word in words:
|
| 114 |
-
n = len(word)
|
| 115 |
-
if n not in text:
|
| 116 |
-
text[n] = []
|
| 117 |
-
text[n].append(word.strip())
|
| 118 |
-
for k,v in (sorted(text.items())):
|
| 119 |
-
print(f'{k} : count = {len(v)} list = {sorted(v)}')
|
| 120 |
-
return None # text
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
def create_object(doc):
|
| 124 |
-
return Text2Words(doc)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
def get_inputs(obj):
|
| 128 |
-
input_seq = torch.tensor(obj.input_seq, device=device)
|
| 129 |
-
target_seq_index = torch.tensor(obj.target_seq_index, device=device)
|
| 130 |
-
return input_seq, target_seq_index
|
| 131 |
-
|
| 132 |
-
class Model(nn.Module):
|
| 133 |
-
def __init__(self, input_size, output_size, hidden_dim, n_layers):
|
| 134 |
-
super(Model, self).__init__()
|
| 135 |
-
|
| 136 |
-
# Defining some parameters
|
| 137 |
-
self.hidden_dim = hidden_dim
|
| 138 |
-
self.n_layers = n_layers
|
| 139 |
-
|
| 140 |
-
#Defining the layers
|
| 141 |
-
# RNN Layer
|
| 142 |
-
self.rnn = nn.RNN(input_size, hidden_dim, n_layers, batch_first=True)
|
| 143 |
-
# Fully connected layer
|
| 144 |
-
self.fc = nn.Linear(hidden_dim, output_size)
|
| 145 |
-
|
| 146 |
-
def forward(self, x):
|
| 147 |
-
batch_size = x.size(0)
|
| 148 |
-
hidden = self.init_hidden(batch_size)
|
| 149 |
-
out, hidden = self.rnn(x, hidden)
|
| 150 |
-
out = out.contiguous().view(-1, self.hidden_dim)
|
| 151 |
-
out = self.fc(out)
|
| 152 |
-
return out, hidden
|
| 153 |
-
|
| 154 |
-
def init_hidden(self, batch_size):
|
| 155 |
-
# This method generates the first hidden state of zeros
|
| 156 |
-
torch.manual_seed(42)
|
| 157 |
-
hidden = torch.zeros((self.n_layers, batch_size, self.hidden_dim), device=device)
|
| 158 |
-
return hidden
|
| 159 |
-
|
| 160 |
-
def create_model(obj):
|
| 161 |
-
model = Model(input_size=obj.dict_size, output_size=obj.dict_size, hidden_dim=2*obj.dict_size, n_layers=1)
|
| 162 |
-
model.to(device)
|
| 163 |
-
lr=0.01
|
| 164 |
-
criterion = nn.CrossEntropyLoss()
|
| 165 |
-
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
|
| 166 |
-
return model, criterion, optimizer
|
| 167 |
-
|
| 168 |
-
# This function takes in the model and character as arguments and returns the next character prediction and hidden state
|
| 169 |
-
def predict(model, character):
|
| 170 |
-
# One-hot encoding our input to fit into the model
|
| 171 |
-
# print(character)
|
| 172 |
-
character = np.array([[obj.char2int[c] for c in character]])
|
| 173 |
-
# print(character)
|
| 174 |
-
character = obj.one_hot_encode(character, obj.dict_size, character.shape[1], 1)
|
| 175 |
-
# print(character,character.shape)
|
| 176 |
-
character = torch.tensor(character, device=device)
|
| 177 |
-
character.to(device)
|
| 178 |
-
out, hidden = model(character)
|
| 179 |
-
# print(out, hidden)
|
| 180 |
-
prob = nn.functional.softmax(out[-1], dim=0).data
|
| 181 |
-
# print(prob)
|
| 182 |
-
char_ind = torch.max(prob, dim=0)[1].item()
|
| 183 |
-
# print(sorted(prob, reverse=True))
|
| 184 |
-
return obj.int2char[char_ind], hidden
|
| 185 |
-
|
| 186 |
-
# This function takes the desired output length and input characters as arguments, returning the produced sentence
|
| 187 |
-
def sample(model, out_len, start='h'):
|
| 188 |
-
model.eval() # eval mode
|
| 189 |
-
chars = [ch for ch in start]
|
| 190 |
-
char = chars[-1]
|
| 191 |
-
chars = chars[:-1]
|
| 192 |
-
# Now pass in the previous characters and get a new one
|
| 193 |
-
while char != ' ':
|
| 194 |
-
chars.append(char)
|
| 195 |
-
char, h = predict(model, chars)
|
| 196 |
-
return ''.join(chars)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
def load_checkpoint(filepath):
|
| 200 |
-
checkpoint = torch.load(filepath)
|
| 201 |
-
# print(checkpoint['state_dict'])
|
| 202 |
-
model = checkpoint['model']
|
| 203 |
-
# print(model)
|
| 204 |
-
model.load_state_dict(checkpoint['state_dict'])
|
| 205 |
-
# print(model.parameters())
|
| 206 |
-
# for parameter in model.parameters():
|
| 207 |
-
# parameter.requires_grad = False
|
| 208 |
-
# print(parameter)
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
model.eval()
|
| 212 |
-
return model
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
@app.route('/')
|
| 216 |
-
def home():
|
| 217 |
-
print(1)
|
| 218 |
-
return {'key':"Hello HuggingFace! Successfully deployed. "}
|
| 219 |
-
# model = load_checkpoint('checkpoint.pth')
|
| 220 |
-
# print(2)
|
| 221 |
-
# res = sample(model, obj.maxlen, 'ap')
|
| 222 |
-
# print(3)
|
| 223 |
-
# return {'key':res}
|
|
|
|
| 1 |
+
rom flask import Flask, jsonify, render_template, request, make_response
|
| 2 |
+
import transformers
|
| 3 |
import torch
|
| 4 |
from torch import nn
|
| 5 |
import re
|
|
|
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
| 14 |
+
# create a python dictionary for your models d = {<key>: <value>, <key>: <value>, ..., <key>: <value>}
|
| 15 |
+
dictOfModels = {"BERT" : transformers.pipeline('sentiment-analysis', model="nlptown/bert-base-multilingual-uncased-sentiment")}
|
| 16 |
+
# create a list of keys to use them in the select part of the html code
|
| 17 |
+
listOfKeys = []
|
| 18 |
+
for key in dictOfModels :
|
| 19 |
+
listOfKeys.append(key)
|
| 20 |
+
|
| 21 |
+
def get_prediction(message,model):
|
| 22 |
+
# inference
|
| 23 |
+
results = model(message)
|
| 24 |
+
return results
|
| 25 |
+
|
| 26 |
+
@app.route('/', methods=['GET'])
|
| 27 |
+
def get():
|
| 28 |
+
# in the select we will have each key of the list in option
|
| 29 |
+
return render_template("home.html", len = len(listOfKeys), listOfKeys = listOfKeys)
|
| 30 |
+
|
| 31 |
+
@app.route('/', methods=['POST'])
|
| 32 |
+
def predict():
|
| 33 |
+
message = "This is good movies" #request.form['message']
|
| 34 |
+
# choice of the model
|
| 35 |
+
results = get_prediction(message, dictOfModels['RoBERTa') # get_prediction(message, dictOfModels['request.form.get("model_choice")'])
|
| 36 |
+
print(f'User selected model : {request.form.get("model_choice")}')
|
| 37 |
+
my_prediction = f'The feeling of this text is {results[0]["label"]} with probability of {results[0]["score"]*100}%.'
|
| 38 |
+
return render_template('result.html', text = f'{message}', prediction = my_prediction)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# @app.route('/')
|
| 43 |
+
# def home():
|
| 44 |
+
# print(1)
|
| 45 |
+
# return {'key':"Hello HuggingFace! Successfully deployed. "}
|
| 46 |
+
# # model = load_checkpoint('checkpoint.pth')
|
| 47 |
+
# # print(2)
|
| 48 |
+
# # res = sample(model, obj.maxlen, 'ap')
|
| 49 |
+
# # print(3)
|
| 50 |
+
# # return {'key':res}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|