|
61 | 61 | "outputs": [], |
62 | 62 | "source": [ |
63 | 63 | "import uuid\n", |
| 64 | + "import boto3\n", |
64 | 65 | "from sagemaker.core import image_uris\n", |
65 | 66 | "from sagemaker.core.helper.session_helper import Session\n", |
66 | 67 | "\n", |
|
71 | 72 | "MLFLOW_TRACKING_ARN = \"XXXXX\"\n", |
72 | 73 | "\n", |
73 | 74 | "# AWS Configuration\n", |
74 | | - "AWS_REGION = Session.boto_region_name\n", |
| 75 | + "boto_session = boto3.Session()\n", |
| 76 | + "sagemaker_session = Session(boto_session=boto_session)\n", |
| 77 | + "AWS_REGION = sagemaker_session.boto_region_name\n", |
75 | 78 | "\n", |
76 | 79 | "# Get PyTorch training image dynamically\n", |
77 | 80 | "PYTORCH_TRAINING_IMAGE = image_uris.retrieve(\n", |
|
297 | 300 | "\n", |
298 | 301 | "# Training on SageMaker managed infrastructure\n", |
299 | 302 | "model_trainer = ModelTrainer(\n", |
| 303 | + " sagemaker_session=sagemaker_session,\n", |
300 | 304 | " training_image=PYTORCH_TRAINING_IMAGE,\n", |
301 | 305 | " source_code=SourceCode(\n", |
302 | 306 | " source_dir=training_code_dir,\n", |
|
333 | 337 | "from mlflow import MlflowClient\n", |
334 | 338 | "\n", |
335 | 339 | "client = MlflowClient()\n", |
336 | | - "registered_model = client.get_registered_model(name=MLFLOW_REGISTERED_MODEL_NAME)\n", |
337 | 340 | "\n", |
338 | | - "latest_version = registered_model.latest_versions[0]\n", |
| 341 | + "# Use search_model_versions (compatible with MLflow 3.x)\n", |
| 342 | + "# Note: latest_versions attribute was removed in MLflow 3.x\n", |
| 343 | + "model_versions = client.search_model_versions(\n", |
| 344 | + " filter_string=f\"name='{MLFLOW_REGISTERED_MODEL_NAME}'\",\n", |
| 345 | + " order_by=['version_number DESC'],\n", |
| 346 | + " max_results=1\n", |
| 347 | + ")\n", |
| 348 | + "\n", |
| 349 | + "if not model_versions:\n", |
| 350 | + " raise ValueError(f\"No versions found for model '{MLFLOW_REGISTERED_MODEL_NAME}'\")\n", |
| 351 | + "\n", |
| 352 | + "latest_version = model_versions[0]\n", |
339 | 353 | "model_version = latest_version.version\n", |
340 | 354 | "model_source = latest_version.source\n", |
341 | 355 | "\n", |
342 | | - "# Get S3 URL of model files (for info only)\n", |
343 | | - "artifact_uri = client.get_model_version_download_uri(MLFLOW_REGISTERED_MODEL_NAME, model_version)\n", |
344 | | - "\n", |
345 | 356 | "# MLflow model registry path to use with ModelBuilder\n", |
346 | 357 | "mlflow_model_path = f\"models:/{MLFLOW_REGISTERED_MODEL_NAME}/{model_version}\"\n", |
347 | 358 | "\n", |
348 | 359 | "print(f\"Registered Model: {MLFLOW_REGISTERED_MODEL_NAME}\")\n", |
349 | 360 | "print(f\"Latest Version: {model_version}\")\n", |
350 | | - "print(f\"Source: {model_source}\")\n", |
351 | | - "print(f\"Model artifacts location: {artifact_uri}\")" |
| 361 | + "print(f\"Source (artifact location): {model_source}\")\n", |
| 362 | + "print(f\"MLflow model path for deployment: {mlflow_model_path}\")" |
352 | 363 | ] |
353 | 364 | }, |
354 | 365 | { |
|
481 | 492 | "metadata": {}, |
482 | 493 | "outputs": [], |
483 | 494 | "source": [ |
484 | | - "import boto3\n", |
485 | | - "\n", |
486 | 495 | "# Test with JSON input\n", |
487 | 496 | "test_data = [[0.1, 0.2, 0.3, 0.4]]\n", |
488 | 497 | "\n", |
489 | | - "runtime_client = boto3.client('sagemaker-runtime')\n", |
490 | | - "response = runtime_client.invoke_endpoint(\n", |
491 | | - " EndpointName=core_endpoint.endpoint_name,\n", |
492 | | - " Body=json.dumps(test_data),\n", |
493 | | - " ContentType='application/json'\n", |
| 498 | + "result = core_endpoint.invoke(\n", |
| 499 | + " body=json.dumps(test_data),\n", |
| 500 | + " content_type='application/json'\n", |
494 | 501 | ")\n", |
495 | 502 | "\n", |
496 | | - "prediction = json.loads(response['Body'].read().decode('utf-8'))\n", |
| 503 | + "# Decode and display the result\n", |
| 504 | + "prediction = json.loads(result.body.read().decode('utf-8'))\n", |
497 | 505 | "print(f\"Input: {test_data}\")\n", |
498 | 506 | "print(f\"Prediction: {prediction}\")" |
499 | 507 | ] |
500 | 508 | }, |
| 509 | + { |
| 510 | + "cell_type": "code", |
| 511 | + "execution_count": null, |
| 512 | + "metadata": {}, |
| 513 | + "outputs": [], |
| 514 | + "source": [ |
| 515 | + "# Test with different tensor inputs\n", |
| 516 | + "test_inputs = [\n", |
| 517 | + " [[0.5, 0.3, 0.2, 0.1]],\n", |
| 518 | + " [[0.9, 0.1, 0.8, 0.2]],\n", |
| 519 | + " [[0.2, 0.7, 0.4, 0.6]]\n", |
| 520 | + "]\n", |
| 521 | + "\n", |
| 522 | + "for i, test_input in enumerate(test_inputs, 1):\n", |
| 523 | + " result = core_endpoint.invoke(\n", |
| 524 | + " body=json.dumps(test_input),\n", |
| 525 | + " content_type='application/json'\n", |
| 526 | + " )\n", |
| 527 | + " \n", |
| 528 | + " prediction = json.loads(result.body.read().decode('utf-8'))\n", |
| 529 | + " print(f\"Test {i} - Input {test_input}: {prediction}\")\n", |
| 530 | + " print(\"-\" * 50)" |
| 531 | + ] |
| 532 | + }, |
501 | 533 | { |
502 | 534 | "cell_type": "markdown", |
503 | 535 | "metadata": {}, |
|
0 commit comments