-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
75 lines (59 loc) · 2.5 KB
/
predict.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
from tensorflow import keras
class TopicModellingModel :
def __init__(self):
dir_name = os.getcwd()
threshold_path = os.path.join(dir_name,'model/best_t.npy')
label_list = os.path.join(dir_name,'model/label_list.npy')
model_path = os.path.join(dir_name,'model/tm_use.keras')
assert (
os.path.exists(threshold_path)
and os.path.exists(label_list)
and os.path.exists(model_path)
), "model files not existent"
use_encoder = "https://tfhub.dev/google/universal-sentence-encoder/4"
print("[USE inference] Load use embedding")
self.embed = hub.load(use_encoder)
print("[USE INFERENCE] done")
print("[USE INFERENCE] Load classification model")
self.model = keras.models.load_model(model_path)
print("[USE INFERENCE] done")
print("[USE INFERENCE] Load threshold and label")
self.best_t = np.load(threshold_path)
self.labels = np.load(label_list, allow_pickle=True)
print("[USE INFERENCE] done")
def run_inference(self,input_string) :
print("[USE INFERENCE] Run inference")
print("[USE INFERENCE] Run embedding")
embedding = self.embed([input_string])
print("[USE INFERENCE] Run classification")
y_pred = self.model.predict(embedding)
print("[USE INFERENCE] Run threshold")
output_classification = (y_pred > self.best_t).astype(np.float32)[0]
print("[USE INFERENCE] inference done")
print("[USE INFERENCE] Run id to tag")
output_labels = [
self.labels[i]
for i in range(len(output_classification))
if output_classification[i] > 0
]
print("[USE INFERENCE] done")
return output_labels
if __name__ == "__main__" :
print("[USE INFERENCE] run local tests")
use_topic_model = TopicModellingModel()
f = open('C:\dev\\topic_modelling\API\\tests\c_posts.txt','r')
contents = f.read()
output = use_topic_model.run_inference(contents)
print(output)
f = open('C:\dev\\topic_modelling\API\\tests\json_posts.txt','r')
contents = f.read()
output = use_topic_model.run_inference(contents)
print(output)
f = open('C:\dev\\topic_modelling\API\\tests\pandas_python.txt','r')
contents = f.read()
output = use_topic_model.run_inference(contents)
print(output)