Modelbit is entirely driven by git. Thus, whenever you push a model for deployment, it internally maintains the deployment as a git repository.
Git-based deployment (Image by author)
Being supported by git, it natively provides all advantages of version control for your deployments, models, and datasets.
What’s more, you can clone into the remote git repository from your local machine and execute all git commands like git pull, git push, or performing branching, etc.
Connect to Modelbit git repo
To access the Modelbit git repository, you should add an SSH key that will connect your local machine to Modelbit.
Open the terminal and run the following command:
ssh-keygen -t rsa -b 4096 -C “My SSH key”
This will create an SSH key. To view it, run the following command:
cat ~/.ssh/id_rsa.pub
The above commands were taken from the Official GitHub Docs.
Now, copy the entire output of the cat command and head over to Git settings in the Modelbit dashboard. Click on “Add Key” and paste the output obtained above. This is demonstrated below:
Add SSH Key (Image by author)
And you are done!
Now we are connected to Modelbit’s remote git repository.
Deploy Model
Let’s push a model for deployment from a Jupyter Notebook. I won’t go into detail as I have already covered this in one of my previous blogs.
In a gist, you should train a model, define a prediction function and push this function object for deployment, as shown below:
## Train Model
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(x, y)
## Define Prediction function
def Linear_Model(input_x):
if isinstance(input_x, (int, float)): ## check input type
return model.predict([[input_x]])[0] ## prediction
else:
return None
## Deploy it
import modelbit
mb = modelbit.login() ## authenticate the notebook here.
mb.deploy(Linear_Model)
Once we deploy a model, we see the following in our Modelbit dashboard:
Deployment Dashboard (Image by author)
Clone Modelbit repo
Let’s clone into this repository to see its contents. Run the following command in the terminal.
modelbit clone my_linear_model
This will clone into Moldebit’s git repository and create a folder my_linear_model.
Cloning deployment repo (Image by author)
Once you run the command, copy the link obtained to authenticate.
As demonstrated above, cloning creates a new local repository, with datasets, deployment, and endpoints in the main branch of the remote git repository of Modelbit.
The current repository structure is as follows:
my_linear_models
├── bin
├── datasets
├── endpoints
└── deployments
└── Linear_Model
├── source.py ## source code
└── data
└── model.pkl ## model pickle
Git push to Modelbit
Now that we have cloned into the remote repository, we can make any changes locally and push them.
Let’s add a dummy CSV file to the Linear_Model folder, commit it to the local repo and push it to the remote repo.
my_linear_models
├── bin
├── datasets
├── endpoints
└── deployments
└── Linear_Model
├── source.py ## source code
├── dummy_data.csv ## added locally
└── data
└── model.pkl ## model pickle
Let’s add the CSV file to the staging area:
git add deployments/Linear_Model/git dummy_data.csv
Next, let’s commit it to the local repo:
git commit -m “Add dummy data csv”
Finally, let’s push it:
git push
With this, the dummy CSV file has been committed to the remote Modelbit git repo.
Note: There’s a reason we added the CSV to the Linear_Model folder but not datasets folder. The datasets folder only supports datasets via SQL queries. The results of those queries are then available at runtime for running deployments. Any other custom dataset isn’t supported yet.
Branching
If you wish to create and work in a separate branch in the remote Modelbit repo, that is also possible.
Create a new branch from the dashboard as follows:
Branching remote repo (Image by author)
Next, say we want to improve our model locally in this branch. In your notebook, you can switch to this new branch as follows:
## notebook.ipynb
mb.switch_branch(“another_branch”)
Now, all new deployments (and other commits, if any) done from the notebook will be pushed to another_branch branch of the remote Modelbit git repo.
The remote Modelbit repo can be automatically synced with your personal GitHub repository.
This is particularly useful for performing GitHub-based code review, CI/CD, and Pull Request workflows on your Modelbit deployments.
#1) Create a new GitHub repo
Below, I have created an empty repo on GitHub.
New GitHub repo (Image by author)
Next, we should give write access to this repository to Modelbit.
#2 Copy the SSH URL of the GitHub repo
Under Code — SSH, copy the URL.
Repo SSH URL (Image by author)
#3) Add Git remote in Modelbit
In the dashboard, go to Git Settings, Add Git Remote and paste the copied repo URL, and Connect Remote.
Add Git Remote to Modelbit (Image by author)
#4) Grant Write access to Modelbit
From the above sync panel, copy the Deploy Key:
Deploy key (Image by author)
Now go to Settings of your GitHub repo, Deploy keys, Add deploy key. Paste the key, give it a title, grant write access and click Add key.
Add deploy key in GitHub repo (Image by author)
And done! The GitHub repository has been automatically updated:
Deployment code in GitHub (Image by author)
Now the remote Modelbit git repository is synced with your GitHub repository, and you can use it for all sorts of collaborative work.