Spaces:
Sleeping
Sleeping
Create predict.py
Browse files- predict.py +37 -0
predict.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import joblib
|
| 3 |
+
|
| 4 |
+
# Load model and encoders
|
| 5 |
+
model = joblib.load("model/model.pkl")
|
| 6 |
+
encoders = joblib.load("model/encoders.pkl")
|
| 7 |
+
|
| 8 |
+
def predict_transaction(data_dict):
|
| 9 |
+
# Convert dict to dataframe
|
| 10 |
+
df = pd.DataFrame([data_dict])
|
| 11 |
+
|
| 12 |
+
# Process time
|
| 13 |
+
df["hour"] = pd.to_datetime(df["time"], format="%H:%M").dt.hour
|
| 14 |
+
df.drop(columns=["check_id", "time"], inplace=True)
|
| 15 |
+
|
| 16 |
+
# Encode categorical features
|
| 17 |
+
for col in ["employee_id", "terminal_id"]:
|
| 18 |
+
df[col] = encoders[col].transform(df[col])
|
| 19 |
+
|
| 20 |
+
# Predict
|
| 21 |
+
prediction = model.predict(df)[0]
|
| 22 |
+
return "Suspicious" if prediction == 1 else "Not Suspicious"
|
| 23 |
+
|
| 24 |
+
# Example usage
|
| 25 |
+
if __name__ == "__main__":
|
| 26 |
+
sample = {
|
| 27 |
+
"check_id": 1005,
|
| 28 |
+
"employee_id": "E101",
|
| 29 |
+
"total": 100,
|
| 30 |
+
"discount_amount": 90,
|
| 31 |
+
"item_count": 1,
|
| 32 |
+
"time": "12:10",
|
| 33 |
+
"terminal_id": "T1"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
result = predict_transaction(sample)
|
| 37 |
+
print("Prediction:", result)
|