-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (40 loc) · 1.9 KB
/
app.py
File metadata and controls
50 lines (40 loc) · 1.9 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
from flask import Flask, request, render_template
import pandas as pd
import joblib
app = Flask(__name__)
# โหลดโมเดลที่บันทึกไว้
model = joblib.load('./model/Ensemble_Model.pkl')
# โหลดข้อมูลจาก CSV
employee_data = pd.read_csv('./data/data.csv')
@app.route('/')
def home():
# แปลงข้อมูลพนักงานจาก DataFrame เป็นรูปแบบ dictionary เพื่อแสดงใน HTML
employee_data_html = employee_data.to_dict(orient='records')
return render_template('index.html', employee_data=employee_data_html)
@app.route('/predict', methods=['POST'])
def predict():
# รับค่าจากฟอร์ม
age = int(request.form['age'])
length_of_service = int(request.form['length_of_service'])
gender = int(request.form['gender']) # 0: Male, 1: Female
marital_status = int(request.form['marital_status']) # 0: Single, 1: Married
salary = float(request.form['salary'])
# สร้าง DataFrame สำหรับข้อมูลที่กรอก
input_data = pd.DataFrame([[age, length_of_service, salary, gender, marital_status]],
columns=['Age', 'Length_of_Service', 'Salary', 'Gender', 'Marital_Status'])
# ทำนายผลลัพธ์
prediction = model.predict(input_data)[0]
# ส่งผลลัพธ์กลับไปยังหน้าเว็บ
result = 'Still Employed' if prediction == 1 else 'Resigned'
return render_template(
'index.html',
prediction_text=f'Employee Status: {result}',
employee_data=employee_data.to_dict(orient='records'),
age=age,
length_of_service=length_of_service,
salary=salary,
gender=gender,
marital_status=marital_status
)
if __name__ == "__main__":
app.run(debug=True)