Spaces:
Sleeping
Sleeping
Arnel Gwen Nuqui
commited on
Commit
·
803db15
1
Parent(s):
6b66a23
Add auto model downloader from Hugging Face Models
Browse files- .gitignore +0 -3
- routes/classification_routes.py +26 -3
.gitignore
CHANGED
|
@@ -3,6 +3,3 @@ model/
|
|
| 3 |
*.keras
|
| 4 |
*.h5
|
| 5 |
*.npy
|
| 6 |
-
!model/
|
| 7 |
-
!model/*.keras
|
| 8 |
-
!model/*.npy
|
|
|
|
| 3 |
*.keras
|
| 4 |
*.h5
|
| 5 |
*.npy
|
|
|
|
|
|
|
|
|
routes/classification_routes.py
CHANGED
|
@@ -17,11 +17,33 @@ preprocess_input = _mv2.preprocess_input
|
|
| 17 |
classification_bp = Blueprint('classification_bp', __name__)
|
| 18 |
|
| 19 |
# ------------------------------------------------------------
|
| 20 |
-
# Model setup
|
| 21 |
# ------------------------------------------------------------
|
| 22 |
-
BASE_DIR
|
| 23 |
MODEL_DIR = BASE_DIR / "model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
CANDIDATES = [
|
| 26 |
"cheating_mobilenetv2_final.keras",
|
| 27 |
"mnv2_clean_best.keras",
|
|
@@ -37,11 +59,12 @@ else:
|
|
| 37 |
model = None
|
| 38 |
print(f"⚠️ No model found in {MODEL_DIR}. Put one of: {CANDIDATES}")
|
| 39 |
|
|
|
|
| 40 |
thr_file = MODEL_DIR / "best_threshold.npy"
|
| 41 |
THRESHOLD = float(np.load(thr_file)[0]) if thr_file.exists() else 0.555
|
| 42 |
print(f"📊 Using decision threshold: {THRESHOLD:.3f}")
|
| 43 |
|
| 44 |
-
# Input shape
|
| 45 |
if model is not None:
|
| 46 |
H, W = model.input_shape[1:3]
|
| 47 |
else:
|
|
|
|
| 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",
|
|
|
|
| 59 |
model = None
|
| 60 |
print(f"⚠️ No model found in {MODEL_DIR}. Put one of: {CANDIDATES}")
|
| 61 |
|
| 62 |
+
# --- Load threshold ---
|
| 63 |
thr_file = MODEL_DIR / "best_threshold.npy"
|
| 64 |
THRESHOLD = float(np.load(thr_file)[0]) if thr_file.exists() else 0.555
|
| 65 |
print(f"📊 Using decision threshold: {THRESHOLD:.3f}")
|
| 66 |
|
| 67 |
+
# --- Input shape ---
|
| 68 |
if model is not None:
|
| 69 |
H, W = model.input_shape[1:3]
|
| 70 |
else:
|