Please check https://github.com/heartexlabs/label-studio-ml-backend instead
Python interface for running ML backend server and using it for active learning & prelabeling & prediction within Heartex platform
First make sure you have Redis server running (otherwise you can use only prediction, not active learning).
Install Heartex SDK:
git clone https://github.com/heartexlabs/pyheartex.git
cd pyheartex/
pip install -r requirements.txt
pip install -e .Last thing you should do is to start RQ workers in the background:
rq worker defaultHere is an example how to start serving image classifier:
cd examples/docker
docker-compose upAll you need to replace with your own model is to change loading, inference and training scripts from this file.
Quick start guide provides the usage of the following popular machine learning frameworks within Heartex platform:
Let's serve scikit-learn model for text classification.
You can simply launch
python examples/quickstart.pyThis script looks like
fromhtx.adapters.sklearnimportservefromsklearn.feature_extraction.textimportTfidfVectorizerfromsklearn.linear_modelimportLogisticRegressionfromsklearn.pipelineimportmake_pipelineif__name__=="__main__":
# Creating sklearn-compatible modelmy_model=make_pipeline(TfidfVectorizer(), LogisticRegression())
# Start serving this modelserve(my_model)It starts serving at http://localhost:16118 listening for Heartex event. To connect your model, go to Heartex -> Settings -> Machine learning page and choose "Add custom model".
Or you can use Heartex API to activate your model:
curl -X POST -H 'Content-Type: application/json' \
-H 'Authorization: Token <PUT-YOUR-TOKEN-HERE>' \
-d '[{"url": "$HOST:$PORT", "name": "my_model", "title": "My model", "description": "My new model deployed on Heartex"}]' \
http://go.heartex.net/api/projects/{project-id}/backends/where $HOST:$PORT is your server URL that should be accessible from the outside.
You can integrate FastAI models similarly to scikit-learn. Check this example to learn how to plug in updateable image classifier.
When you want to go beyond using sklearn compatible API, you can build your own model, by making manually input/output interface conversion. You have to subclass Heartex models as follows:
fromhtx.base_modelimportBaseModel# This class exposes methods needed to handle model in the runtime (loading into memory, running predictions)classMyModel(BaseModel):
defget_input(self, task):
"""Extract input from serialized task"""passdefget_output(self, task):
"""Extract output from serialized task"""passdefload(self, train_output):
"""Loads model into memory. `train_output` dict is actually the output the `train` method (see below)"""passdefpredict(self, tasks):
"""Get list of tasks, already processed by `get_input` method, and returns completions in Heartex format"""pass# This method handles model retrainingdeftrain(input_tasks, output_model_dir, **kwargs):
""" :param input_tasks: list of tasks already processed by `get_input` :param output_model_dir: output directory where you can optionally store model resources :param kwargs: any additional kwargs taken from `train_kwargs` :return: `train_output` dict for consequent model loading """pass