label-studio-ml-backend icon indicating copy to clipboard operation
label-studio-ml-backend copied to clipboard

[fix] Fix KeyError getting project id for HuggingFace and Sklearn bac…

Open ginic opened this issue 6 months ago • 0 comments

This fixes a problem retrieving the project ID during the webhook for model training in the huggingface_ner, bert_classifier, and sklearn_text_classifier backends, resolving https://github.com/HumanSignal/label-studio-ml-backend/issues/768 and https://github.com/HumanSignal/label-studio-ml-backend/issues/745. There is also some trailing whitespace removed in the respective backend's model.py files to be consistent with PEP8.

I confirmed by rebuilding and running the backends in docker containers that I am now able to successfully complete training after triggering it in the "Model" section of project settings with huggingface_ner, bert_classifier, and sklearn_text_classifier backends.

Previously, when model training was triggered, a KeyError arose as follows:

bert_classifier  | [2025-06-25 16:28:19,504] [ERROR] [label_studio_ml.api::log_exception::875] Exception on /webhook [POST]
bert_classifier  | Traceback (most recent call last):
bert_classifier  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1511, in wsgi_app
bert_classifier  |     response = self.full_dispatch_request()
bert_classifier  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bert_classifier  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 919, in full_dispatch_request
bert_classifier  |     rv = self.handle_user_exception(e)
bert_classifier  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bert_classifier  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 917, in full_dispatch_request
bert_classifier  |     rv = self.dispatch_request()
bert_classifier  |          ^^^^^^^^^^^^^^^^^^^^^^^
bert_classifier  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 902, in dispatch_request
bert_classifier  |     return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
bert_classifier  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bert_classifier  |   File "/usr/local/lib/python3.11/site-packages/label_studio_ml/api.py", line 126, in webhook
bert_classifier  |     result = model.fit(event, data)
bert_classifier  |              ^^^^^^^^^^^^^^^^^^^^^^
bert_classifier  |   File "/app/model.py", line 125, in fit
bert_classifier  |     project_id = data['annotation']['project']
bert_classifier  |                  ~~~~^^^^^^^^^^^^^^
bert_classifier  | KeyError: 'annotation'

ginic avatar Jun 26 '25 18:06 ginic