-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.py
56 lines (39 loc) · 1.41 KB
/
server.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
#In this file, we will use the flask web framework to handle the POST requests that we will get from the request.py and from HTML file
#import packages
import os
import numpy as np
import flask
from flask import Flask, request, jsonify, render_template
import pickle
app = Flask(__name__)
model = pickle.load(open('models/regressor.pkl','rb'))
#to tell flask what url shoud trigger the function index()
@app.route('/')
@app.route('/index')
def index():
return flask.render_template('index.html')
# get data from the html form and perform prediction
@app.route('/result',methods = ['POST'])
def result():
if request.method == 'POST':
data = request.form['year']
input = float(data)
# convert the data into numpy array and perform prediction
prediction = model.predict([[np.array(input)]])
output = prediction[0]
# round output into two decimals
output = round(output, 2)
return render_template("result.html", prediction=output, years = data)
# get data from script file and perfrom prediction
@app.route('/api',methods=['POST'])
def predict():
#get the data in json format
data = request.get_json(force=True)
#convert the data into numpy array and perform prediction
prediction = model.predict([[np.array(data['exp'])]])
output = prediction[0]
#return result in json format
return jsonify(output)
# set port into 5000 and debug is True
if __name__ == '__main__':
app.run(port=5000, debug=True)