-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.py
28 lines (21 loc) · 827 Bytes
/
score.py
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
import json
from sentence_transformers import SentenceTransformer
from azureml.core import Model
def init():
global model, tokenizer
# The model and tokenizer are loaded from a path where the model is registered in Azure
model_path = Model.get_model_path('bge-m3')
model = SentenceTransformer(model_path)
def run(raw_data):
try:
# Convert the JSON data into a Python dictionary
data = json.loads(raw_data)
texts = data['data']
# Prediction
predictions = model.encode(texts)
predictions = predictions.tolist() # convert to a list to be able to serialize it
# Return predictions in a JSON format
return json.dumps({"predictions": predictions})
except Exception as e:
error = str(e)
return json.dumps({"error": error})