Arnel Gwen Nuqui commited on
Commit
4a63a35
·
1 Parent(s): 9d5b23e

done fixing path

Browse files
Files changed (1) hide show
  1. routes/classification_routes.py +17 -23
routes/classification_routes.py CHANGED
@@ -6,44 +6,37 @@ import numpy as np
6
  from PIL import Image
7
 
8
  try:
9
- # TF-bundled Keras (common with TensorFlow installs)
10
- from tensorflow.keras.applications import mobilenet_v2 as _mv2 # type: ignore
11
  except Exception:
12
- # Standalone Keras 3
13
- from keras.applications import mobilenet_v2 as _mv2 # type: ignore
14
 
15
- # --- Model preprocessing ---
16
  preprocess_input = _mv2.preprocess_input
17
  classification_bp = Blueprint('classification_bp', __name__)
18
 
19
  # ------------------------------------------------------------
20
  # Model setup and auto-download
21
  # ------------------------------------------------------------
22
- BASE_DIR = Path(__file__).resolve().parent.parent # -> /app/
23
- MODEL_DIR = BASE_DIR / "model"
24
  os.makedirs(MODEL_DIR, exist_ok=True)
25
 
26
- # --- Hugging Face model URLs ---
27
  MODEL_URLS = {
28
  "model": "https://huggingface.co/Gwen01/ProctorVision-Models/resolve/main/cheating_mobilenetv2_final.keras",
29
  "threshold": "https://huggingface.co/Gwen01/ProctorVision-Models/resolve/main/best_threshold.npy"
30
  }
31
 
32
- # --- Download missing model files ---
33
- for name, url in MODEL_URLS.items():
34
- dest = MODEL_DIR / os.path.basename(url)
35
- if not dest.exists():
36
- print(f"📥 Downloading {name} from {url} ...")
37
- try:
38
- r = requests.get(url)
39
- r.raise_for_status()
40
- with open(dest, "wb") as f:
41
- f.write(r.content)
42
- print(f"✅ Saved to {dest}")
43
- except Exception as e:
44
- print(f"⚠️ Failed to download {name}: {e}")
45
-
46
- # --- Load model ---
47
  CANDIDATES = [
48
  "cheating_mobilenetv2_final.keras",
49
  "mnv2_clean_best.keras",
@@ -51,6 +44,7 @@ CANDIDATES = [
51
  "mnv2_finetune_best.keras",
52
  ]
53
 
 
54
  model_path = next((MODEL_DIR / f for f in CANDIDATES if (MODEL_DIR / f).exists()), None)
55
  if model_path and model_path.exists():
56
  model = tf.keras.models.load_model(model_path, compile=False)
 
6
  from PIL import Image
7
 
8
  try:
9
+ from tensorflow.keras.applications import mobilenet_v2 as _mv2
 
10
  except Exception:
11
+ from keras.applications import mobilenet_v2 as _mv2
 
12
 
 
13
  preprocess_input = _mv2.preprocess_input
14
  classification_bp = Blueprint('classification_bp', __name__)
15
 
16
  # ------------------------------------------------------------
17
  # Model setup and auto-download
18
  # ------------------------------------------------------------
19
+ MODEL_DIR = Path(os.getenv("MODEL_DIR", "/tmp/model"))
 
20
  os.makedirs(MODEL_DIR, exist_ok=True)
21
 
 
22
  MODEL_URLS = {
23
  "model": "https://huggingface.co/Gwen01/ProctorVision-Models/resolve/main/cheating_mobilenetv2_final.keras",
24
  "threshold": "https://huggingface.co/Gwen01/ProctorVision-Models/resolve/main/best_threshold.npy"
25
  }
26
 
27
+ MODEL_PATHS = {}
28
+ for key, url in MODEL_URLS.items():
29
+ local_path = MODEL_DIR / Path(url).name
30
+ MODEL_PATHS[key] = str(local_path)
31
+ if not local_path.exists():
32
+ print(f"📥 Downloading {key} from Hugging Face…")
33
+ r = requests.get(url)
34
+ r.raise_for_status()
35
+ with open(local_path, "wb") as f:
36
+ f.write(r.content)
37
+ print(f"✅ Saved {key} {local_path}")
38
+
39
+ # Candidate filenames for compatibility
 
 
40
  CANDIDATES = [
41
  "cheating_mobilenetv2_final.keras",
42
  "mnv2_clean_best.keras",
 
44
  "mnv2_finetune_best.keras",
45
  ]
46
 
47
+
48
  model_path = next((MODEL_DIR / f for f in CANDIDATES if (MODEL_DIR / f).exists()), None)
49
  if model_path and model_path.exists():
50
  model = tf.keras.models.load_model(model_path, compile=False)