Commit
•
0c6bfe8
1
Parent(s):
40cee5b
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
# =[Modules dan Packages]========================
|
2 |
import c45
|
3 |
-
from flask import Flask,render_template,request,jsonify
|
4 |
import pandas as pd
|
5 |
import json
|
6 |
from joblib import load
|
@@ -9,14 +9,12 @@ from mysql.connector import Error
|
|
9 |
import logging
|
10 |
|
11 |
# =[Variabel Global]=============================
|
12 |
-
|
13 |
-
app = Flask(__name__, static_url_path='/static')
|
14 |
model = None
|
15 |
|
16 |
-
|
17 |
# =[Routing]=====================================
|
18 |
|
19 |
-
# [Routing untuk Halaman Utama atau Home]
|
20 |
@app.route("/")
|
21 |
def beranda():
|
22 |
return render_template('index.html')
|
@@ -30,17 +28,16 @@ def apiDeteksi():
|
|
30 |
nama = request.form['Nama']
|
31 |
posisi_harapan = request.form['Posisi_Harapan']
|
32 |
usia = int(request.form['Usia'])
|
|
|
33 |
# Extract kategori umur sebagai objek
|
34 |
kategori_umur_obj = request.form.get('KategoriUmur')
|
35 |
-
|
36 |
-
# Check if kategori_umur_obj has a value
|
37 |
if kategori_umur_obj is not None:
|
38 |
# Ubah objek JavaScript menjadi dict Python
|
39 |
kategori_umur_values = json.loads(kategori_umur_obj)
|
40 |
else:
|
41 |
# Jika tidak ada nilai, atur sebagai dictionary kosong
|
42 |
kategori_umur_values = {}
|
43 |
-
|
44 |
# Python Flask code to access category values from formData
|
45 |
kategori_umur_values = {f"KategoriUmur[{i}]": int(request.form.get(f"KategoriUmur[{i}]")) for i in range(1, 11)}
|
46 |
kategori_umur_dict = {f"Kategori_Umur_{i}": kategori_umur_values.get(f"KategoriUmur[{i}]", 0) for i in range(1, 11)}
|
@@ -51,22 +48,28 @@ def apiDeteksi():
|
|
51 |
kesesuaian_posisi_dg_pengalaman = int(request.form['Kesesuaian_Posisi_Pengalaman'])
|
52 |
|
53 |
# Perform prediction
|
54 |
-
data = {**kategori_umur_dict,
|
55 |
"Pendidikan": pendidikan,
|
56 |
"Lama_Pengalaman": lama_pengalaman,
|
57 |
"Kesesuaian_Posisi_Skill": kesesuaian_posisi_dg_skills,
|
58 |
"Kesesuaian_Posisi_Pengalaman": kesesuaian_posisi_dg_pengalaman}
|
59 |
-
|
60 |
df_test = pd.DataFrame(data, index=[0])
|
61 |
print("Data untuk prediksi:")
|
62 |
print(df_test)
|
63 |
-
hasil_prediksi = model.predict(df_test[0:1])[0]
|
64 |
-
# Set up logging
|
65 |
-
logging.basicConfig(level=logging.DEBUG)
|
66 |
|
67 |
-
#
|
68 |
-
logging.debug("
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# Map prediction to a string
|
72 |
if hasil_prediksi == 1:
|
@@ -79,20 +82,20 @@ def apiDeteksi():
|
|
79 |
# Save data to the database
|
80 |
try:
|
81 |
connection = mysql.connector.connect(host='sql12.freesqldatabase.com',
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
if connection.is_connected():
|
86 |
cursor = connection.cursor(prepared=True)
|
87 |
insert_query = ("INSERT INTO klasifikasi "
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
cursor.execute(insert_query, (nama,) + tuple(data.values()) + (posisi_harapan, usia, hasil_prediksi_str))
|
97 |
connection.commit()
|
98 |
print("Data berhasil disimpan ke database.")
|
@@ -116,26 +119,26 @@ def apiDeteksi():
|
|
116 |
|
117 |
# If the request is not POST, render the prediksi.html page
|
118 |
return render_template('prediksi.html')
|
119 |
-
|
120 |
@app.route("/view_tabel", methods=['GET'])
|
121 |
def view_tabel():
|
122 |
try:
|
123 |
connection = mysql.connector.connect(host='sql12.freesqldatabase.com',
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
if connection.is_connected():
|
128 |
cursor = connection.cursor(dictionary=True)
|
129 |
cursor.execute("SELECT * FROM klasifikasi")
|
130 |
data_tabel = cursor.fetchall()
|
131 |
-
|
132 |
# Konversi nilai pendidikan, lama_pengalaman, kesesuaian_posisi_dg_skills, dan kesesuaian_posisi_dg_pengalaman
|
133 |
for row in data_tabel:
|
134 |
row['pendidikan'] = ">= S1" if row['pendidikan'] == 1 else "<S1"
|
135 |
row['lama_pengalaman'] = ">1 Tahun" if row['lama_pengalaman'] == 1 else "<1 Tahun"
|
136 |
row['kesesuaian_posisi_dg_skills'] = "Sesuai Posisi Harapan" if row['kesesuaian_posisi_dg_skills'] == 1 else "Tidak Sesuai Posisi Harapan"
|
137 |
row['kesesuaian_posisi_dg_pengalaman'] = "Sesuai Posisi Harapan" if row['kesesuaian_posisi_dg_pengalaman'] == 1 else "Tidak Sesuai Posisi Harapan"
|
138 |
-
|
139 |
return render_template('view_tabel.html', data_tabel=data_tabel)
|
140 |
except Error as e:
|
141 |
print("Error while connecting to MySQL", e)
|
@@ -144,13 +147,16 @@ def view_tabel():
|
|
144 |
cursor.close()
|
145 |
connection.close()
|
146 |
print("MySQL connection is closed")
|
147 |
-
|
148 |
# =[Main]========================================
|
149 |
|
150 |
if __name__ == '__main__':
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
154 |
|
155 |
-
|
156 |
-
|
|
|
1 |
# =[Modules dan Packages]========================
|
2 |
import c45
|
3 |
+
from flask import Flask, render_template, request, jsonify
|
4 |
import pandas as pd
|
5 |
import json
|
6 |
from joblib import load
|
|
|
9 |
import logging
|
10 |
|
11 |
# =[Variabel Global]=============================
|
12 |
+
app = Flask(__name__, static_url_path='/static')
|
|
|
13 |
model = None
|
14 |
|
|
|
15 |
# =[Routing]=====================================
|
16 |
|
17 |
+
# [Routing untuk Halaman Utama atau Home]
|
18 |
@app.route("/")
|
19 |
def beranda():
|
20 |
return render_template('index.html')
|
|
|
28 |
nama = request.form['Nama']
|
29 |
posisi_harapan = request.form['Posisi_Harapan']
|
30 |
usia = int(request.form['Usia'])
|
31 |
+
|
32 |
# Extract kategori umur sebagai objek
|
33 |
kategori_umur_obj = request.form.get('KategoriUmur')
|
|
|
|
|
34 |
if kategori_umur_obj is not None:
|
35 |
# Ubah objek JavaScript menjadi dict Python
|
36 |
kategori_umur_values = json.loads(kategori_umur_obj)
|
37 |
else:
|
38 |
# Jika tidak ada nilai, atur sebagai dictionary kosong
|
39 |
kategori_umur_values = {}
|
40 |
+
|
41 |
# Python Flask code to access category values from formData
|
42 |
kategori_umur_values = {f"KategoriUmur[{i}]": int(request.form.get(f"KategoriUmur[{i}]")) for i in range(1, 11)}
|
43 |
kategori_umur_dict = {f"Kategori_Umur_{i}": kategori_umur_values.get(f"KategoriUmur[{i}]", 0) for i in range(1, 11)}
|
|
|
48 |
kesesuaian_posisi_dg_pengalaman = int(request.form['Kesesuaian_Posisi_Pengalaman'])
|
49 |
|
50 |
# Perform prediction
|
51 |
+
data = {**kategori_umur_dict,
|
52 |
"Pendidikan": pendidikan,
|
53 |
"Lama_Pengalaman": lama_pengalaman,
|
54 |
"Kesesuaian_Posisi_Skill": kesesuaian_posisi_dg_skills,
|
55 |
"Kesesuaian_Posisi_Pengalaman": kesesuaian_posisi_dg_pengalaman}
|
56 |
+
|
57 |
df_test = pd.DataFrame(data, index=[0])
|
58 |
print("Data untuk prediksi:")
|
59 |
print(df_test)
|
|
|
|
|
|
|
60 |
|
61 |
+
# Debug log to check the DataFrame
|
62 |
+
logging.debug(f"DataFrame for prediction:\n{df_test}")
|
63 |
+
|
64 |
+
# Check if model is loaded
|
65 |
+
if model is None:
|
66 |
+
raise Exception("Model not loaded")
|
67 |
+
|
68 |
+
# Perform prediction
|
69 |
+
hasil_prediksi = model.predict(df_test)[0]
|
70 |
+
|
71 |
+
# Debug log to check the prediction result
|
72 |
+
logging.debug(f"Prediction result: {hasil_prediksi}")
|
73 |
|
74 |
# Map prediction to a string
|
75 |
if hasil_prediksi == 1:
|
|
|
82 |
# Save data to the database
|
83 |
try:
|
84 |
connection = mysql.connector.connect(host='sql12.freesqldatabase.com',
|
85 |
+
database='sql12711336',
|
86 |
+
user='sql12711336',
|
87 |
+
password='ufDVxditnx')
|
88 |
if connection.is_connected():
|
89 |
cursor = connection.cursor(prepared=True)
|
90 |
insert_query = ("INSERT INTO klasifikasi "
|
91 |
+
"(nama, kategori_umur_1, kategori_umur_2, "
|
92 |
+
"kategori_umur_3, kategori_umur_4, kategori_umur_5, kategori_umur_6, "
|
93 |
+
"kategori_umur_7, kategori_umur_8, kategori_umur_9, kategori_umur_10, pendidikan, lama_pengalaman, "
|
94 |
+
"kesesuaian_posisi_dg_skills, kesesuaian_posisi_dg_pengalaman, "
|
95 |
+
"posisi_harapan, usia, hasil_prediksi) "
|
96 |
+
"VALUES "
|
97 |
+
"(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
|
98 |
+
|
99 |
cursor.execute(insert_query, (nama,) + tuple(data.values()) + (posisi_harapan, usia, hasil_prediksi_str))
|
100 |
connection.commit()
|
101 |
print("Data berhasil disimpan ke database.")
|
|
|
119 |
|
120 |
# If the request is not POST, render the prediksi.html page
|
121 |
return render_template('prediksi.html')
|
122 |
+
|
123 |
@app.route("/view_tabel", methods=['GET'])
|
124 |
def view_tabel():
|
125 |
try:
|
126 |
connection = mysql.connector.connect(host='sql12.freesqldatabase.com',
|
127 |
+
database='sql12711336',
|
128 |
+
user='sql12711336',
|
129 |
+
password='ufDVxditnx')
|
130 |
if connection.is_connected():
|
131 |
cursor = connection.cursor(dictionary=True)
|
132 |
cursor.execute("SELECT * FROM klasifikasi")
|
133 |
data_tabel = cursor.fetchall()
|
134 |
+
|
135 |
# Konversi nilai pendidikan, lama_pengalaman, kesesuaian_posisi_dg_skills, dan kesesuaian_posisi_dg_pengalaman
|
136 |
for row in data_tabel:
|
137 |
row['pendidikan'] = ">= S1" if row['pendidikan'] == 1 else "<S1"
|
138 |
row['lama_pengalaman'] = ">1 Tahun" if row['lama_pengalaman'] == 1 else "<1 Tahun"
|
139 |
row['kesesuaian_posisi_dg_skills'] = "Sesuai Posisi Harapan" if row['kesesuaian_posisi_dg_skills'] == 1 else "Tidak Sesuai Posisi Harapan"
|
140 |
row['kesesuaian_posisi_dg_pengalaman'] = "Sesuai Posisi Harapan" if row['kesesuaian_posisi_dg_pengalaman'] == 1 else "Tidak Sesuai Posisi Harapan"
|
141 |
+
|
142 |
return render_template('view_tabel.html', data_tabel=data_tabel)
|
143 |
except Error as e:
|
144 |
print("Error while connecting to MySQL", e)
|
|
|
147 |
cursor.close()
|
148 |
connection.close()
|
149 |
print("MySQL connection is closed")
|
150 |
+
|
151 |
# =[Main]========================================
|
152 |
|
153 |
if __name__ == '__main__':
|
154 |
+
# Load model yang telah ditraining
|
155 |
+
try:
|
156 |
+
model = load('model_scratch_2.model')
|
157 |
+
print("Model loaded successfully")
|
158 |
+
except Exception as e:
|
159 |
+
print(f"Error loading model: {e}")
|
160 |
|
161 |
+
# Run Flask di localhost
|
162 |
+
app.run(host="localhost", port=5000, debug=True)
|