Haseeb949/fluenta-backend
0
1# batch_predict.py2import os, csv, joblib, numpy as np3from pathlib import Path4from feature_extraction import extract_features5 6MODELS_DIR = Path("C:/Users/X/Downloads/models") # adjust if needed7CLIPS_DIR = Path("C:/Users/X/Downloads/models/Dataset/clips/clips")8OUT_CSV = Path("C:/Users/X/Downloads/models/backend/results.csv")9 10model = joblib.load(MODELS_DIR / "stutter_model_final.pkl")11scaler = joblib.load(MODELS_DIR / "scaler_final.pkl")12fc = joblib.load(MODELS_DIR / "feature_count.pkl")13try:14 expected = int(fc)15except Exception:16 expected = int(np.asarray(fc).item())17 18files = sorted([p for p in CLIPS_DIR.glob("*") if p.suffix.lower() in [".wav",".flac",".mp3",".ogg"]])19with OUT_CSV.open("w", newline="", encoding="utf-8") as f:20 writer = csv.writer(f)21 writer.writerow(["filename","prediction","confidence"])22 for p in files:23 feats = extract_features(str(p))24 if feats is None:25 writer.writerow([str(p.name),"ERROR","-"])26 continue27 if len(feats) != expected:28 writer.writerow([str(p.name),"FEATURE_MISMATCH",f"{len(feats)}/{expected}"])29 continue30 X = scaler.transform([feats])31 pred = model.predict(X)[0]32 conf = None33 if hasattr(model, "predict_proba"):34 conf = float(model.predict_proba(X)[0][int(pred)])35 label = "Stutter" if int(pred)==1 else "Non-Stutter"36 writer.writerow([str(p.name), label, "" if conf is None else round(conf,4)])37print("Saved results to", OUT_CSV)38 