hexselarchieles commited on
Commit
6dd5e3c
·
verified ·
1 Parent(s): 8f4ec9b

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +23 -0
  2. app.py +50 -0
  3. predict.py +37 -0
  4. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1. Tentukan Base Image (Dasar Kontainer)
2
+ # Kami menggunakan Python 3.9 versi ringan (slim) untuk mengurangi ukuran kontainer.
3
+ FROM python:3.9-slim
4
+
5
+ # 2. Tentukan Direktori Kerja di dalam Kontainer
6
+ # Semua perintah selanjutnya akan dieksekusi dari folder /app
7
+ WORKDIR /app
8
+
9
+ # 3. Salin File Kebutuhan dan Instal Pustaka
10
+ # Pertama, salin requirements.txt ke kontainer
11
+ COPY requirements.txt .
12
+ # Kemudian, instal semua pustaka yang ada di requirements.txt
13
+ # --no-cache-dir mengurangi ukuran kontainer
14
+ RUN pip install --no-cache-dir -r requirements.txt
15
+
16
+ # 4. Salin Semua File Proyek Lainnya
17
+ # Salin app.py, predict.py, dan SEMUA file model (.joblib, .h5)
18
+ COPY . .
19
+
20
+ # 5. Perintah untuk Menjalankan Aplikasi
21
+ # Menjalankan Gunicorn (server produksi yang lebih kuat) pada port 5000
22
+ # Ini akan menjalankan app.py Anda.
23
+ CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Untitled4.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1EXuvC4UJ8gxvo2IexM6RnsC9uktSij2N
8
+ """
9
+
10
+ # app.py
11
+ from flask import Flask, request, jsonify
12
+ from predict import get_credit_score # Import fungsi dari predict.py
13
+ import os
14
+
15
+ app = Flask(__name__)
16
+
17
+ @app.route('/')
18
+ def home():
19
+ # Route sederhana untuk mengecek apakah server hidup
20
+ return "Credit Score API is running!"
21
+
22
+ @app.route('/credit_score', methods=['POST'])
23
+ def predict_score():
24
+ try:
25
+ # Ambil data JSON dari body request (dari aplikasi mobile)
26
+ raw_data = request.get_json()
27
+
28
+ if not raw_data:
29
+ return jsonify({"error": "No input data provided."}), 400
30
+
31
+ # Panggil fungsi prediksi
32
+ result = get_credit_score(raw_data)
33
+
34
+ # Kembalikan hasil prediksi ke aplikasi mobile
35
+ return jsonify({
36
+ "status": "success",
37
+ "score": result['score'],
38
+ "keputusan": result['decision']
39
+ })
40
+
41
+ except Exception as e:
42
+ # Handle error jika ada yang salah (misalnya, data input kurang)
43
+ print(f"An error occurred: {e}")
44
+ return jsonify({"error": f"Prediction failed due to an internal error: {str(e)}"}), 500
45
+
46
+ if __name__ == '__main__':
47
+ # Pastikan 'predict.py' dan file model/scalers berada di path yang benar
48
+ # Ganti '0.0.0.0' agar server dapat diakses dari luar (penting untuk deployment)
49
+ port = int(os.environ.get('PORT', 5000))
50
+ app.run(host='0.0.0.0', port=port)
predict.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import tensorflow as tf
3
+ from huggingface_hub import hf_hub_download # PENTING
4
+
5
+ # Tentukan Repositori Model Anda
6
+ REPO_ID = "hexselarchieles/koperasi-ml-models"
7
+
8
+ def load_models():
9
+ """Memuat semua model dan scaler dari Hugging Face Hub."""
10
+ # --- Pemuatan Model Statis (Joblib) ---
11
+ # Unduh file model dan scaler ke cache lokal
12
+ lgbm_path = hf_hub_download(repo_id=REPO_ID, filename="lgbm_model.joblib")
13
+ scaler_static_path = hf_hub_download(repo_id=REPO_ID, filename="scaler_static.joblib")
14
+
15
+ lgbm_model = joblib.load(lgbm_path)
16
+ scaler_static = joblib.load(scaler_static_path)
17
+
18
+ # --- Pemuatan Model LSTM (TensorFlow/H5) ---
19
+ lstm_path = hf_hub_download(repo_id=REPO_ID, filename="lstm_model.h5")
20
+ scaler_ts_path = hf_hub_download(repo_id=REPO_ID, filename="scaler_ts.joblib")
21
+
22
+ # NOTE: Model LSTM biasanya dimuat dari folder, ini mungkin perlu sedikit penyesuaian:
23
+ # lstm_model = tf.keras.models.load_model(lstm_path)
24
+
25
+ lstm_model = tf.keras.models.load_model(lstm_path)
26
+ scaler_ts = joblib.load(scaler_ts_path)
27
+
28
+ # --- Pemuatan Blending ---
29
+ blending_params_path = hf_hub_download(repo_id=REPO_ID, filename="blending_params.joblib")
30
+ blending_params = joblib.load(blending_params_path)
31
+
32
+ return lgbm_model, lstm_model, scaler_static, scaler_ts, blending_params
33
+
34
+ # Sekarang, panggil load_models() di luar fungsi prediksi utama Anda
35
+ LGBM_MODEL, LSTM_MODEL, SCALER_STATIC, SCALER_TS, BLENDING_PARAMS = load_models()
36
+
37
+ # Hapus kode pemuatan lama dari file lokal di predict.py Anda!
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ Flask
2
+ gunicorn
3
+ pandas
4
+ numpy
5
+ joblib
6
+ lightgbm
7
+ tensorflow==2.18.0 # Ganti ke versi yang ada di daftar 'found versions'
8
+ scikit-learn