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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -7,20 +7,10 @@ from huggingface_hub import hf_hub_download
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,
18
- "TRANSFER": 1,
19
- "CASH_OUT": 2,
20
- "DEBIT": 3,
21
- "CASH_IN": 4
22
- }
23
-
24
 
25
  def predict(json_input):
26
 
@@ -30,9 +20,26 @@ def predict(json_input):
30
  amount = float(d["amount"])
31
  old = float(d["oldbalanceOrg"])
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])
@@ -55,4 +62,4 @@ iface = gr.Interface(
55
  )
56
 
57
  if __name__ == "__main__":
58
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
7
  REPO = "gabrielnkl/model-fraud-detect"
8
  MODEL = "modelo_fraude.pkl"
9
 
 
10
  model_path = hf_hub_download(repo_id=REPO, filename=MODEL)
 
 
11
  model = joblib.load(model_path)
12
 
13
+ TYPES = ["TRANSFER","CASH_OUT","PAYMENT","DEBIT","CASH_IN"]
 
 
 
 
 
 
 
14
 
15
  def predict(json_input):
16
 
 
20
  amount = float(d["amount"])
21
  old = float(d["oldbalanceOrg"])
22
  new = float(d["newbalanceOrig"])
23
+ t = d["type"]
24
+
25
+ # engineered features
26
+ errorBalanceOrig = old - new - amount
27
+ balanceDifference = old - new
28
+
29
+ # base numerical features
30
+ features = [
31
+ amount,
32
+ old,
33
+ new,
34
+ errorBalanceOrig,
35
+ balanceDifference
36
+ ]
37
+
38
+ # one-hot type encoding (fixed order)
39
+ for typ in TYPES:
40
+ features.append(1.0 if t == typ else 0.0)
41
 
42
+ X = np.array([features], dtype=float)
43
 
44
  prob = float(model.predict_proba(X)[0][1])
45
  pred = int(model.predict(X)[0])
 
62
  )
63
 
64
  if __name__ == "__main__":
65
+ iface.launch(server_name="0.0.0.0", server_port=7860)