| """ |
| package.py β Build model.tar.gz for SageMaker |
| ================================================ |
| Run this script once before deploying. |
| It bundles your code/ folder into the exact archive structure SageMaker expects. |
| |
| Usage: |
| cd "RL Envir" |
| python sagemaker/package.py |
| |
| Output: |
| sagemaker/model.tar.gz β upload this to S3, then point SageMaker at it |
| |
| What goes inside model.tar.gz: |
| code/ |
| βββ inference.py β SageMaker entry point (the 4 handlers) |
| βββ classifier.py β your rule-based classifier logic |
| |
| SageMaker unpacks the archive and looks for code/inference.py automatically |
| when you use the SKLearn or generic Python containers. |
| """ |
|
|
| import os |
| import tarfile |
|
|
| |
| HERE = os.path.dirname(os.path.abspath(__file__)) |
| OUTPUT_TAR = os.path.join(HERE, "model.tar.gz") |
|
|
| |
| FILES_TO_PACK = { |
| "code/inference.py": os.path.join(HERE, "inference.py"), |
| "code/classifier.py": os.path.join(HERE, "classifier.py"), |
| } |
|
|
|
|
| def build(): |
| print("\n Building model.tar.gz ...") |
| print(f" Output β {OUTPUT_TAR}\n") |
|
|
| |
| missing = [src for src in FILES_TO_PACK.values() if not os.path.exists(src)] |
| if missing: |
| print(" β Missing files:") |
| for f in missing: |
| print(f" {f}") |
| raise FileNotFoundError("Fix missing files then re-run.") |
|
|
| |
| with tarfile.open(OUTPUT_TAR, "w:gz") as tar: |
| for archive_name, source_path in FILES_TO_PACK.items(): |
| tar.add(source_path, arcname=archive_name) |
| size_kb = os.path.getsize(source_path) / 1024 |
| print(f" + {archive_name:<30} ({size_kb:.1f} KB)") |
|
|
| |
| tar_size_kb = os.path.getsize(OUTPUT_TAR) / 1024 |
| print(f"\n β
Done! model.tar.gz = {tar_size_kb:.1f} KB") |
|
|
| |
| print("\n Contents of model.tar.gz:") |
| with tarfile.open(OUTPUT_TAR, "r:gz") as tar: |
| for member in tar.getmembers(): |
| print(f" {member.name:<35} {member.size / 1024:.1f} KB") |
|
|
| print(f"\n Next step β upload to S3:") |
| print(f" aws s3 cp {OUTPUT_TAR} s3://YOUR-BUCKET/email-gatekeeper/model.tar.gz\n") |
|
|
|
|
| if __name__ == "__main__": |
| build() |
|
|