-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
183 lines (168 loc) · 5.71 KB
/
api.py
File metadata and controls
183 lines (168 loc) · 5.71 KB
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
# run: python api.py
from crypt import methods
from flask import Flask, request, make_response, send_file
from flasgger import Swagger
from stemming.porter2 import stem
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.cluster import KMeans
from keras.datasets import mnist
from keras.models import load_model
from PIL import Image
import matplotlib.pyplot as plt
from io import BytesIO
import time
import zipfile
import pickle
import pandas as pd
import numpy as np
with open("./pickles/forest.pkl", "rb") as model_file: # model to predict from
model_forest = pickle.load(model_file)
model_img = load_model('./model_img.h5')
app = Flask(__name__)
swagger = Swagger(app) # http://127.0.0.1:5050/apidocs/
# IRIS API
@app.route("/iris", methods=["GET"]) # http://127.0.0.1:5050/iris?s_length=5.7&s_width=5.6&p_length=4.3&p_width=7.8
def iris():
"""
Returns a prediction of iris.
---
parameters:
- name: s_length
in: query
type: number
required: true
- name: s_width
in: query
type: number
required: true
- name: p_length
in: query
type: number
required: true
- name: p_width
in: query
type: number
required: true
responses:
200:
description: OK
"""
s_length = request.args.get("s_length")
s_width = request.args.get("s_width")
p_length = request.args.get("p_length")
p_width = request.args.get("p_width")
prediction = model_forest.predict(np.array([[s_length, s_width, p_length, p_width]]))
return str(prediction)
@app.route("/iris_file", methods=["POST"]) # http://127.0.0.1:5050/iris_file
def iris_input():
"""
Returns a prediction of iris given a csv file.
---
parameters:
- name: input_file
in: formData
type: file
required: true
responses:
200:
description: OK
"""
input_file = pd.read_csv(request.files.get("input_file"), header=None) # form-data
prediction = model_forest.predict(input_file)
return str(prediction)
# TEXT API
def clean_text(txt):
if txt:
cleaned = " ".join(txt.split()) # remove whitespaces
reduced_text = [stem(word) for word in cleaned.split()] # stemming
return " ".join(reduced_text)
else:
return txt
@app.route("/cluster", methods=["POST"])
def cluster():
"""
Returns clusters given a csv file. Can pass the name of the text column as query param.
---
parameters:
- name: dataset
in: formData
type: file
required: true
- name: col
in: query
type: string
required: false
responses:
200:
description: OK
"""
data = pd.read_csv(request.files["dataset"])
unstructure = "text"
if "col" in request.args:
unstructure = request.args.get("col")
no_of_clusters = 2
if "no_of_clusters" in request.args:
no_of_clusters = int(request.args.get("no_of_clusters"))
data = data.fillna("NULL")
data["clean_sum"] = data[unstructure].apply(clean_text)
vectorizer = CountVectorizer(analyzer="word", stop_words="english") # matrix with words frequency
counts = vectorizer.fit_transform(data["clean_sum"]) # get only the clean column
kmeans = KMeans(n_clusters=no_of_clusters)
data["cluster_num"] = kmeans.fit_predict(counts)
data = data.drop(["clean_sum"], axis=1)
output = BytesIO()
writer = pd.ExcelWriter(output, engine="xlsxwriter")
data.to_excel(writer, sheet_name="clusters", encoding="utf-8", index=False)
clusters = []
for i in range(np.shape(kmeans.cluster_centers_)[0]): # new sheet to display top 10 keywords for each text
data_cluster = pd.concat([pd.Series(vectorizer.get_feature_names()), pd.DataFrame(kmeans.cluster_centers_[i])], axis=1)
data_cluster.columns = ['keywords', 'weights']
data_cluster = data_cluster.sort_values(by=['weights'], ascending=False)
data_clust = data_cluster.head(n=10)['keywords'].tolist()
clusters.append(data_clust)
pd.DataFrame(clusters).to_excel(writer, sheet_name='top_keywords', encoding='utf-8')
# pivot
data_pivot = data.groupby(['cluster_num'], as_index=False).size()
data_pivot.name = 'size'
data_pivot = data_pivot.reset_index()
data_pivot.to_excel(writer, sheet_name='cluster_report', encoding='utf-8', index=False)
# insert chart
workbook = writer.book
worksheet = writer.sheets['cluster_report']
chart = workbook.add_chart({'type': 'column'})
chart.add_series({ 'values': '=cluster_report!$C$2:$C'+str(no_of_clusters+1) })
worksheet.insert_chart('D2', chart)
writer.save()
memory_file = BytesIO()
with zipfile.ZipFile(memory_file, "w") as zf:
names = ["cluster_output.xlsx"]
files = [output]
for i in range(len(files)):
data = zipfile.ZipInfo(names[i])
data.date_time = time.localtime(time.time())
data.compress_type = zipfile.ZIP_DEFLATED
zf.writestr(data, files[i].getvalue())
memory_file.seek(0)
response = make_response(send_file(memory_file, as_attachment=True, attachment_filename="cluster_output.zip"))
response.headers["Content-Disposition"] = "atachment;filename=cluster_output.zip"
response.headers["Access-Control-Allow-Origin"] = "*"
return response
# IMAGE API
@app.route('/digit', methods=['POST'])
def predict_digit():
"""Returns a prediction of mnist digit
---
parameters:
- name: image
in: formData
type: file
required: true
responses:
200:
description: OK
"""
img = Image.open(request.files['image'])
img2arr = np.array(img).reshape((1, 1, 28, 28))
return str(np.argmax(model_img.predict(img2arr)))
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5050)