generated from bananaml/serverless-template
-
Notifications
You must be signed in to change notification settings - Fork 55
/
app.py
48 lines (35 loc) · 1.33 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from transformers import GPTJForCausalLM, GPT2Tokenizer
import torch
device = "cuda:0" if torch.cuda.is_available() else "cpu"
# Init is ran on server startup
# Load your model to GPU as a global variable here.
def init():
global model
global tokenizer
print("loading to CPU...")
model = GPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B", revision="float16", torch_dtype=torch.float16, low_cpu_mem_usage=True)
print("done")
# conditionally load to GPU
if device == "cuda:0":
print("loading to GPU...")
model.cuda()
print("done")
tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-j-6B")
# Inference is ran for every server call
# Reference your preloaded global model variable here.
def inference(model_inputs:dict) -> dict:
global model
global tokenizer
# Parse out your arguments
prompt = model_inputs.get('prompt', None)
if prompt == None:
return {'message': "No prompt provided"}
# Tokenize inputs
input_tokens = tokenizer.encode(prompt, return_tensors="pt").to(device)
# Run the model
output = model.generate(input_tokens)
# Decode output tokens
output_text = tokenizer.batch_decode(output, skip_special_tokens = True)[0]
result = {"output": output_text}
# Return the results as a dictionary
return result