serverless.yaml
provider:
name: aws
runtime: python3.8
...
plugins:
- serverless-python-requirements
...
functions:
handler: lambda_function.lambda_handler
events:
- http:
path: ftaQaInference
method: post
cors: true
The filepath is definately correct. In src/backend/ lambda_function.py I have
import json
import boto3
import torch
from transformers import RobertaTokenizer, RobertaForSequenceClassification
# Set up the S3 client
s3 = boto3.client('s3')
# Load the model from S3
bucket_name = 'sagemaker-fta'
model_key = 'model.pt'
model_path = '/'
s3.download_file(bucket_name, model_key, model_path)
# Load the model
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = RobertaForSequenceClassification.from_pretrained(model_path)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
model.eval()
def lambda_function(event, context):
# Extract the input from the event
input_data = json.loads(event['body'])
question = input_data['question']
document_text = input_data['document_text']
answer = input_data['answer']
# Preprocess the input
input_text = f"{question} {document_text} {answer}"
encoding = tokenizer.encode_plus(
input_text,
add_special_tokens=True,
truncation=True,
padding='max_length',
max_length=128,
return_tensors='pt'
)
input_ids = encoding['input_ids'].to(device)
attention_mask = encoding['attention_mask'].to(device)
# Perform inference
with torch.no_grad():
logits = model(input_ids, attention_mask=attention_mask).logits
probabilities = torch.softmax(logits, dim=1)
predicted_labels = torch.argmax(probabilities, dim=1)
# Prepare the response
response_body = {
'predicted_label': predicted_labels.item(),
'probabilities': probabilities.tolist()[0]
}
response = {
'statusCode': 200,
'body': json.dumps(response_body)
}
return response
And a requirements.txt file with:
boto3==1.26.154
torch==2.0.1
When I invoke the function, in CloudWatch I get:
[ERROR] ModuleNotFoundError: No module named 'lambda_function'
Traceback (most recent call last):
File "/var/task/serverless_sdk/__init__.py", line 144, in wrapped_handler
return user_handler(event, context)
File "/var/task/s_ftaQaInference.py", line 25, in error_handler
raise e
File "/var/task/s_ftaQaInference.py", line 20, in <module>
user_handler = serverless_sdk.get_user_handler('lambda_function.lambda_handler')
File "/var/task/serverless_sdk/__init__.py", line 56, in get_user_handler
user_module = import_module(user_module_name)
File "/var/lang/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
Can anyone spot what I’m doing wrong?