File size: 3,823 Bytes
b00615b
 
 
 
 
0bb50bf
 
 
 
 
 
 
 
 
 
 
 
 
 
e39e9ae
 
 
0bb50bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e39e9ae
0bb50bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b00615b
0bb50bf
 
b00615b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
102
103
104
105
106
107
108
109
110
111
import os

# Set CUDA_VISIBLE_DEVICES to empty string to disable GPU usage
os.environ["CUDA_VISIBLE_DEVICES"] = ""

from fastapi import FastAPI
from asgiref.wsgi import WsgiToAsgi
from flask import Flask, request, render_template
import joblib
from tensorflow.keras.models import load_model
import numpy as np
import os
from fastapi.middleware.wsgi import WSGIMiddleware

# Load models
model_load = load_model('lstm_model.h5')
scaler_load = joblib.load('scaler.sav')  # Load the correct scaler

# Flask app initialization
flask_app = Flask(__name__)
flask_app.config["SECRET_KEY"] = os.urandom(24)
flask_app.config["DEBUG"] = True

def validasi_inputan(form_data):
    errors = {}

    # Validasi temp_1, temp_2, temp_3
    for field in ["temp_1", "temp_2", "temp_3"]:
        if not form_data.get(field):
            errors[field] = f"{field} tidak boleh kosong."
        else:
            try:
                float(form_data.get(field))
            except ValueError:
                errors[field] = f"{field} harus berupa angka."

    # Validasi feelslike_1, feelslike_2, feelslike_3
    for field in ["feelslike_1", "feelslike_2", "feelslike_3"]:
        if not form_data.get(field):
            errors[field] = f"{field} tidak boleh kosong."
        else:
            try:
                float(form_data.get(field))
            except ValueError:
                errors[field] = f"{field} harus berupa angka."

    return errors

def validate_data(record):
    errors = {}

    # Validasi rentang nilai (15 - 50 untuk suhu)
    for key, value in record.items():
        if value < 15 or value > 50:
            errors[key] = f"{key} harus diantara 15 dan 50."

    return errors

@flask_app.route("/", methods=["GET", "POST"])
def index():
    prediction = None
    errors = {}
    predictions_list = []

    if request.method == "POST":
        # Validasi input kosong
        errors = validasi_inputan(request.form)

        if not errors:
            record = {
                "temp_1": float(request.form.get("temp_1")),
                "temp_2": float(request.form.get("temp_2")),
                "temp_3": float(request.form.get("temp_3")),
                "feelslike_1": float(request.form.get("feelslike_1")),
                "feelslike_2": float(request.form.get("feelslike_2")),
                "feelslike_3": float(request.form.get("feelslike_3")),
            }

            # Validasi rentang nilai
            errors = validate_data(record)

            if not errors:
                # Input data untuk prediksi
                input_data = np.array([
                    [record["temp_1"], record["feelslike_1"]],
                    [record["temp_2"], record["feelslike_2"]],
                    [record["temp_3"], record["feelslike_3"]]
                ])

                # Scaling input data
                input_data_scaled = scaler_load.transform(input_data)

                # Prediksi untuk 5 periode ke depan
                last_input_scaled = input_data_scaled.copy()

                for _ in range(5):  # Prediksi 5 periode
                    prediction_normalized = model_load.predict(last_input_scaled.reshape(1, 3, 2))
                    prediction_denormalized = scaler_load.inverse_transform(prediction_normalized)
                    predictions_list.append(prediction_denormalized.flatten())

                    # Update input dengan prediksi terbaru
                    last_input_scaled = np.append(last_input_scaled[1:], prediction_normalized, axis=0)

    return render_template('index.html', prediction=predictions_list, errors=errors, record=request.form)

# FastAPI app to mount Flask app
app = FastAPI()  # Change this line from `fastapi_app` to `app`

# Mount Flask app inside FastAPI using WSGIMiddleware
app.mount("/", WSGIMiddleware(flask_app))  # Keep this line