Spaces:
Sleeping
Sleeping
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 | |
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 | |