Browse Source

Change example id from auto field to uuid field

pull/1544/head
Hironsan 3 years ago
parent
commit
f381184f21
5 changed files with 40 additions and 16 deletions
  1. 19
      backend/api/migrations/0017_alter_example_id.py
  2. 2
      backend/api/models.py
  3. 11
      backend/api/tasks.py
  4. 18
      backend/api/urls.py
  5. 6
      backend/api/views/download/writer.py

19
backend/api/migrations/0017_alter_example_id.py

@ -0,0 +1,19 @@
# Generated by Django 3.2.8 on 2021-11-02 05:47
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
dependencies = [
('api', '0016_auto_20211018_0556'),
]
operations = [
migrations.AlterField(
model_name='example',
name='id',
field=models.UUIDField(db_index=True, default=uuid.uuid4, editable=False, primary_key=True, serialize=False),
),
]

2
backend/api/models.py

@ -1,4 +1,5 @@
import string
import uuid
from typing import Literal
from auto_labeling_pipeline.models import RequestModelFactory
@ -149,6 +150,7 @@ class Label(models.Model):
class Example(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
meta = models.JSONField(default=dict)
filename = models.FileField(default='.', max_length=1024)
project = models.ForeignKey(

11
backend/api/tasks.py

@ -1,4 +1,5 @@
import itertools
import uuid
from celery import shared_task
from celery.utils.log import get_task_logger
@ -60,12 +61,14 @@ class DataFactory:
self.label_class.objects.bulk_create(labels)
def create_data(self, examples, project):
uuids = sorted(uuid.uuid4() for _ in range(len(examples)))
dataset = [
self.data_class(project=project, **example.data)
for example in examples
self.data_class(id=uid, project=project, **example.data)
for uid, example in zip(uuids, examples)
]
results = self.data_class.objects.bulk_create(dataset)
return results
data = self.data_class.objects.bulk_create(dataset)
data.sort(key=lambda example: example.id)
return data
def create_annotation(self, examples, ids, user, project):
mapping = {label.text: label.id for label in project.labels.all()}

18
backend/api/urls.py

@ -48,7 +48,7 @@ urlpatterns_project = [
name='example_list'
),
path(
route='examples/<int:example_id>',
route='examples/<uuid:example_id>',
view=views.ExampleDetail.as_view(),
name='example_detail'
),
@ -89,23 +89,23 @@ urlpatterns_project = [
name='doc_list'
),
path(
route='docs/<int:doc_id>',
route='docs/<uuid:doc_id>',
view=views.DocumentDetail.as_view(),
name='doc_detail'
),
path(
route='approval/<int:example_id>',
route='approval/<uuid:example_id>',
view=views.ApprovalAPI.as_view(),
name='approve_labels'
),
# Todo: change.
path(
route='docs/<int:doc_id>/annotations',
route='docs/<uuid:doc_id>/annotations',
view=views.AnnotationList.as_view(),
name='annotation_list'
),
path(
route='docs/<int:doc_id>/annotations/<int:annotation_id>',
route='docs/<uuid:doc_id>/annotations/<int:annotation_id>',
view=views.AnnotationDetail.as_view(),
name='annotation_detail'
),
@ -120,7 +120,7 @@ urlpatterns_project = [
name='tag_detail'
),
path(
route='examples/<int:example_id>/comments',
route='examples/<uuid:example_id>/comments',
view=views.CommentListDoc.as_view(),
name='comment_list_doc'
),
@ -130,12 +130,12 @@ urlpatterns_project = [
name='comment_list_project'
),
path(
route='examples/<int:example_id>/comments/<int:comment_id>',
route='examples/<uuid:example_id>/comments/<int:comment_id>',
view=views.CommentDetail.as_view(),
name='comment_detail'
),
path(
route='examples/<int:example_id>/states',
route='examples/<uuid:example_id>/states',
view=views.ExampleStateList.as_view(),
name='example_state_list'
),
@ -175,7 +175,7 @@ urlpatterns_project = [
name='auto_labeling_config_test'
),
path(
route='examples/<int:example_id>/auto-labeling',
route='examples/<uuid:example_id>/auto-labeling',
view=views.AutoLabelingAnnotation.as_view(),
name='auto_labeling_annotation'
),

6
backend/api/views/download/writer.py

@ -82,7 +82,7 @@ class CsvWriter(BaseWriter):
def create_line(self, record) -> Dict:
return {
'id': record.id,
'id': str(record.id),
'data': record.data,
'label': '#'.join(record.label),
**record.metadata
@ -120,7 +120,7 @@ class JSONWriter(BaseWriter):
def create_line(self, record) -> Dict:
return {
'id': record.id,
'id': str(record.id),
'data': record.data,
'label': record.label,
**record.metadata
@ -132,7 +132,7 @@ class JSONLWriter(LineWriter):
def create_line(self, record):
return json.dumps({
'id': record.id,
'id': str(record.id),
'data': record.data,
'label': record.label,
**record.metadata

Loading…
Cancel
Save