-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
51 lines (47 loc) · 1.62 KB
/
handler.py
File metadata and controls
51 lines (47 loc) · 1.62 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
import json
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
def serverless_pipeline(model_path='./model'):
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
def summary(article):
input_ids = tokenizer(article, return_tensors="pt").input_ids
output = model.generate(
input_ids,
max_length=50,
num_beams=5,
early_stopping=True
)
summary = tokenizer.decode(output[0], skip_special_tokens=True)
return(summary)
return(summary)
# initializes the pipeline
summarization_pipeline = serverless_pipeline()
def handler(event, context):
try:
print(event)
print(context)
# loads the incoming event into a dictonary
body = json.loads(event['body'])
# uses the pipeline to predict the answer
answer = summarization_pipeline(article=body['article'])
return {
"statusCode": 200,
"headers": {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": True
},
"body": json.dumps({'answer': answer})
}
except Exception as e:
print(repr(e))
return {
"statusCode": 500,
"headers": {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": True
},
"body": json.dumps({"error": repr(e)})
}