ahmadardhy commited on
Commit
bf84b00
1 Parent(s): 0c6bfe8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -7,6 +7,7 @@ from joblib import load
7
  import mysql.connector
8
  from mysql.connector import Error
9
  import logging
 
10
 
11
  # =[Variabel Global]=============================
12
  app = Flask(__name__, static_url_path='/static')
@@ -47,6 +48,16 @@ def apiDeteksi():
47
  kesesuaian_posisi_dg_skills = int(request.form['Kesesuaian_Posisi_Skill'])
48
  kesesuaian_posisi_dg_pengalaman = int(request.form['Kesesuaian_Posisi_Pengalaman'])
49
 
 
 
 
 
 
 
 
 
 
 
50
  # Perform prediction
51
  data = {**kategori_umur_dict,
52
  "Pendidikan": pendidikan,
@@ -55,10 +66,6 @@ def apiDeteksi():
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
@@ -68,7 +75,7 @@ def apiDeteksi():
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
@@ -98,21 +105,22 @@ def apiDeteksi():
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.")
102
  except Error as e:
103
- print("Error while connecting to MySQL", e)
104
  finally:
105
  if connection.is_connected():
106
  cursor.close()
107
  connection.close()
108
- print("MySQL connection is closed")
109
 
110
  # Return prediction result as JSON
111
  return jsonify({
112
  "prediksi": hasil_prediksi_str
113
  })
114
  except Exception as e:
115
- print(f"Error during prediction: {e}")
 
116
  return jsonify({
117
  "error": "Error during prediction"
118
  })
@@ -141,12 +149,12 @@ def view_tabel():
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)
145
  finally:
146
  if connection.is_connected():
147
  cursor.close()
148
  connection.close()
149
- print("MySQL connection is closed")
150
 
151
  # =[Main]========================================
152
 
@@ -154,9 +162,9 @@ 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)
 
7
  import mysql.connector
8
  from mysql.connector import Error
9
  import logging
10
+ import traceback
11
 
12
  # =[Variabel Global]=============================
13
  app = Flask(__name__, static_url_path='/static')
 
48
  kesesuaian_posisi_dg_skills = int(request.form['Kesesuaian_Posisi_Skill'])
49
  kesesuaian_posisi_dg_pengalaman = int(request.form['Kesesuaian_Posisi_Pengalaman'])
50
 
51
+ # Log input data
52
+ logging.debug(f"Nama: {nama}")
53
+ logging.debug(f"Posisi Harapan: {posisi_harapan}")
54
+ logging.debug(f"Usia: {usia}")
55
+ logging.debug(f"Pendidikan: {pendidikan}")
56
+ logging.debug(f"Lama Pengalaman: {lama_pengalaman}")
57
+ logging.debug(f"Kesesuaian Posisi Skill: {kesesuaian_posisi_dg_skills}")
58
+ logging.debug(f"Kesesuaian Posisi Pengalaman: {kesesuaian_posisi_dg_pengalaman}")
59
+ logging.debug(f"Kategori Umur: {kategori_umur_dict}")
60
+
61
  # Perform prediction
62
  data = {**kategori_umur_dict,
63
  "Pendidikan": pendidikan,
 
66
  "Kesesuaian_Posisi_Pengalaman": kesesuaian_posisi_dg_pengalaman}
67
 
68
  df_test = pd.DataFrame(data, index=[0])
 
 
 
 
69
  logging.debug(f"DataFrame for prediction:\n{df_test}")
70
 
71
  # Check if model is loaded
 
75
  # Perform prediction
76
  hasil_prediksi = model.predict(df_test)[0]
77
 
78
+ # Log prediction result
79
  logging.debug(f"Prediction result: {hasil_prediksi}")
80
 
81
  # Map prediction to a string
 
105
 
106
  cursor.execute(insert_query, (nama,) + tuple(data.values()) + (posisi_harapan, usia, hasil_prediksi_str))
107
  connection.commit()
108
+ logging.debug("Data berhasil disimpan ke database.")
109
  except Error as e:
110
+ logging.error(f"Error while connecting to MySQL: {e}")
111
  finally:
112
  if connection.is_connected():
113
  cursor.close()
114
  connection.close()
115
+ logging.debug("MySQL connection is closed")
116
 
117
  # Return prediction result as JSON
118
  return jsonify({
119
  "prediksi": hasil_prediksi_str
120
  })
121
  except Exception as e:
122
+ logging.error(f"Error during prediction: {e}")
123
+ logging.error(traceback.format_exc())
124
  return jsonify({
125
  "error": "Error during prediction"
126
  })
 
149
 
150
  return render_template('view_tabel.html', data_tabel=data_tabel)
151
  except Error as e:
152
+ logging.error(f"Error while connecting to MySQL: {e}")
153
  finally:
154
  if connection.is_connected():
155
  cursor.close()
156
  connection.close()
157
+ logging.debug("MySQL connection is closed")
158
 
159
  # =[Main]========================================
160
 
 
162
  # Load model yang telah ditraining
163
  try:
164
  model = load('model_scratch_2.model')
165
+ logging.debug("Model loaded successfully")
166
  except Exception as e:
167
+ logging.error(f"Error loading model: {e}")
168
 
169
  # Run Flask di localhost
170
+ app.run(host="localhost", port=5000, debug=True)