-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
53 lines (44 loc) · 1.92 KB
/
app.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
from flask import Flask, render_template, request
import numpy as np
import pandas as pd
import pickle
# Inisialisasi aplikasi and model prediction
app = Flask(__name__)
model = pickle.load(open("best_model.pkl", "rb"))
rev_model = pickle.load(open("rev_model.pkl", "rb"))
# Routing aplikasi awal -> menampilkan index page
@app.route("/")
def home():
return render_template('index_2.html')
# Routing /prediksi -> prediksi class object
@app.route("/predict", methods=["POST"])
def predict():
# Simpan data fitur object dalam array dan diubah ke float
# float_features = np.array([float(x) for x in request.form.values()])
#REV:
area = float(request.form.get('area'))
perimeter = float(request.form.get('perimeter'))
compactness = float(request.form.get('compactness'))
lengthOfKernel = float(request.form.get('lengthOfKernel'))
widthOfKernel = float(request.form.get('widthOfKernel'))
asymmetryCoefficient = float(request.form.get('asymmetryCoefficient'))
lengthOfKernelGroove = float(request.form.get('lengthOfKernelGroove'))
float_features = np.array([area, perimeter, lengthOfKernel, widthOfKernel, lengthOfKernelGroove])
# Buat dalam array 2D karena model hanya menerima 2D array
features = np.array([float_features])
# Prediksi class object dengan model yang telah dibuat
# prediction = model.predict(features)
#REV:
prediction = rev_model.predict(features)
# Karena class prediksi berupa int, ubah ke bentuk
if(prediction==np.array([0])):
res = "Canadian Wheat"
elif(prediction==np.array([1])):
res = "Rosa Wheat"
elif(prediction==np.array([2])):
res = "Kama Wheat"
# Mengembalikan hasil prediksi ke index.html dalam variabel result
return render_template("index_2.html", result = "{}".format(res))
# Menjalankan aplikasi dengan konfigurasi host dan debug mode
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)