Skip to content

Commit acbb7b6

Browse files
committed
Update lambda to use new model
1 parent 36ad19b commit acbb7b6

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/backend/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ COPY lambda_requirements.txt ${LAMBDA_TASK_ROOT}
77
RUN pip install --compile --no-cache-dir -r lambda_requirements.txt
88

99
# Copy model
10-
COPY trained/gpt2-728 ${LAMBDA_TASK_ROOT}/trained/gpt2-728
10+
COPY trained/mini-copilot ${LAMBDA_TASK_ROOT}/trained/mini-copilot
11+
12+
# Copy tokenizer
13+
COPY trained/mini-copilot-tokenizer ${LAMBDA_TASK_ROOT}/trained/mini-copilot-tokenizer
1114

1215
# Copy function code
1316
COPY lambda.py ${LAMBDA_TASK_ROOT}

src/backend/lambda.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
55

66
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7-
tokenizer = AutoTokenizer.from_pretrained("gpt2")
8-
model = AutoModelForCausalLM.from_pretrained("trained/gpt2-728")
7+
tokenizer = AutoTokenizer.from_pretrained(
8+
"trained/mini-copilot-tokenizer/tokenizer_10M")
9+
model = AutoModelForCausalLM.from_pretrained("trained/mini-copilot/gpt2-large")
910
model.to(device)
1011
model.eval()
1112

13+
pipe = pipeline("text-generation", model=model,
14+
tokenizer=tokenizer, device=device, max_new_tokens=32)
1215

13-
# Function to predict the next token
14-
def predict_next_token(input_text, model=model, tokenizer=tokenizer, max_length=50):
15-
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=device)
16-
17-
predicted_text = pipe(input_text)[0]["generated_text"]
1816

19-
input_text_len = len(input_text)
20-
21-
return predicted_text[input_text_len:]
17+
# Function to predict the next token
18+
def get_completion(inp: str) -> str:
19+
return pipe(inp)[0]["generated_text"][len(inp):]
2220

2321

22+
# Lambda handler
2423
def handler(event, context):
2524
try:
2625
if event.get('isBase64Encoded', False):
@@ -29,4 +28,4 @@ def handler(event, context):
2928
body = event['body']
3029
except (KeyError, json.JSONDecodeError) as e:
3130
return {"statusCode": 400, "body": f"Error processing request: {str(e)}"}
32-
return {"statusCode": 200, "body": predict_next_token(body)}
31+
return {"statusCode": 200, "body": get_completion(body)}

0 commit comments

Comments
 (0)