File size: 5,119 Bytes
b1bae0d |
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 144 145 146 147 148 149 150 151 152 153 |
from flask import Flask, render_template, request, url_for
import pickle
import numpy as np
app = Flask(__name__, static_folder='static')
linreg = pickle.load(open('Models/linreg_model.pkl', 'rb'))
knn_model = pickle.load(open('Models/knn_model.pkl', 'rb'))
gaussian_nb = pickle.load(open('Models/nbG_model.pkl', 'rb'))
multinomial_nb = pickle.load(open('Models/nbM_model.pkl', 'rb'))
bernoulli_nb = pickle.load(open('Models/nbB_model.pkl', 'rb'))
job_map = {
1: 'Junior',
2: 'Senior',
3: 'Project Manager',
4: 'CTO',
}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/algos')
def algos():
return render_template('algos.html')
@app.route('/linear', methods=['GET', 'POST'])
def linear():
return render_template('linear.html')
@app.route('/knn', methods=['GET', 'POST'])
def knn():
return render_template('knn.html')
@app.route('/kmeans', methods=['GET', 'POST'])
def kmeans():
return render_template('kmeans.html')
@app.route('/naive', methods=['GET', 'POST'])
def naive():
return render_template('naive.html')
@app.route('/predict', methods=['POST'])
def predict():
position_level = request.form.get('comp_select')
experience_str = request.form.get('experience')
try:
experience = float(experience_str)
except ValueError:
return render_template('linear.html', prediction_text=f"Error: Invalid input value for experience: '{experience_str}'. Please enter a valid numerical value.")
if position_level in ['1', '2', '3', '4']:
int_position_level = int(position_level)
float_experience = float(experience)
int_features = [int_position_level, float_experience]
final_features = [np.array(int_features)]
prediction = linreg.predict(final_features)
int_position_level = job_map.get(int(position_level))
predicted_salary_f = round(float(prediction.item()), 3)
predicted_salary = "{:,.3f}".format(predicted_salary_f)
return render_template('linear.html', position_level=f'Position: {int_position_level}',experience=f'Experience: {experience}', prediction_text=f'Predicted Salary Rate: ₱{predicted_salary}')
else:
return render_template('linear.html', prediction_text='Error: Invalid input values. Please select a valid position level and enter a numerical value for experience.')
@app.route('/predictknn', methods=['POST'])
def predictknn():
experience_str = request.form.get('experience')
salary_str = request.form.get('salary')
try:
experience = float(experience_str)
salary = float(salary_str)
except ValueError:
return render_template('knn.html', prediction_text=f"Error: Invalid input value. Please enter a valid numerical value for both experience and salary.")
features = [[experience, salary]]
prediction = knn_model.predict(features)
predicted_job_num = int(prediction[0])
predicted_job = job_map[predicted_job_num]
return render_template('knn.html', prediction_text=f'Predicted job: {predicted_job}', experience=f'Experience: {experience}', salary=f'Salary: {salary}')
@app.route('/predictnaive', methods=['GET', 'POST'])
def predictnaive():
# Get the user's input values
salary = float(request.form['salary'])
experience = float(request.form['experience'])
try:
if float(experience) < 0 or float(salary) < 0:
raise ValueError()
int_features = [salary, experience]
features = np.array(int_features).reshape(1, -1)
gaussian_prediction = gaussian_nb.predict(features)
multinomial_prediction = multinomial_nb.predict(features)
bernoulli_prediction = bernoulli_nb.predict(features)
# # Map the predicted job titles to their corresponding string values
gaussian_prediction = job_map.get(int(gaussian_prediction))
multinomial_prediction = job_map.get(int(multinomial_prediction))
bernoulli_prediction = job_map.get(int(bernoulli_prediction))
# Render the results template with the predicted job classification and accuracy scores
return render_template('naive.html',
gaussian_prediction=gaussian_prediction,
multinomial_prediction=multinomial_prediction,
bernoulli_prediction=bernoulli_prediction,
salary=salary,
experience=experience,
reset=True)
except:
return render_template('naive.html')
@app.route('/predictkm', methods=['GET'])
def predictkm():
# render the HTML template
return render_template('kmeans.html')
# # convert the figure to a base64 string for embedding in the HTML template
# import io
# import base64
# buf = io.BytesIO()
# fig.savefig(buf, format='png')
# figdata = base64.b64encode(buf.getbuffer()).decode('utf-8')
# # render the HTML template and pass the figure data to it
# return render_template('kmeans.html', figdata=figdata)
if __name__ == '__main__':
app.run(debug=True, port=8000) |