generated from bananaml/serverless-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
32 lines (26 loc) · 1.06 KB
/
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
28
29
30
31
32
from transformers import pipeline
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 = pipeline("summarization", model="facebook/bart-large-cnn", 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
input = model_inputs.get('input', None)
max_length = model_inputs.get('max_length', None)
min_length = model_inputs.get('min_length', None)
if input == None:
return {'message': "No input provided"}
if max_length == None:
return {'message': "No max_length provided"}
if min_length == None:
return {'message': "No min_length provided"}
# Run the model
result = model(input, max_length=max_length, min_length=min_length, do_sample=False)[0]
# Return the results as a dictionary
return result