# SageMaker JumpStart provides APIs as part of SageMaker SDK that allow you to deploy and fine-tune models in network isolation using scripts that SageMaker maintains.

from sagemaker.jumpstart.model import JumpStartModel


model = JumpStartModel(model_id="huggingface-textgeneration1-bloomz-7b1-fp16")
example_payloads = model.retrieve_all_examples()

predictor = model.deploy()

for payload in example_payloads:
    response = predictor.predict(payload)
    print("Input:\n", payload)
    print("Output:\n", response)

# Fine-tune a Hugging Face Hub model using the SageMaker Python SDK v3.
# See https://github.com/huggingface/transformers/tree/v4.56.2/examples/pytorch

import boto3
from sagemaker.train.model_trainer import ModelTrainer
from sagemaker.core.training.configs import Compute, SourceCode
from sagemaker.core import image_uris

training_image = image_uris.retrieve(
    framework="huggingface",
    region=boto3.Session().region_name,
    version="4.56.2",
    base_framework_version="pytorch2.8.0",
    py_version="py312",
    image_scope="training",
)

trainer = ModelTrainer(
    training_image=training_image,
    source_code=SourceCode(
        command=(
            "git clone --depth 1 --branch v4.56.2 "
            "https://github.com/huggingface/transformers.git /tmp/transformers && "
            "python /tmp/transformers/examples/pytorch/language-modeling/run_clm.py "
            "--model_name_or_path bigscience/bloomz-7b1 "
            "--output_dir /opt/ml/model "
            "--do_train"
        ),
    ),
    compute=Compute(instance_type="ml.g5.2xlarge"),
)
trainer.train()
