This repository has been archived by the owner on May 27, 2024. It is now read-only.
generated from bananaml/serverless-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.py
40 lines (29 loc) · 1.34 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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
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
tokenizer = AutoTokenizer.from_pretrained("togethercomputer/GPT-JT-6B-v1")
model = AutoModelForCausalLM.from_pretrained("togethercomputer/GPT-JT-6B-v1", torch_dtype=torch.float16, low_cpu_mem_usage=True).to("cuda")
# 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)
max_new = model_inputs.get('max_new_tokens', 10)
if prompt == None:
return {'message': "No prompt provided"}
# Tokenize inputs
input_tokens = tokenizer.encode(prompt, return_tensors="pt").to(device)
# Run the model, and set `pad_token_id` to `eos_token_id`:50256 for open-end generation
output = model.generate(input_tokens, max_new_tokens=max_new, pad_token_id=50256)
# 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