AI Cloud Platforms
AWS SageMaker
import boto3
import sagemaker
from sagemaker.huggingface import HuggingFaceModel
class AWSAIPlatform:
def __init__(self, role_arn: str):
self.role = role_arn
self.session = sagemaker.Session()
self.sm_client = boto3.client("sagemaker")
def deploy_huggingface_model(self, model_name: str, instance_type: str = "ml.g4dn.xlarge"):
huggingface_model = HuggingFaceModel(
model_data="s3://my-bucket/model.tar.gz",
role=self.role,
transformers_version="4.26",
pytorch_version="1.13",
py_version="py39"
)
predictor = huggingface_model.deploy(
initial_instance_count=1,
instance_type=instance_type,
endpoint_name=model_name
)
return predictor
def create_endpoint_config(self, model_name: str, instance_type: str) -> str:
response = self.sm_client.create_endpoint_config(
EndpointConfigName=f"{model_name}-config",
ProductionVariants=[
{
"VariantName": "primary",
"ModelName": model_name,
"InstanceType": instance_type,
"InitialInstanceCount": 1,
"InitialVariantWeight": 1.0
}
]
)
return response["EndpointConfigArn"]
aws = AWSAIPlatform(role_arn="arn:aws:iam::role/SageMakerRole")
predictor = aws.deploy_huggingface_model("my-model")
GCP Vertex AI
from google.cloud import aiplatform
from google.cloud.aiplatform import Model, Endpoint
class GCPAIPlatform:
def __init__(self, project_id: str, region: str):
self.project_id = project_id
self.region = region
aiplatform.init(project=project_id, location=region)
def deploy_model(self, model_id: str, machine_type: str = "n1-standard-4"):
model = Model(model_name=model_id)
endpoint = model.deploy(
deployed_model_display_name="my-endpoint",
machine_type=machine_type,
min_replica_count=1,
max_replica_count=3
)
return endpoint
def predict(self, endpoint: Endpoint, instances: list):
predictions = endpoint.predict(instances=instances)
return predictions.predictions
gcp = GCPAIPlatform(project_id="my-project", region="us-central1")
endpoint = gcp.deploy_model("models/my-model")
predictions = gcp.predict(endpoint, [{"text": "Hello world"}])
Azure OpenAI
from azure.ai.openai import OpenAIClient, AzureOpenAI
from azure.identity import DefaultAzureCredential
class AzureAIPlatform:
def __init__(self, endpoint: str, api_key: str):
self.client = AzureOpenAI(
api_key=api_key,
azure_endpoint=endpoint,
api_version="2024-02-15-preview"
)
def chat_completion(self, messages: list, model: str = "gpt-4"):
response = self.client.chat.completions.create(
model=model,
messages=messages,
temperature=0.7,
max_tokens=800
)
return response.choices[0].message.content
def generate_embeddings(self, texts: list, model: str = "text-embedding-ada-002"):
embeddings = self.client.embeddings.create(
model=model,
input=texts
)
return [e.embedding for e in embeddings.data]
azure = AzureAIPlatform(
endpoint="https://myresource.openai.azure.com/",
api_key="my-api-key"
)
response = azure.chat_completion([{"role": "user", "content": "Hello"}])
embeddings = azure.generate_embeddings(["text1", "text2"])
Platform Comparison
| Feature | AWS | GCP | Azure |
|---|
| Foundation Models | Bedrock | PaLM API | OpenAI |
| Training | SageMaker | Vertex AI | ML Studio |
| Vision | Rekognition | Vision AI | Computer Vision |
| NLP | Comprehend | NL API | Language API |
| Speech | Polly/Transcribe | Speech-to-Text | Speech Services |
| Pricing | Pay-per-use | Pay-per-use | Pay-per-use |
Best Practices
- Compare pricing across platforms for your workload
- Use managed services to reduce operational overhead
- Implement multi-cloud strategies for resilience
- Leverage spot instances for training cost savings
- Monitor and optimize GPU utilization
- Use appropriate instance types for inference vs training