File size: 996 Bytes
d35f33e
 
 
 
 
f0372d1
d35f33e
 
 
 
 
 
 
 
f0372d1
d35f33e
 
 
 
 
 
 
 
 
 
 
f0372d1
d35f33e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
import tensorflow as tf
import tensorflow_addons as tfa

from constance_data import emotion_track_list, decode_cut_list
from pre_processing_data import preprocessing_data, pre_processing_data_2, text_transform, user_capture


def emotion_predict(sentence: str):
    lr = 1e-3
    wd = 1e-4 * lr
    model = tf.keras.models.load_model("model/nlp_surrey_coursework_hunglenhat")
    model.compile(loss='sparse_categorical_crossentropy',
                  optimizer=tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd), metrics=['accuracy'])
    sentence_temp = sentence
    sentence = pre_processing_data_2(sentence)
    if not sentence:
        sentence = preprocessing_data(sentence)

    sentence = text_transform(sentence)
    try:
        sentence = model.predict(sentence)
    except Exception as E:
        print(E)
    index_max = np.argmax(sentence)
    result = emotion_track_list[decode_cut_list[index_max]]
    user_capture(sentence_temp, result)
    return result