-
Notifications
You must be signed in to change notification settings - Fork 89
/
webservice.py
212 lines (173 loc) · 6.3 KB
/
webservice.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import sys
from flask import Flask, jsonify, request, make_response, abort
import os
import nltk
import numpy as np
import pandas as pd
import sklearn
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
import time
import logging
import pickle
import re
# Use with Azure Web Apps
os.environ['PATH'] = r'D:\home\python354x64;' + os.environ['PATH']
sys.path.append(".")
sys.path.append("..")
sys.path.append("webservice/models")
sys.path.append("wwwroot/models")
app = Flask(__name__)
__location__ = os.path.realpath(os.path.join(
os.getcwd(), os.path.dirname(__file__), 'models'))
# Download models
from models.download_models import download_file, download_models
download_models()
# Loading models
model_impact = pickle.load(
open(
os.path.join(__location__, "impact.model"), "rb"
)
)
model_ticket_type = pickle.load(
open(
os.path.join(__location__, "ticket_type.model"), "rb"
)
)
model_category = pickle.load(
open(
os.path.join(__location__, "category.model"), "rb"
)
)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.route('/')
def index():
return """
<html>
<body>
Hello, World!<br>
This is a sample web service written in Python using <a href=""http://flask.pocoo.org/"">Flask</a> module.<br>
</body>
</html>
"""
@app.route('/endava/api/v1.0/predictall', methods=['POST'])
def predictall():
ts = time.gmtime()
logging.info("Request received - %s" % time.strftime("%Y-%m-%d %H:%M:%S", ts))
if (not request.json) or ('description' not in request.json):
abort(400)
description = request.json['description']
description = preprocess_data(description)
predicted_ticket_type = model_ticket_type.predict([description])[0]
print("predicted ticket_type: "+str(predicted_ticket_type))
predicted_category = model_category.predict([description])[0]
print("predicted category: "+str(predicted_category))
predicted_impact = model_impact.predict([description])[0]
print("predicted impact: "+str(predicted_impact))
# predicted_business_service = model_business_service.predict([description])[0]
# print("predicted business_service: "+str(predicted_business_service))
# predicted_urgency = model_urgency.predict([description])[0]
# print("predicted urgency: "+str(predicted_urgency))
ts = time.gmtime()
logging.info(
"Request sent to evaluation - %s"
% time.strftime("%Y-%m-%d %H:%M:%S", ts)
)
return jsonify({
"description": description,
"ticket_type": predicted_ticket_type,
# "business_service": predicted_business_service,
"category": predicted_category,
"impact": predicted_impact
})
@app.route('/endava/api/v1.0/category', methods=['POST'])
def category1():
ts = time.gmtime()
logging.info("Request received - %s" % time.strftime("%Y-%m-%d %H:%M:%S", ts))
print(request)
print(request.json)
if not request.json or 'description' not in request.json:
abort(400)
description = request.json['description']
print(description)
predicted = model_category.predict([description])
print("Predicted: "+str(predicted))
ts = time.gmtime()
logging.info("Request sent to evaluation - %s" % time.strftime("%Y-%m-%d %H:%M:%S", ts))
return jsonify({"category": predicted[0]})
@app.route('/endava/api/v1.0/tickettype', methods=['POST'])
def tickettype():
ts = time.gmtime()
logging.info("Request received - %s" % time.strftime("%Y-%m-%d %H:%M:%S", ts))
print(request)
print(request.json)
if not request.json or 'description' not in request.json:
abort(400)
description = request.json['description']
print(description)
predicted = model_ticket_type.predict([description])
print("Predicted: " + str(predicted))
ts = time.gmtime()
logging.info("Request sent to evaluation - %s" % time.strftime("%Y-%m-%d %H:%M:%S", ts))
return jsonify({"ticket_type": predicted[0]})
# Data prep - much to improve :)
regexArr1 = []
regexArr2 = []
def getRegexList1():
regexList = []
regexList += ['From:(.*)'] # from line
regexList += ['Sent:(.*)'] # sent to line
regexList += ['Received:(.*)'] # received data line
regexList += ['To:(.*)'] # to line
regexList += ['CC:(.*)'] # cc line
regexList += ['https?:[^\]\n\r]+'] # https & http
regexList += ['Subject:']
regexList += ['[\w\d\-\_\.]+@[\w\d\-\_\.]+'] # emails
return regexList
def getRegexList2():
regexList = []
regexList += ['From:'] # from line
regexList += ['Sent:'] # sent to line
regexList += ['Received:'] # received data line
regexList += ['To:'] # to line
regexList += ['CC:'] # cc line
regexList += ['The information(.*)infection'] # footer
regexList += ['Endava Limited is a company(.*)or omissions'] # footer
regexList += ['The information in this email is confidential and may be legally(.*)interference if you are not the intended recipient'] # footer
regexList += ['\[cid:(.*)]'] # images cid
regexList += ['https?:[^\]\n\r]+'] # https & http
regexList += ['Subject:']
regexList += ['[\w\d\-\_\.]+@[\w\d\-\_\.]+'] # emails
regexList += ['[\\r]'] # \r\n
regexList += ['[\\n]'] # \r\n
regexList += ['^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$']
regexList += ['[^a-zA-Z]']
return regexList
def preprocess_data(data):
print(data)
content = data.lower()
content = content.split('\\n')
for word in content:
for regex in regexArr1:
word = re.sub(regex.lower(), ' ', word)
print(content)
content = "".join(content)
print(content)
for regex in regexArr2:
content = re.sub(regex.lower(), ' ', content)
print(content)
return content
if __name__ == '__main__':
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
regexArr1 = getRegexList1()
regexArr2 = getRegexList2()
app.run(HOST, PORT)