1. Real-time Inference with GCP AI Platform Predictions

    Python

    To set up real-time inference with GCP AI Platform Predictions using Pulumi, you need to follow these steps:

    1. Provision a Machine Learning (ML) model in GCP that you wish to use for inference.
    2. Deploy your model to the AI Platform Predictions service.
    3. Create an endpoint for serving predictions in real-time.
    4. (Optional) Configure IAM bindings for identity and access management if you need to control access to your ML model.

    Below is a Pulumi Python program that demonstrates these steps. It assumes that you already have a trained ML model ready to be deployed for serving predictions. You would need to replace placeholders with appropriate values, such as the paths to your model and any specific configurations.

    import pulumi import pulumi_gcp as gcp # Assume we already have a trained ML model ready for deployment. # Replace `YOUR_MODEL_NAME`, `YOUR_PROJECT`, `YOUR_REGION`, and `YOUR_MODEL_STORAGE_PATH` # with your model name, GCP project ID, GCP region, and Cloud Storage path to the trained model. # Step 1: Register the ML Model. # This will create a new model resource that you can use to manage model versions. ai_model = gcp.ml.EngineModel("my-ai-model", name="YOUR_MODEL_NAME", project="YOUR_PROJECT", description="Description of the model", regions=["YOUR_REGION"], online_prediction_logging=True) # Step 2: Create a model version based on that model, which is an endpoint # for the trained ML model stored in Cloud Storage. ai_model_version = gcp.ml.EngineModelVersion("my-ai-model-version", name="v1", model=ai_model.name, description="Version 1 of the model", runtime_version="2.1", deployment_uri="YOUR_MODEL_STORAGE_PATH", machine_type="mls1-c1-m2") # Step 3: Create an AI Platform endpoint. This step makes the model version # accessible for online prediction requests. ai_endpoint = gcp.vertex.AiEndpoint("my-ai-endpoint", project="YOUR_PROJECT", location="YOUR_REGION", display_name="my-endpoint", description="Endpoint for real-time predictions") # Step 4: (Optional) Configure IAM binding for the endpoint. # This step is only necessary if you need to set up access control. # Replace `MEMBER` with the member you want to add, such as `user:email@example.com`. ai_endpoint_iam_binding = gcp.ml.EngineModelIamBinding("my-ai-endpoint-iam-binding", project="YOUR_PROJECT", region="YOUR_REGION", model_id=ai_model.name, role="roles/ml.modelUser", # Assign the role that has permissions to use the model. members=["MEMBER"]) # Export the endpoint URL, which you can use to make prediction requests. pulumi.export("endpoint_url", ai_endpoint.predictor_ids.apply( lambda ids: f"https://CONTENT-{ai_model.region}.aiplatform.googleapis.com/v1/projects/{ai_model.project}/locations/{ai_model.region}/endpoints/{ids[0]}" ))

    In this code:

    • We create an AI model resource (EngineModel) corresponding to our ML model.
    • We deploy a version of this model with EngineModelVersion. Here, deployment_uri points to where your trained model is stored, typically a Cloud Storage bucket.
    • We then create an endpoint (AiEndpoint) for serving predictions using this model.
    • We set up IAM bindings with EngineModelIamBinding for controlling who can access the model. This step is optional and depends on your access control needs.

    Please replace placeholders with values specific to your environment and model. Once the deployment is successful, you can use the exported endpoint URL to send real-time prediction requests to the AI Platform Predictions service.