bram4627's picture
Update app.py
9f51c76 verified
from fastapi import FastAPI
from asgiref.wsgi import WsgiToAsgi
from flask import Flask, request, render_template
import pickle, os
from fastapi.middleware.wsgi import WSGIMiddleware
# Initialize Flask app
flask_app = Flask(__name__)
def validasi_inputan(form_data):
errors = {}
# Validasi untuk field Age, TB, Alkphos, Sgpt, A/G Ratio
if not form_data.get("Age"):
errors["Age"] = "Age tidak boleh kosong."
else:
try:
Age = int(form_data.get("Age"))
except ValueError:
errors["Age"] = "Age harus berupa angka."
if form_data.get("Gender") not in ["0", "1"]:
errors["Gender"] = "Gender harus 0 (Female) atau 1 (Male)."
if not form_data.get("TB"):
errors["TB"] = "TB tidak boleh kosong."
else:
try:
TB = float(form_data.get("TB"))
except ValueError:
errors["TB"] = "TB harus berupa angka."
if not form_data.get("Alkphos"):
errors["Alkphos"] = "Alkphos tidak boleh kosong."
else:
try:
Alkphos = float(form_data.get("Alkphos"))
except ValueError:
errors["Alkphos"] = "Alkphos harus berupa angka."
if not form_data.get("Sgpt"):
errors["Sgpt"] = "Sgpt tidak boleh kosong."
else:
try:
Sgpt = float(form_data.get("Sgpt"))
except ValueError:
errors["Sgpt"] = "Sgpt harus berupa angka."
if not form_data.get("AG_Ratio"):
errors["AG_Ratio"] = "A/G Ratio tidak boleh kosong."
else:
try:
AG_Ratio = float(form_data.get("AG_Ratio"))
except ValueError:
errors["AG_Ratio"] = "A/G Ratio harus berupa angka."
return errors
def validate_data(record):
errors = {}
# Menambahkan batasan nilai untuk beberapa input
if record["Age"] < 0 or record["Age"] > 150:
errors["Age"] = "Age harus antara 0 dan 150."
if record["TB"] < 0 or record["TB"] > 100:
errors["TB"] = "TB harus antara 0 dan 100."
if record["Alkphos"] < 0 or record["Alkphos"] > 1000:
errors["Alkphos"] = "Alkphos harus antara 0 dan 1000."
if record["Sgpt"] < 0 or record["Sgpt"] > 1000:
errors["Sgpt"] = "Sgpt harus antara 0 dan 1000."
if record["AG_Ratio"] < 0 or record["AG_Ratio"] > 2:
errors["AG_Ratio"] = "A/G Ratio harus antara 0 dan 2."
return errors
# Load models
linear_model_load = pickle.load(open('model.sav', 'rb'))
scaler_load = pickle.load(open('scaler.sav', 'rb'))
# Flask route
@flask_app.route("/", methods=["GET", "POST"])
def index():
prediction = None
errors = {}
if request.method == "POST":
# Validasi inputan tidak boleh kosong
errors = validasi_inputan(request.form)
if not errors:
record = {
"Age": int(request.form.get("Age")),
"Gender": int(request.form.get("Gender")), # Gender (0 = Female, 1 = Male)
"TB": float(request.form.get("TB")),
"Alkphos": float(request.form.get("Alkphos")),
"Sgpt": float(request.form.get("Sgpt")),
"AG_Ratio": float(request.form.get("AG_Ratio"))
}
errors = validate_data(record)
if not errors:
# Data input untuk prediksi
input_data = [
record["Age"],
record["Gender"],
record["TB"],
record["Alkphos"],
record["Sgpt"],
record["AG_Ratio"]
]
# Normalisasi input data
input_data_normalized = scaler_load.transform([input_data])
# Membuat prediksi dari model
predicted_value_normalized = linear_model_load.predict(input_data_normalized)
# Menyesuaikan bentuk data untuk inverse_transform
# Mengambil elemen prediksi pertama sebagai hasil akhir
prediction = int(predicted_value_normalized)
return render_template('index.html', prediction=prediction, errors=errors, record=request.form)
# FastAPI app to mount Flask app
app = FastAPI()
# Mount Flask app inside FastAPI using WSGIMiddleware
app.mount("/", WSGIMiddleware(flask_app))