from truefoundry.ml import get_client, SklearnFramework, sklearn_infer_schema
import joblib
import numpy ass np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
# Define training data
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
y = np.array([1, 1, 2, 2])
# Create and train the model
clf = make_pipeline(StandardScaler(), SVC(gamma="auto"))
model = clf.fit(X, y)
# Save the model
joblib.dump(clf, "sklearn-pipeline.joblib")
# Initialize the TrueFoundry client
client = get_client()
# Infer model schema
model_schema = sklearn_infer_schema(
model_input=X, model=model, infer_method_name="predict"
)
# Log the model
model_version = client.log_model(
ml_repo="my-classification-project",
name="my-sklearn-model",
model_file_or_folder="sklearn-pipeline.joblib",
# To make the model deployable and generate the inference script,
# model file, and schema(with the method name) are required.
framework=SklearnFramework(
model_filepath="sklearn-pipeline.joblib",
model_schema=model_schema,
),
# Auto-captures the current environment details (e.g., python_version, pip_packages)
# based on the framework. If you want to override, you can add this block:
# environment=ModelVersionEnvironment(
# python_version="3.10",
# pip_packages=[
# "joblib==1.4.2",
# "numpy==1.26.4",
# "pandas==2.2.3",
# "scikit-learn==1.6.1",
# ],
# ),
)
# Output the model's Fully Qualified Name (FQN)
print(f"Model version logged successfully: {model_version.fqn}")