bram4627 commited on
Commit
0bb50bf
1 Parent(s): f8fa836

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -101
app.py CHANGED
@@ -1,101 +1,107 @@
1
- from flask import Flask, request, render_template
2
- import joblib
3
- from tensorflow.keras.models import load_model
4
- import numpy as np
5
- import os
6
-
7
- # Load models
8
- model_load = load_model('lstm_model.h5')
9
- scaler_load = joblib.load('scaler.sav') # Load the correct scaler
10
-
11
- # Flask app initialization
12
- app = Flask(__name__)
13
- app.config["SECRET_KEY"] = os.urandom(24)
14
- app.config["DEBUG"] = True
15
-
16
- def validasi_inputan(form_data):
17
- errors = {}
18
-
19
- # Validasi temp_1, temp_2, temp_3
20
- for field in ["temp_1", "temp_2", "temp_3"]:
21
- if not form_data.get(field):
22
- errors[field] = f"{field} tidak boleh kosong."
23
- else:
24
- try:
25
- float(form_data.get(field))
26
- except ValueError:
27
- errors[field] = f"{field} harus berupa angka."
28
-
29
- # Validasi feelslike_1, feelslike_2, feelslike_3
30
- for field in ["feelslike_1", "feelslike_2", "feelslike_3"]:
31
- if not form_data.get(field):
32
- errors[field] = f"{field} tidak boleh kosong."
33
- else:
34
- try:
35
- float(form_data.get(field))
36
- except ValueError:
37
- errors[field] = f"{field} harus berupa angka."
38
-
39
- return errors
40
-
41
- def validate_data(record):
42
- errors = {}
43
-
44
- # Validasi rentang nilai (15 - 50 untuk suhu)
45
- for key, value in record.items():
46
- if value < 15 or value > 50:
47
- errors[key] = f"{key} harus diantara 15 dan 50."
48
-
49
- return errors
50
-
51
- @app.route("/", methods=["GET", "POST"])
52
- def index():
53
- prediction = None
54
- errors = {}
55
- predictions_list = []
56
-
57
- if request.method == "POST":
58
- # Validasi input kosong
59
- errors = validasi_inputan(request.form)
60
-
61
- if not errors:
62
- record = {
63
- "temp_1": float(request.form.get("temp_1")),
64
- "temp_2": float(request.form.get("temp_2")),
65
- "temp_3": float(request.form.get("temp_3")),
66
- "feelslike_1": float(request.form.get("feelslike_1")),
67
- "feelslike_2": float(request.form.get("feelslike_2")),
68
- "feelslike_3": float(request.form.get("feelslike_3")),
69
- }
70
-
71
- # Validasi rentang nilai
72
- errors = validate_data(record)
73
-
74
- if not errors:
75
- # Input data untuk prediksi
76
- input_data = np.array([
77
- [record["temp_1"], record["feelslike_1"]],
78
- [record["temp_2"], record["feelslike_2"]],
79
- [record["temp_3"], record["feelslike_3"]]
80
- ])
81
-
82
- # Scaling input data
83
- input_data_scaled = scaler_load.transform(input_data)
84
-
85
- # Prediksi untuk 5 periode ke depan
86
- last_input_scaled = input_data_scaled.copy()
87
-
88
- for _ in range(5): # Prediksi 5 periode
89
- prediction_normalized = model_load.predict(last_input_scaled.reshape(1, 3, 2))
90
- prediction_denormalized = scaler_load.inverse_transform(prediction_normalized)
91
- predictions_list.append(prediction_denormalized.flatten())
92
-
93
- # Update input dengan prediksi terbaru
94
- last_input_scaled = np.append(last_input_scaled[1:], prediction_normalized, axis=0)
95
-
96
-
97
-
98
- return render_template('index.html', prediction=predictions_list, errors=errors, record=request.form)
99
-
100
- if __name__ == "__main__":
101
- app.run()
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from asgiref.wsgi import WsgiToAsgi
3
+ from flask import Flask, request, render_template
4
+ import joblib
5
+ from tensorflow.keras.models import load_model
6
+ import numpy as np
7
+ import os
8
+ from fastapi.middleware.wsgi import WSGIMiddleware
9
+
10
+ # Load models
11
+ model_load = load_model('lstm_model.h5')
12
+ scaler_load = joblib.load('scaler.sav') # Load the correct scaler
13
+
14
+ # Flask app initialization
15
+ app = Flask(__name__)
16
+ app.config["SECRET_KEY"] = os.urandom(24)
17
+ app.config["DEBUG"] = True
18
+
19
+ def validasi_inputan(form_data):
20
+ errors = {}
21
+
22
+ # Validasi temp_1, temp_2, temp_3
23
+ for field in ["temp_1", "temp_2", "temp_3"]:
24
+ if not form_data.get(field):
25
+ errors[field] = f"{field} tidak boleh kosong."
26
+ else:
27
+ try:
28
+ float(form_data.get(field))
29
+ except ValueError:
30
+ errors[field] = f"{field} harus berupa angka."
31
+
32
+ # Validasi feelslike_1, feelslike_2, feelslike_3
33
+ for field in ["feelslike_1", "feelslike_2", "feelslike_3"]:
34
+ if not form_data.get(field):
35
+ errors[field] = f"{field} tidak boleh kosong."
36
+ else:
37
+ try:
38
+ float(form_data.get(field))
39
+ except ValueError:
40
+ errors[field] = f"{field} harus berupa angka."
41
+
42
+ return errors
43
+
44
+ def validate_data(record):
45
+ errors = {}
46
+
47
+ # Validasi rentang nilai (15 - 50 untuk suhu)
48
+ for key, value in record.items():
49
+ if value < 15 or value > 50:
50
+ errors[key] = f"{key} harus diantara 15 dan 50."
51
+
52
+ return errors
53
+
54
+ @app.route("/", methods=["GET", "POST"])
55
+ def index():
56
+ prediction = None
57
+ errors = {}
58
+ predictions_list = []
59
+
60
+ if request.method == "POST":
61
+ # Validasi input kosong
62
+ errors = validasi_inputan(request.form)
63
+
64
+ if not errors:
65
+ record = {
66
+ "temp_1": float(request.form.get("temp_1")),
67
+ "temp_2": float(request.form.get("temp_2")),
68
+ "temp_3": float(request.form.get("temp_3")),
69
+ "feelslike_1": float(request.form.get("feelslike_1")),
70
+ "feelslike_2": float(request.form.get("feelslike_2")),
71
+ "feelslike_3": float(request.form.get("feelslike_3")),
72
+ }
73
+
74
+ # Validasi rentang nilai
75
+ errors = validate_data(record)
76
+
77
+ if not errors:
78
+ # Input data untuk prediksi
79
+ input_data = np.array([
80
+ [record["temp_1"], record["feelslike_1"]],
81
+ [record["temp_2"], record["feelslike_2"]],
82
+ [record["temp_3"], record["feelslike_3"]]
83
+ ])
84
+
85
+ # Scaling input data
86
+ input_data_scaled = scaler_load.transform(input_data)
87
+
88
+ # Prediksi untuk 5 periode ke depan
89
+ last_input_scaled = input_data_scaled.copy()
90
+
91
+ for _ in range(5): # Prediksi 5 periode
92
+ prediction_normalized = model_load.predict(last_input_scaled.reshape(1, 3, 2))
93
+ prediction_denormalized = scaler_load.inverse_transform(prediction_normalized)
94
+ predictions_list.append(prediction_denormalized.flatten())
95
+
96
+ # Update input dengan prediksi terbaru
97
+ last_input_scaled = np.append(last_input_scaled[1:], prediction_normalized, axis=0)
98
+
99
+
100
+
101
+ return render_template('index.html', prediction=predictions_list, errors=errors, record=request.form)
102
+
103
+ # FastAPI app to mount Flask app
104
+ app = FastAPI()
105
+
106
+ # Mount Flask app inside FastAPI using WSGIMiddleware
107
+ app.mount("/", WSGIMiddleware(app))