scam_dectect_sheid / scripts /04_inference.py
adsutraemp1
init
ae77244
Raw History Blame Contribute Delete
4.66 kB
"""
Run predictions with your fine-tuned scam classifier.
Usage:
python predict_scam.py --model_dir ./scam-classifier-model --text "You won a free prize!"
"""
import argparse
import torch
import os
import subprocess
from pathlib import Path
import mlflow
from dotenv import load_dotenv
from transformers import AutoTokenizer, AutoModelForSequenceClassification
REPO_ROOT = Path(__file__).resolve().parents[1]
BRANCH_STAGE_MAP = {
"feature/phase-2-audio-asr": "feature/phase-2-audio-asr",
"feature/phase-3-serving-quantization": "feature/phase-3-serving-quantization",
"feature/phase-1.5-ultimate-dataset": "model-modernbert-universal",
"model-long-context": "model-modernbert-universal",
"model-distilbert": "model-distilbert",
"main": "main",
}
def detect_branch(default="main"):
env_branch = os.getenv("DAGSHUB_BRANCH") or os.getenv("GIT_BRANCH")
if env_branch:
return env_branch.strip()
result = subprocess.run(
["git", "branch", "--show-current"],
cwd=REPO_ROOT,
capture_output=True,
text=True,
check=False,
)
branch = result.stdout.strip()
return branch or default
def stage_for_branch(branch):
return BRANCH_STAGE_MAP.get(branch, branch.replace("/", "-") or "main")
def get_device():
if torch.backends.mps.is_available():
return "mps"
if torch.cuda.is_available():
return "cuda"
return "cpu"
def main():
load_dotenv()
parser = argparse.ArgumentParser()
parser.add_argument("--model_dir", type=str, default="mlflow", help="Path to local model, Hugging Face repo ID, or 'mlflow' to pull latest from DagsHub")
parser.add_argument("--text", type=str, required=True)
parser.add_argument("--registry_name", type=str, default=None, help="Name of the model in DagsHub MLflow Registry when using --model_dir=mlflow")
args = parser.parse_args()
branch = detect_branch()
stage = stage_for_branch(branch)
stage_slug = stage.replace("/", "-")
registry_name = args.registry_name or f"ModernBERT-Scam-Classifier-{stage_slug}"
# DagsHub Auth
username = os.environ.get("MLFLOW_TRACKING_USERNAME")
password = os.environ.get("MLFLOW_TRACKING_PASSWORD")
repo_owner = os.environ.get("DAGSHUB_REPO_OWNER")
repo_name = os.environ.get("DAGSHUB_REPO_NAME")
if username and password:
os.environ["DAGSHUB_USER"] = username
os.environ["DAGSHUB_TOKEN"] = password
import dagshub
dagshub.auth.add_app_token(password)
if repo_owner and repo_name:
import dagshub
dagshub.init(repo_name=repo_name, repo_owner=repo_owner, mlflow=True)
# CRITICAL FIX: Force MLflow's internal boto3 client to route to DagsHub S3 instead of AWS
os.environ["MLFLOW_S3_ENDPOINT_URL"] = f"https://dagshub.com/{repo_owner}/{repo_name}.s3"
os.environ["AWS_ACCESS_KEY_ID"] = username
os.environ["AWS_SECRET_ACCESS_KEY"] = password
device = get_device()
if args.model_dir == "mlflow":
print("Fetching latest model from DagsHub MLflow registry...")
mlflow.set_experiment(f"scam-detection/{stage}/inference")
# Load directly from the Model Registry instead of searching for runs
model_uri = f"models:/{registry_name}/latest"
print(f"Loading model from registry: {model_uri}")
try:
pipeline = mlflow.transformers.load_model(model_uri, return_type="components")
model = pipeline["model"]
tokenizer = pipeline["tokenizer"]
except Exception as e:
print(f"ERROR: Failed to load model from registry ({e}). Please ensure you have trained and registered it.")
return
else:
tokenizer = AutoTokenizer.from_pretrained(args.model_dir)
model = AutoModelForSequenceClassification.from_pretrained(args.model_dir)
model.to(device)
model.eval()
inputs = tokenizer(args.text, return_tensors="pt", truncation=True, max_length=8192).to(device)
# Fix for some models crashing when token_type_ids are generated by the standard tokenizer
if "token_type_ids" in inputs:
del inputs["token_type_ids"]
with torch.no_grad():
logits = model(**inputs).logits
probs = torch.softmax(logits, dim=-1)[0]
label = "SCAM" if probs[1] > probs[0] else "LEGIT"
confidence = probs.max().item()
print(f"Text: {args.text}")
print(f"Prediction: {label} (confidence: {confidence:.2%})")
print(f" Legit prob: {probs[0]:.2%} | Scam prob: {probs[1]:.2%}")
if __name__ == "__main__":
main()