Spaces:
Sleeping
Sleeping
GitHub Actions commited on
Commit ·
2773b4f
0
Parent(s):
Deploy to Hugging Face
Browse files- Dockerfile +11 -0
- README.md +11 -0
- app.py +60 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 7 |
+
pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY app.py .
|
| 10 |
+
|
| 11 |
+
CMD ["sh", "-c", "uvicorn app:app --host 0.0.0.0 --port ${PORT}"]
|
README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Fraud Detection FastAPI
|
| 3 |
+
emoji: 📡
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
short_description: API for making fraud detection prediction
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GCP CREDENTIAL for HF SPACE
|
| 2 |
+
import os
|
| 3 |
+
if "GCP_KEY" in os.environ:
|
| 4 |
+
with open("/tmp/gcp_key.json", "w") as f:
|
| 5 |
+
f.write(os.environ["GCP_KEY"])
|
| 6 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/tmp/gcp_key.json"
|
| 7 |
+
|
| 8 |
+
### LIBRARIES ###
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import mlflow.sklearn
|
| 11 |
+
import mlflow
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
from typing import Union
|
| 14 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 15 |
+
from fastapi.responses import JSONResponse
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
### APP ###
|
| 19 |
+
app = FastAPI(title="Automatic Fraud Detection")
|
| 20 |
+
MLFLOW_TRACKING_URI = os.getenv("MLFLOW_TRACKING_URI")
|
| 21 |
+
|
| 22 |
+
class PredictionFeatures(BaseModel):
|
| 23 |
+
category: str
|
| 24 |
+
gender: str
|
| 25 |
+
city: str
|
| 26 |
+
dob: str
|
| 27 |
+
city_pop: Union[int, float]
|
| 28 |
+
job: str
|
| 29 |
+
amt: Union[int, float]
|
| 30 |
+
last: str
|
| 31 |
+
first: str
|
| 32 |
+
merchant: str
|
| 33 |
+
zip: Union[int, float]
|
| 34 |
+
|
| 35 |
+
@app.post("/predict")
|
| 36 |
+
async def predict(predictionFeatures: PredictionFeatures):
|
| 37 |
+
try:
|
| 38 |
+
df = pd.DataFrame([predictionFeatures.dict()])
|
| 39 |
+
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
|
| 40 |
+
model_name = "Automatic_Fraud_Detector_XGB"
|
| 41 |
+
model_version = "latest"
|
| 42 |
+
model = mlflow.sklearn.load_model(f"models:/{model_name}/{model_version}")
|
| 43 |
+
prediction = model.predict(df)
|
| 44 |
+
return {"prediction": prediction.tolist()[0]}
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 47 |
+
|
| 48 |
+
@app.post("/batch-predict")
|
| 49 |
+
async def batch_predict(file: UploadFile = File(...)):
|
| 50 |
+
try:
|
| 51 |
+
df = pd.read_csv(file.file)
|
| 52 |
+
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
|
| 53 |
+
model_name = "Automatic_Fraud_Detector_XGB"
|
| 54 |
+
model_version = "latest"
|
| 55 |
+
model = mlflow.sklearn.load_model(f"models:/{model_name}/{model_version}")
|
| 56 |
+
predictions = model.predict(df)
|
| 57 |
+
return {"predictions": predictions.tolist()}
|
| 58 |
+
except Exception as e:
|
| 59 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 60 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi[standard]
|
| 2 |
+
mlflow==3.9.0
|
| 3 |
+
pandas==2.3.3
|
| 4 |
+
scikit-learn==1.8.0
|
| 5 |
+
xgboost==2.1.1
|
| 6 |
+
google-cloud-storage
|