-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
148 lines (90 loc) · 3.49 KB
/
app.py
File metadata and controls
148 lines (90 loc) · 3.49 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
## dependencies
from flask import Flask, render_template, request, jsonify
from typing import Annotated, Dict
from model.ESP import predict_exam_score, VERSION, NAME
from schema.user_input import StudentInputDetails
## some crucial details
API_NAME = 'ESP-API'
API_VERSION = '1.0.0'
MODEL_VERSION: str = VERSION
MODEL_NAME: str = NAME
## creating app instance
app = Flask(__name__)
## -------------------------------------------- home page --------------------------------------------- #
@app.route("/")
def home_page():
''' This function will render the home HTML page when user fill form '''
return render_template('home.html')
@app.route("/home", methods=['GET','POST'])
def home():
''' This function will render the home HTML page when user fill form '''
## extracting values from form
try:
study_hrs = float(request.form['study-hrs'])
exercise_frequency = int(request.form['exercise-frequency'])
mental_health_rating = int(request.form['mental-health-rating'])
except Exception as e:
# this is a heuristic method
# without this our code will be not work
if (str(e).startswith('400')):
return render_template('home.html', message="")
error_details: Dict[str,str] = {
'exception occur': str(e),
'message': 'any input type mismatch'
}
return jsonify(error_details)
## predicting exam score
predicted_exam_score: float = predict_exam_score(
exercise_frequency = exercise_frequency,
study_hours = study_hrs,
mental_health_rating = mental_health_rating
)
## defining message that will be direct seen by the user
message = f"You will achieve around {round(predicted_exam_score,2)}% in your exam."
return render_template('home.html', message=message)
## --------------------- API -------------------- ##
@app.route('/api')
def api() -> jsonify:
''' This function contains script for API '''
## defining queries
try:
study_hrs = float(request.args.get('sh'))
exercise_frequency = int(request.args.get('ef'))
mental_health_rating = int(request.args.get('mhr'))
except TypeError:
data: Dict[str,str] = {
'status': "success",
'details': f"welcome in {MODEL_NAME} ({MODEL_VERSION}) ",
'queries details': {
'sh': 'study hours [float]',
'ef': 'exercise frequency [integer]',
'mhr': 'mental health rating (0-10)[integer]',
}
}
return jsonify(data)
## predicting exam score
exam_score = round(
predict_exam_score(
study_hours = study_hrs,
exercise_frequency = exercise_frequency,
mental_health_rating = mental_health_rating
), 2)
## managing data for return
data: Dict[str, str | float] = {
'status': 'success',
'status code': 200,
'model details': {
'name': MODEL_NAME,
'version': MODEL_VERSION
},
'predicted exam score': exam_score,
'input details': {
'study hrs': study_hrs,
'mental health rating': mental_health_rating,
'exercise frequency': exercise_frequency
}
}
return jsonify(data) # returning data
if __name__ == "__main__": # --------------------------------------------------------------------------- Main execution
## running flask app
app.run(debug=True)