-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprediction_endpoint.py
More file actions
40 lines (32 loc) · 1.27 KB
/
prediction_endpoint.py
File metadata and controls
40 lines (32 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
from pydantic import BaseModel
from typing import List, Optional, Tuple # Added Tuple import for type hinting
from sklearn.pipeline import Pipeline
import os
import joblib
class PredictionInput(BaseModel):
data: List[float] # Define input data type
class PredictionOutput(BaseModel):
category: int # Define output category
class FraudDetector:
model: Optional[Pipeline]
targets: Optional[List[int]]
def load_model(self):
"""Loads the model"""
model_file = os.path.join(os.path.dirname(
__file__), "ml/fraud_model.joblib")
# Adjust the type hint to include Tuple
loaded_model: Tuple[Pipeline, List[int]] = joblib.load(model_file)
model, targets = loaded_model
self.model = model
self.targets = targets
def predict(self, input_data: PredictionInput) -> PredictionOutput:
"""Runs a prediction"""
# Reshape input data into 2D array
input_data_np = np.array(input_data.data).reshape(1, -1)
# Perform prediction
prediction = self.model.predict(input_data_np)
# Get category from targets based on prediction
category = self.targets[prediction[0]]
# Return prediction output
return PredictionOutput(category=category)