Untitled
unknown
python
8 months ago
1.2 kB
7
Indexable
@app.route("/train", methods=["GET"])
def train_model():
"""Endpoint untuk melatih ulang model SARIMA."""
df = get_filtered_logs()
if df is None or df.empty:
return jsonify({"error": "Data tidak tersedia atau kosong."}), 500
train_size = int(len(df) * 0.8)
train = df["volume"][:train_size]
test = df["volume"][train_size:]
model = train_sarima_model(train)
if model is None:
return jsonify({"error": "Gagal melatih model SARIMA."}), 500
MODEL_CACHE["model"] = model
MODEL_CACHE["last_updated"] = datetime.now()
# Evaluasi model
predictions = model.predict(n_periods=len(test))
mse = mean_squared_error(test, predictions)
rmse = np.sqrt(mse)
try:
model_path = "/home/iqsansyachranie/sarima_model2.pkl"
with open(model_path, "wb") as f:
pickle.dump(model, f)
logger.info("✅ Model SARIMA berhasil disimpan!")
except Exception as e:
logger.warning(f"⚠️ Gagal menyimpan model: {str(e)}")
return jsonify({
"message": "Model berhasil dilatih!",
"aic": model.aic(),
"mse": mse,
"rmse": rmse
})Editor is loading...
Leave a Comment