obx0x3 commited on
Commit
8ee07b0
·
verified ·
1 Parent(s): e39f9e1

Update predict_impulse.py

Browse files
Files changed (1) hide show
  1. predict_impulse.py +32 -9
predict_impulse.py CHANGED
@@ -1,9 +1,13 @@
1
  import joblib
2
  import pandas as pd
3
  from huggingface_hub import hf_hub_download
4
- import opik
5
 
6
- # Download model from HF Model repo
 
 
 
 
 
7
  MODEL_REPO = "obx0x3/sensei-model"
8
  MODEL_FILE = "impulse_model.pkl"
9
 
@@ -14,6 +18,15 @@ model_path = hf_hub_download(
14
 
15
  impulse_model = joblib.load(model_path)
16
 
 
 
 
 
 
 
 
 
 
17
  def predict_impulse(category, amount, payment_method, day):
18
  df = pd.DataFrame([{
19
  "category": category,
@@ -30,12 +43,22 @@ def predict_impulse(category, amount, payment_method, day):
30
  "confidence": round(float(prob), 3)
31
  }
32
 
33
- # ✅ Correct Opik usage (NO init)
34
- opik.log(
35
- name="Impulse Prediction",
36
- input=df.to_dict(orient="records")[0],
37
- output=result,
38
- model="sensei-impulse-model"
39
- )
 
 
 
 
 
 
 
 
 
 
40
 
41
  return result
 
1
  import joblib
2
  import pandas as pd
3
  from huggingface_hub import hf_hub_download
 
4
 
5
+ # Opik (correct usage)
6
+ from opik import Opik
7
+
8
+ # -----------------------------
9
+ # Load model from HF Model repo
10
+ # -----------------------------
11
  MODEL_REPO = "obx0x3/sensei-model"
12
  MODEL_FILE = "impulse_model.pkl"
13
 
 
18
 
19
  impulse_model = joblib.load(model_path)
20
 
21
+ # -----------------------------
22
+ # Initialize Opik client
23
+ # -----------------------------
24
+ try:
25
+ opik_client = Opik(project_name="budgetbuddy-hackathon")
26
+ except Exception:
27
+ opik_client = None
28
+
29
+
30
  def predict_impulse(category, amount, payment_method, day):
31
  df = pd.DataFrame([{
32
  "category": category,
 
43
  "confidence": round(float(prob), 3)
44
  }
45
 
46
+ # -----------------------------
47
+ # Opik logging (SAFE)
48
+ # -----------------------------
49
+ if opik_client:
50
+ try:
51
+ opik_client.log_event(
52
+ name="Impulse Prediction",
53
+ input=df.to_dict(orient="records")[0],
54
+ output=result,
55
+ model="sensei-impulse-model",
56
+ metadata={
57
+ "source": "hf-space",
58
+ "task": "impulse-detection"
59
+ }
60
+ )
61
+ except Exception:
62
+ pass # Never crash inference
63
 
64
  return result