-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscore.py
More file actions
55 lines (47 loc) · 1.7 KB
/
score.py
File metadata and controls
55 lines (47 loc) · 1.7 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
import joblib
import numpy as np
from azureml.core.model import Model
# Configure logging at INFO level
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def init():
"""
Initialize the global model variable.
This function is called once when the service starts.
"""
global model
try:
model_path = Model.get_model_path("my_model_RegressionModel")
model = joblib.load(model_path)
logger.info("Model loaded successfully from: %s", model_path)
except Exception as e:
logger.error("Failed to load model due to: %s", e)
raise
def run(data: dict) -> dict:
"""
Run scoring on the input data.
Parameters:
data (dict): A dictionary containing the key "data" mapped to the input features.
Returns:
dict: A dictionary with key 'result' containing model predictions,
or key 'error' with an error message.
"""
try:
# Validate input
if not isinstance(data, dict):
raise ValueError("Input must be a dictionary with key 'data'.")
raw_data = data.get("data")
if raw_data is None:
raise ValueError("Missing 'data' key in input.")
# Convert to numpy array
input_data = np.array(raw_data)
logger.info("Input data converted to numpy array with shape: %s", input_data.shape)
# Generate predictions
predictions = model.predict(input_data)
logger.info("Prediction successful.")
# Return results in a structured format
return {"result": predictions.tolist()}
except Exception as e:
logger.error("Error during run: %s", e)
return {"error": str(e)}