generated from bananaml/serverless-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
27 lines (19 loc) · 770 Bytes
/
app.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
from sentence_transformers import CrossEncoder
import torch
# Init is ran on server startup
# Load your model to GPU as a global variable here using the variable name "model"
def init():
global model
device = 0 if torch.cuda.is_available() else -1
model = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2', device=device)
# Inference is ran for every server call
# Reference your preloaded global model variable here.
def inference(model_inputs:dict) -> dict:
global model
# Parse out your arguments
sentences = model_inputs.get('sentence_pairs', None)
if sentences == None:
return {'message': "No sentences provided"}
# Run the model
result = model.predict(sentences)
return {"output": result.tolist()}