Sahithi27 commited on
Commit
1056ebc
·
verified ·
1 Parent(s): a659408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -60
app.py CHANGED
@@ -1,48 +1,3 @@
1
- import os
2
- import onnx
3
- import joblib
4
-
5
- ONNX_PATH = "collusion_xgb_model.onnx"
6
- FEATURES_PATH = "feature_order.joblib"
7
-
8
-
9
- def main():
10
-
11
- print("🔍 Hugging Face inference environment check")
12
-
13
- # -----------------------------
14
- # 1. Check ONNX model
15
- # -----------------------------
16
- if not os.path.exists(ONNX_PATH):
17
- raise FileNotFoundError(
18
- "ONNX model not found. Expected collusion_xgb_model.onnx"
19
- )
20
-
21
- print("ONNX model found")
22
-
23
- # -----------------------------
24
- # 2. Load & verify ONNX
25
- # -----------------------------
26
- model = onnx.load(ONNX_PATH)
27
- onnx.checker.check_model(model)
28
-
29
- print("ONNX model is valid")
30
-
31
- # -----------------------------
32
- # 3. Feature order check (optional)
33
- # -----------------------------
34
- if os.path.exists(FEATURES_PATH):
35
- features = joblib.load(FEATURES_PATH)
36
- print("Feature order loaded:", features)
37
- else:
38
- print("feature_order.joblib not found (ok for inference-only)")
39
-
40
- print("\n🚀 Environment ready for inference")
41
- print("➡️ Use app.py to serve predictions")
42
-
43
-
44
- if __name__ == "__main__":
45
- main()
46
  import onnxruntime as ort
47
  import gradio as gr
48
  import numpy as np
@@ -50,27 +5,27 @@ import numpy as np
50
  sess = ort.InferenceSession("collusion_xgb_model.onnx")
51
 
52
  def predict(amount, user_txn, driver_txn, pair_count, hour, day):
53
- X = np.array(
54
- [[amount, user_txn, driver_txn, pair_count, hour, day]],
55
- dtype=np.float32
56
- )
57
-
58
  outputs = sess.run(None, {sess.get_inputs()[0].name: X})
59
-
60
  scores = outputs[0]
61
 
62
- # Case 1: class probabilities [prob_0, prob_1]
63
  if scores.ndim == 2 and scores.shape[1] == 2:
64
  fraud_prob = scores[0][1]
65
-
66
- # Case 2: single probability
67
- elif scores.ndim == 2 and scores.shape[1] == 1:
68
- fraud_prob = scores[0][0]
69
-
70
- # Case 3: raw score → sigmoid
71
  else:
72
- raw_score = scores[0]
73
- fraud_prob = 1 / (1 + np.exp(-raw_score))
74
 
75
  return float(fraud_prob)
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import onnxruntime as ort
2
  import gradio as gr
3
  import numpy as np
 
5
  sess = ort.InferenceSession("collusion_xgb_model.onnx")
6
 
7
  def predict(amount, user_txn, driver_txn, pair_count, hour, day):
8
+ X = np.array([[amount, user_txn, driver_txn, pair_count, hour, day]], dtype=np.float32)
 
 
 
 
9
  outputs = sess.run(None, {sess.get_inputs()[0].name: X})
 
10
  scores = outputs[0]
11
 
 
12
  if scores.ndim == 2 and scores.shape[1] == 2:
13
  fraud_prob = scores[0][1]
 
 
 
 
 
 
14
  else:
15
+ fraud_prob = scores[0][0]
 
16
 
17
  return float(fraud_prob)
18
 
19
+ gr.Interface(
20
+ fn=predict,
21
+ inputs=[
22
+ gr.Number(label="Amount"),
23
+ gr.Number(label="User Txn Count"),
24
+ gr.Number(label="Driver Txn Count"),
25
+ gr.Number(label="User-Driver Pair Count"),
26
+ gr.Number(label="Hour"),
27
+ gr.Number(label="Day of Week"),
28
+ ],
29
+ outputs=gr.Number(label="Fraud Probability"),
30
+ title="Collusion Fraud Detection (ONNX)"
31
+ ).launch()