From 5c191a20b75c1cde66189e8d3f2eb70925be6b24 Mon Sep 17 00:00:00 2001 From: Hakan Erol <47988814+hakan458@users.noreply.github.com> Date: Mon, 15 Apr 2024 11:18:06 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20DIA-957:=20Delete=20predictions=20when?= =?UTF-8?q?=20ModelVersion=20updated=20or=20ModelR=E2=80=A6=20(#5719)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …un deleted ### PR fulfills these requirements - [ ] Commit message(s) and PR title follows the format `[fix|feat|ci|chore|doc]: TICKET-ID: Short description of change made` ex. `fix: DEV-XXXX: Removed inconsistent code usage causing intermittent errors` - [ ] Tests for the changes have been added/updated (for bug fixes/features) - [ ] Docs have been added/updated (for bug fixes/features) - [ ] Best efforts were made to ensure docs/code are concise and coherent (checked for spelling/grammatical errors, commented out code, debug logs etc.) - [ ] Self-reviewed and ran all changes on a local instance (for bug fixes/features) #### Change has impacts in these area(s) _(check all that apply)_ - [ ] Product design - [ ] Backend (Database) - [ ] Backend (API) - [ ] Frontend ### Describe the reason for change _(link to issue, supportive screenshots etc.)_ #### What does this fix? _(if this is a bug fix)_ #### What is the new behavior? _(if this is a breaking or feature change)_ #### What is the current behavior? _(if this is a breaking or feature change)_ #### What libraries were added/updated? _(list all with version changes)_ #### Does this change affect performance? _(if so describe the impacts positive or negative)_ #### Does this change affect security? _(if so describe the impacts positive or negative)_ #### What alternative approaches were there? _(briefly list any if applicable)_ #### What feature flags were used to cover this change? _(briefly list any if applicable)_ ### Does this PR introduce a breaking change? _(check only one)_ - [ ] Yes, and covered entirely by feature flag(s) - [ ] Yes, and covered partially by feature flag(s) - [ ] No - [ ] Not sure (briefly explain the situation below) ### What level of testing was included in the change? _(check all that apply)_ - [ ] e2e - [ ] integration - [ ] unit ### Which logical domain(s) does this change affect? _(for bug fixes/features, be as precise as possible. ex. Authentication, Annotation History, Review Stream etc.)_ --- label_studio/ml_models/models.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/label_studio/ml_models/models.py b/label_studio/ml_models/models.py index ae611529c6f0..0c63ce782b12 100644 --- a/label_studio/ml_models/models.py +++ b/label_studio/ml_models/models.py @@ -7,6 +7,7 @@ from ml_model_providers.models import ModelProviderConnection from projects.models import Project from rest_framework.exceptions import ValidationError +from tasks.models import Prediction def validate_string_list(value): @@ -54,11 +55,20 @@ class Meta: parent_model = models.ForeignKey(ModelInterface, related_name='model_versions', on_delete=models.CASCADE) + prompt = models.TextField(_('prompt'), null=False, blank=False, help_text='Prompt to execute') + @property def full_title(self): return f'{self.parent_model.title}__{self.title}' - prompt = models.TextField(_('prompt'), null=False, blank=False, help_text='Prompt to execute') + def delete(self, *args, **kwargs): + """ + Deletes Predictions associated with ModelVersion + """ + model_runs = ModelRun.objects.filter(model_version=self.id) + for model_run in model_runs: + model_run.delete_predictions() + super().delete(*args, **kwargs) class ThirdPartyModelVersion(ModelVersion): @@ -159,3 +169,17 @@ def output_file_name(self): @property def error_file_name(self): return f'{self.project.id}_{self.model_version.pk}_{self.pk}/error.csv' + + def delete_predictions(self): + """ + Deletes any predictions that have originated from a ModelRun + """ + + Prediction.objects.filter(model_run=self.id).delete() + + def delete(self, *args, **kwargs): + """ + Deletes Predictions associated with ModelRun + """ + self.delete_predictions() + super().delete(*args, **kwargs)