Spaces:
Sleeping
Sleeping
Commit ·
ee1c180
1
Parent(s): 26a71ad
Fixed7
Browse files- app.py +12 -0
- utils/model_selector.py +21 -13
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import eventlet
|
| 2 |
eventlet.monkey_patch()
|
| 3 |
|
|
|
|
|
|
|
| 4 |
import logging
|
| 5 |
from flask import Flask, jsonify
|
| 6 |
from flask_cors import CORS
|
|
@@ -84,6 +86,16 @@ def register_blueprints(app):
|
|
| 84 |
|
| 85 |
register_blueprints(app)
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
@app.route("/")
|
| 88 |
def home():
|
| 89 |
# Detect environment for frontend awareness
|
|
|
|
| 1 |
import eventlet
|
| 2 |
eventlet.monkey_patch()
|
| 3 |
|
| 4 |
+
import utils.model_selector
|
| 5 |
+
|
| 6 |
import logging
|
| 7 |
from flask import Flask, jsonify
|
| 8 |
from flask_cors import CORS
|
|
|
|
| 86 |
|
| 87 |
register_blueprints(app)
|
| 88 |
|
| 89 |
+
with app.app_context():
|
| 90 |
+
try:
|
| 91 |
+
from utils.model_selector import load_model
|
| 92 |
+
print("📥 Pre-loading AI models...")
|
| 93 |
+
# Start with 'bcc' as it's the default
|
| 94 |
+
load_model("bcc")
|
| 95 |
+
print("✅ Default models ready.")
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"❌ Startup Model Error: {e}")
|
| 98 |
+
|
| 99 |
@app.route("/")
|
| 100 |
def home():
|
| 101 |
# Detect environment for frontend awareness
|
utils/model_selector.py
CHANGED
|
@@ -15,22 +15,30 @@ _ACTIVE_LOCK = threading.Lock()
|
|
| 15 |
_MODEL_CACHE = {}
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
-
# We reach into the sub-module to find the "hidden" function
|
| 20 |
import sklearn.utils._column_transformer as ct_utils
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
if not hasattr(sklearn.utils, 'parse_version'):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
# --------------------------------------------
|
| 35 |
|
| 36 |
# 1. FIXED PATH LOGIC:
|
|
|
|
| 15 |
_MODEL_CACHE = {}
|
| 16 |
|
| 17 |
|
| 18 |
+
# --- THE "BULLETPROOF" SKLEARN PATCH ---
|
| 19 |
+
# We must do this BEFORE any other ML imports
|
| 20 |
+
import sklearn.utils
|
| 21 |
try:
|
|
|
|
| 22 |
import sklearn.utils._column_transformer as ct_utils
|
| 23 |
+
sklearn.utils._get_column_indices = ct_utils._get_column_indices
|
| 24 |
+
print("[Patch] Successfully injected _get_column_indices")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
# If the above fails, we define a dummy function to stop the crash
|
| 27 |
+
def _get_column_indices(X, key):
|
| 28 |
+
from sklearn.utils._column_transformer import _get_column_indices as gci
|
| 29 |
+
return gci(X, key)
|
| 30 |
+
sklearn.utils._get_column_indices = _get_column_indices
|
| 31 |
+
print(f"[Patch] Manual injection fallback used: {e}")
|
| 32 |
+
|
| 33 |
+
# Patch for parse_version
|
| 34 |
if not hasattr(sklearn.utils, 'parse_version'):
|
| 35 |
+
try:
|
| 36 |
+
from packaging import version
|
| 37 |
+
sklearn.utils.parse_version = version.parse
|
| 38 |
+
print("[Patch] Successfully injected parse_version")
|
| 39 |
+
except ImportError:
|
| 40 |
+
print("[Patch] 'packaging' library missing. Ensure it is in requirements.txt")
|
| 41 |
+
# ---------------------------------------
|
| 42 |
# --------------------------------------------
|
| 43 |
|
| 44 |
# 1. FIXED PATH LOGIC:
|