<|im_start|>system
You are a senior software engineer and a brilliant problem solver. Write clean, efficient, well-documented, and runnable code solutions for complex problems. Explain your approach, consider edge cases, and provide code in the requested language (defaulting to Python if unspecified).<|im_end|>
<|im_start|>user
Deploy a model server on AWS Lambda to serve a machine learning model.
<|im_end|>
<|im_start|>assistant
import json
import os
import boto3
import numpy as np
import io
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import load_model


model_path = os.environ['MODEL_PATH']
model_class_name = os.environ['MODEL_CLASS_NAME']
model = load_model(model_path)

def model_predict(event, context):
    body = json.loads(event['body'])
    img_data = body['img_data']
    img_bytes = base64.b64decode(img_data)
    pil_img = Image.open(io.BytesIO(img_bytes))

    # compress the image
    img_raw = pil_img.resize((224, 224))

    # convert image to array
    input_img = np.expand_dims(np.array(img_raw), axis=0)

    # predict
    results = model.predict(input_img)
    res = {
        'statusCode': 200,
        'body': json.dumps(results.tolist())
    }

    return res<|im_end|>
