-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
81 lines (65 loc) · 1.71 KB
/
inference.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
76
77
78
79
80
81
"""
inference.py - module for intent-classification inference
"""
# import dependencies
import os
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
class IntentClassifier:
"""
IntentClassifier - class for intent-classification
"""
def __init__(self, model, vectorizer, intents):
"""
Constructor for IntentClassifier class
Support hosting SVM model
Inputs:
- model : str or sklearn model
Path to movel weights
- vectorizer : str or Sklearn Vectorizer
Path to vectorizer-file or Sklearn Vectorizer object
- intents : str or list of intents
Path to intent_list of list of intents
"""
# load model
print("Loading IntentClassifier model")
if isinstance(model, str):
self.model = pickle.load(open(model, 'rb'))
else:
self.modle = model
# load intents
if isinstance(intents, str):
with open(intents) as file:
self.intents = file.read().split('\n')
else:
self.intents = intents
# load vectorizer
if isinstance(vectorizer, str):
self.vectorizer = pickle.load(open(vectorizer, 'rb'))
else:
self.vectorizer = vectorizer
def process_text(self, text):
"""
Function to process text: lowercase, remove punctuations and stopwords
"""
# convert to list
if isinstance(text, str):
text = [text]
return self.vectorizer.transform(text)
def predict(self, input):
"""
prediction - function to make predictions
Inputs:
- input : str
Raw text
Outputs:
- output : str
Correct intent tag
"""
# extract TF-iDF features
input = self.process_text(input)
# make predictions
output = self.model.predict(input)
# parse raw-predictions to correct intent class
output = self.intents[output[0]]
return output