Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,107 @@
|
|
1 |
-
from
|
2 |
-
import
|
3 |
-
from
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
app
|
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 |
-
errors
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
"
|
67 |
-
"
|
68 |
-
"
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
#
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|