-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_api.py
89 lines (81 loc) · 2.2 KB
/
flask_api.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
import os
import numpy as np
import flask
import json
import requests
from flask import Flask, render_template, request
# creating instance of the class
app = Flask(__name__)
# to tell flask what url shoud trigger the function index()
@app.route('/')
@app.route('/index')
def index():
return flask.render_template('index.html')
# prediction function
def ValuePredictor(jsondata):
headers = {'Content-Type': 'application/json'}
uri = "http://20.185.111.89:80/score"
resp = requests.post(uri, jsondata, headers = headers)
finalclass = int(resp.text.strip('[]'))
return finalclass
@app.route('/result', methods = ['POST'])
def result():
if request.method == 'POST':
list1=[]
description = request.form['description']
quantity = request.form['quantity']
unitprice = request.form['unitprice']
timestamp = request.form['timestamp']
dictionary = {"description": description, "quantity": quantity, "unitprice": unitprice, "timestamp": timestamp}
list1.append(dictionary)
final_dict = {"data": list1}
jsondata = json.dumps(final_dict)
result = ValuePredictor(jsondata)
country = {
36: 'United Kingdom',
13: 'France',
0: 'Australia',
24: 'Netherlands',
14: 'Germany',
25: 'Norway',
10: 'EIRE',
33: 'Switzerland',
31: 'Spain',
26: 'Poland',
27: 'Portugal',
19: 'Italy',
3: 'Belgium',
22: 'Lithuania',
20: 'Japan',
17: 'Iceland',
6: 'Channel Islands',
9: 'Denmark',
7: 'Cyprus',
32: 'Sweden',
1: 'Austria',
18: 'Israel',
12: 'Finland',
2: 'Bahrain',
15: 'Greece',
16: 'Hong Kong',
30: 'Singapore',
21: 'Lebanon',
35: 'United Arab Emirates',
29: 'Saudi Arabia',
8: 'Czech Republic',
5: 'Canada',
37: 'Unspecified',
4: 'Brazil',
34: 'USA',
11: 'European Community',
23: 'Malta',
28: 'RSA'
}
if result in country:
prediction = country[result]
return render_template("index.html", prediction = prediction)
else:
prediction = "Not found"
return render_template("index.html", prediction = prediction)
if __name__ == '__main__':
app.run(debug = True, threaded=True)