-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (64 loc) · 2.43 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
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
import math
from flask import Flask, render_template, request, jsonify
import pickle
import pandas as pd
import numpy as np
import mariadb
import jinja2
conn = mariadb.connect(
user="root",
password="root",
host="localhost",
database="pal_taqdeer")
cur = conn.cursor()
app = Flask(__name__)
@app.route('/')
def index():
query = "select * from student_forecast"
cur.execute(query)
data = list(cur)
return render_template("index.html", data=data)
with open("model.pkl", "rb") as f:
model = pickle.load(f)
@app.route('/result', methods=['POST'])
def result():
age = request.form.get('age')
medu = request.form.get('medu')
fedu = request.form.get('fedu')
failures = request.form.get('failures')
higher = request.form.get('higher')
romantic = request.form.get('romantic')
g1 = request.form.get('g1')
g2 = request.form.get('g2')
gouout = request.form.get('gouout')
if not age or int(age) < 15 or int(age) > 22:
return jsonify({'error' : 'Invalid Age'})
if medu == "":
return jsonify({'error' : 'Invalid Mother Education Status'})
if fedu == "":
return jsonify({'error' : 'Invalid Father Education Status'})
if failures == "" or int(failures) > 4:
return jsonify({'error' : 'Invalid Failures No.'})
if not higher:
return jsonify({'error' : 'Invalid Higher Education Status'})
if not romantic:
return jsonify({'error' : 'Invalid Romantic Status'})
if g1 == "" or int(g1) > 20:
return jsonify({'error' : 'Invalid First Period Grade'})
if g2 == "" or int(g2) > 20:
return jsonify({'error' : 'Invalid Second Period Grade'})
if not int(gouout) or int(gouout) > 5:
return jsonify({'error' : 'Invalid Hang Out Status'})
inputs = [age, medu, fedu, failures,
higher, g1, g2, romantic, gouout]
df = pd.DataFrame([np.array(inputs)], columns=[
'age', 'Medu', 'Fedu', 'failures', 'higher_yes', 'G1', 'G2', 'romantic_no', 'goout'])
finalGrade = model.predict(df)
final_grade_rounded = round(float(finalGrade))
query = f""" INSERT INTO student_forecast (age, medu, fedu, failures, higher, romantic, g1, g2, gouout)
VALUES ('{age}','{medu}','{fedu}','{failures}','{higher}','{romantic}','{g1}','{g2}','{gouout}'); """
cur.execute(query)
conn.commit()
return jsonify({'grade': final_grade_rounded})
if __name__ == "__main__":
app.run(debug=True)