From c0b59d684aa582e8de0686ccf6761bed8c561a25 Mon Sep 17 00:00:00 2001 From: livindra Date: Sun, 31 May 2026 13:00:24 +0700 Subject: [PATCH] fix --- app.py | 28 ++++++++++++++++++++++++++-- templates/index.html | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 1e1ee5b..588e53d 100644 --- a/app.py +++ b/app.py @@ -86,9 +86,10 @@ label_encoder = None extractor = FeatureExtractor() model_loading = False model_loaded = False +model_load_error = None def _load_model_background(): - global model, scaler, label_encoder, model_loading, model_loaded + global model, scaler, label_encoder, model_loading, model_loaded, model_load_error # Prevent double-loading if model_loading or model_loaded: @@ -101,12 +102,14 @@ def _load_model_background(): m, s, le = load_model() model, scaler, label_encoder = m, s, le model_loaded = True + model_load_error = None print('[APP] Model loaded successfully.') except Exception as e: model = None scaler = None label_encoder = None - model_loaded = True # Mark as done to prevent retry loop + model_loaded = False + model_load_error = str(e) print(f"[APP] ❌ Background model load failed: {e}") finally: model_loading = False @@ -120,6 +123,27 @@ def is_model_ready(): return model is not None and scaler is not None and label_encoder is not None +@app.route('/api/model-status') +def api_model_status(): + """Return the current model loading status for the homepage.""" + ready = is_model_ready() + if ready: + status = 'ready' + elif model_loading: + status = 'loading' + elif model_load_error: + status = 'error' + else: + status = 'loading' + + return jsonify({ + 'status': status, + 'ready': ready, + 'loading': model_loading, + 'error': model_load_error, + }) + + def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS diff --git a/templates/index.html b/templates/index.html index 2942e98..afb4099 100644 --- a/templates/index.html +++ b/templates/index.html @@ -16,15 +16,13 @@

- {% if model_loaded %} -
+
+ {% if model_loaded %} Model siap digunakan -
- {% else %} -
+ {% else %} Model sedang dimuat, silakan tunggu... + {% endif %}
- {% endif %}
@@ -100,4 +98,38 @@ border-radius: 50%; } + {% endblock %} \ No newline at end of file