Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import json
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
# Download model artifacts from the model repo
|
| 8 |
+
MODEL_REPO = "shahviransh/fraud-detection"
|
| 9 |
+
MODEL_FILE = "fraud_detection_model.pkl"
|
| 10 |
+
|
| 11 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 12 |
+
model = joblib.load(model_path)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def predict(input_json):
|
| 16 |
+
"""
|
| 17 |
+
input_json expected format:
|
| 18 |
+
{
|
| 19 |
+
"features": [f1, f2, f3, ...]
|
| 20 |
+
}
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
data = json.loads(input_json)
|
| 25 |
+
features = np.array(data["features"]).reshape(1, -1)
|
| 26 |
+
|
| 27 |
+
pred = model.predict(features)[0]
|
| 28 |
+
prob = model.predict_proba(features)[0].tolist()
|
| 29 |
+
|
| 30 |
+
return {
|
| 31 |
+
"prediction": int(pred),
|
| 32 |
+
"probabilities": prob
|
| 33 |
+
}
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return {"error": str(e)}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=predict,
|
| 40 |
+
inputs=gr.Textbox(label="JSON Input"),
|
| 41 |
+
outputs="json",
|
| 42 |
+
title="Fraud Detection API",
|
| 43 |
+
description="Submit JSON payload containing numerical features."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
iface.launch()
|