demoaccta commited on
Commit
a43ecac
·
verified ·
1 Parent(s): e8d79c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -1,18 +1,17 @@
1
  import gradio as gr
2
- import torch
3
  import numpy as np
4
  import json
5
  from huggingface_hub import hf_hub_download
6
 
7
  REPO = "gabrielnkl/model-fraud-detect"
8
- MODEL = "fraud_detect_model.pth"
9
 
10
- # download once + cache
11
  model_path = hf_hub_download(repo_id=REPO, filename=MODEL)
12
 
13
- # load model
14
- model = torch.load(model_path, map_location=torch.device("cpu"))
15
- model.eval()
16
 
17
  TYPE_MAP = {
18
  "PAYMENT": 0,
@@ -22,6 +21,7 @@ TYPE_MAP = {
22
  "CASH_IN": 4
23
  }
24
 
 
25
  def predict(json_input):
26
 
27
  try:
@@ -32,11 +32,10 @@ def predict(json_input):
32
  new = float(d["newbalanceOrig"])
33
  t = TYPE_MAP[d["type"]]
34
 
35
- x = np.array([[amount, old, new, t]], dtype=np.float32)
36
- tensor = torch.from_numpy(x)
37
 
38
- prob = float(model(tensor).item())
39
- pred = int(prob > 0.5)
40
 
41
  return {
42
  "prediction": pred,
@@ -46,11 +45,13 @@ def predict(json_input):
46
  except Exception as e:
47
  return {"error": str(e)}
48
 
 
49
  iface = gr.Interface(
50
  fn=predict,
51
  inputs=gr.Textbox(lines=8, label="JSON Input"),
52
  outputs="json",
53
- title="Fraud Detection API"
 
54
  )
55
 
56
  if __name__ == "__main__":
 
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
  REPO = "gabrielnkl/model-fraud-detect"
8
+ MODEL = "modelo_fraude.pkl"
9
 
10
+ # download model
11
  model_path = hf_hub_download(repo_id=REPO, filename=MODEL)
12
 
13
+ # load sklearn model
14
+ model = joblib.load(model_path)
 
15
 
16
  TYPE_MAP = {
17
  "PAYMENT": 0,
 
21
  "CASH_IN": 4
22
  }
23
 
24
+
25
  def predict(json_input):
26
 
27
  try:
 
32
  new = float(d["newbalanceOrig"])
33
  t = TYPE_MAP[d["type"]]
34
 
35
+ X = np.array([[amount, old, new, t]], dtype=float)
 
36
 
37
+ prob = float(model.predict_proba(X)[0][1])
38
+ pred = int(model.predict(X)[0])
39
 
40
  return {
41
  "prediction": pred,
 
45
  except Exception as e:
46
  return {"error": str(e)}
47
 
48
+
49
  iface = gr.Interface(
50
  fn=predict,
51
  inputs=gr.Textbox(lines=8, label="JSON Input"),
52
  outputs="json",
53
+ title="Fraud Detection API (Sklearn Model)",
54
+ description="Submit JSON payload for fraud scoring."
55
  )
56
 
57
  if __name__ == "__main__":