Download app.py from Sahithi27/Payment_Transaction_Fraud_detection: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/Sahithi27/Payment_Transaction_Fraud_detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/Sahithi27/Payment_Transaction_Fraud_detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Sahithi27/Payment_Transaction_Fraud_detection/resolve/main/app.py
1 kB
| import os | |
| import json | |
| import joblib | |
| import numpy as np | |
| import onnxruntime as ort | |
| from fastapi import FastAPI | |
| MODEL_PATH = "models/fraud_model.onnx" | |
| SCALER_PATH = "models/scaler.joblib" | |
| META_PATH = "models/metadata.json" | |
| # Fail fast if files missing | |
| for p in [MODEL_PATH, SCALER_PATH, META_PATH]: | |
| if not os.path.exists(p): | |
| raise FileNotFoundError(f"Missing file: {p}") | |
| scaler = joblib.load(SCALER_PATH) | |
| with open(META_PATH) as f: | |
| meta = json.load(f) | |
| num_features = meta["num_features"] | |
| session = ort.InferenceSession( | |
| MODEL_PATH, | |
| providers=["CPUExecutionProvider"] | |
| ) | |
| app = FastAPI(title="Fraud Detection API") | |
| def health(): | |
| return {"status": "running"} | |
| def predict(payload: dict): | |
| x = np.array([[payload[f] for f in num_features]], dtype=np.float32) | |
| x = scaler.transform(x) | |
| input_name = session.get_inputs()[0].name | |
| prob = session.run(None, {input_name: x})[0][0][0] | |
| return {"fraud_probability": float(prob)} | |