Skip to content

Add ConTextTab model #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/tasks/src/model-libraries-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,62 @@ wav = model.generate(text, audio_prompt_path=AUDIO_PROMPT_PATH)
ta.save("test-2.wav", wav, model.sr)`,
];

export const contexttab = (): string[] => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, these 3 snippets don't really show how to use the model in the repo. Adding snippets would be very beneficial if contexttab had a from_pretrained method for instance to load a pre-trained model from a repo_id. By doing so, you allow users to share their own weights in new repos which will result in personalized snippets to reuse their model. Does that make sense to you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I understand the confusion! I've omitted the default parameters, but both ConTextTabClassifier and ConTextTabRegressor shown in the example use the HF hub model weights, taking a checkpoint and checkpoint_revision argument pointing to the HF repo. I can add it to be more verbose if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Wauplin : done ✅

const installSnippet = `pip install git+https://github.com/SAP-samples/contexttab`;

const classificationSnippet = `# Run a classification task
from sklearn.datasets import load_breast_cancer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

from contexttab import ConTextTabClassifier

# Load sample data
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)

# Initialize a classifier
# You can omit checkpoint and checkpoint_revision to use the default model
clf = ConTextTabClassifier(checkpoint="l2/base.pt", checkpoint_revision="v1.0.0", bagging=1, max_context_size=2048)

clf.fit(X_train, y_train)

# Predict probabilities
prediction_probabilities = clf.predict_proba(X_test)
# Predict labels
predictions = clf.predict(X_test)
print("Accuracy", accuracy_score(y_test, predictions))`;

const regressionsSnippet = `# Run a regression task
from sklearn.datasets import fetch_openml
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split

from contexttab import ConTextTabRegressor


# Load sample data
df = fetch_openml(data_id=531, as_frame=True)
X = df.data
y = df.target.astype(float)

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)

# Initialize the regressor
# You can omit checkpoint and checkpoint_revision to use the default model
regressor = ConTextTabRegressor(checkpoint="l2/base.pt", checkpoint_revision="v1.0.0", bagging=1, max_context_size=2048)

regressor.fit(X_train, y_train)

# Predict on the test set
predictions = regressor.predict(X_test)

r2 = r2_score(y_test, predictions)
print("R² Score:", r2)`;
return [installSnippet, classificationSnippet, regressionsSnippet];
};

export const cxr_foundation = (): string[] => [
`# pip install git+https://github.com/Google-Health/cxr-foundation.git#subdirectory=python

Expand Down
7 changes: 7 additions & 0 deletions packages/tasks/src/model-libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
repoUrl: "https://github.com/Unbabel/COMET/",
countDownloads: `path:"hparams.yaml"`,
},
contexttab: {
prettyLabel: "ConTextTab",
repoName: "ConTextTab",
repoUrl: "https://github.com/SAP-samples/contexttab",
countDownloads: `path_extension:"pt"`,
snippets: snippets.contexttab,
},
cosmos: {
prettyLabel: "Cosmos",
repoName: "Cosmos",
Expand Down
Loading