You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.7 KiB

  1. import abc
  2. from django.contrib.auth.models import User
  3. from django.db import models
  4. from polymorphic.models import PolymorphicModel
  5. DOCUMENT_CLASSIFICATION = 'DocumentClassification'
  6. SEQUENCE_LABELING = 'SequenceLabeling'
  7. SEQ2SEQ = 'Seq2seq'
  8. SPEECH2TEXT = 'Speech2text'
  9. IMAGE_CLASSIFICATION = 'ImageClassification'
  10. INTENT_DETECTION_AND_SLOT_FILLING = 'IntentDetectionAndSlotFilling'
  11. PROJECT_CHOICES = (
  12. (DOCUMENT_CLASSIFICATION, 'document classification'),
  13. (SEQUENCE_LABELING, 'sequence labeling'),
  14. (SEQ2SEQ, 'sequence to sequence'),
  15. (INTENT_DETECTION_AND_SLOT_FILLING, 'intent detection and slot filling'),
  16. (SPEECH2TEXT, 'speech to text'),
  17. (IMAGE_CLASSIFICATION, 'image classification')
  18. )
  19. class Project(PolymorphicModel):
  20. name = models.CharField(max_length=100)
  21. description = models.TextField(default='')
  22. guideline = models.TextField(default='', blank=True)
  23. created_at = models.DateTimeField(auto_now_add=True)
  24. updated_at = models.DateTimeField(auto_now=True)
  25. created_by = models.ForeignKey(
  26. User,
  27. on_delete=models.SET_NULL,
  28. null=True,
  29. )
  30. project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
  31. random_order = models.BooleanField(default=False)
  32. collaborative_annotation = models.BooleanField(default=False)
  33. single_class_classification = models.BooleanField(default=False)
  34. @property
  35. @abc.abstractmethod
  36. def is_text_project(self) -> bool:
  37. return False
  38. @property
  39. def can_define_label(self) -> bool:
  40. """Whether or not the project can define label(ignoring the type of label)"""
  41. return False
  42. @property
  43. def can_define_relation(self) -> bool:
  44. """Whether or not the project can define relation."""
  45. return False
  46. @property
  47. def can_define_category(self) -> bool:
  48. """Whether or not the project can define category."""
  49. return False
  50. @property
  51. def can_define_span(self) -> bool:
  52. """Whether or not the project can define span."""
  53. return False
  54. def __str__(self):
  55. return self.name
  56. class TextClassificationProject(Project):
  57. @property
  58. def is_text_project(self) -> bool:
  59. return True
  60. @property
  61. def can_define_label(self) -> bool:
  62. return True
  63. @property
  64. def can_define_category(self) -> bool:
  65. return True
  66. class SequenceLabelingProject(Project):
  67. allow_overlapping = models.BooleanField(default=False)
  68. grapheme_mode = models.BooleanField(default=False)
  69. @property
  70. def is_text_project(self) -> bool:
  71. return True
  72. @property
  73. def can_define_label(self) -> bool:
  74. return True
  75. @property
  76. def can_define_span(self) -> bool:
  77. return True
  78. class Seq2seqProject(Project):
  79. @property
  80. def is_text_project(self) -> bool:
  81. return True
  82. class IntentDetectionAndSlotFillingProject(Project):
  83. @property
  84. def is_text_project(self) -> bool:
  85. return True
  86. @property
  87. def can_define_label(self) -> bool:
  88. return True
  89. @property
  90. def can_define_category(self) -> bool:
  91. return True
  92. @property
  93. def can_define_span(self) -> bool:
  94. return True
  95. class Speech2textProject(Project):
  96. @property
  97. def is_text_project(self) -> bool:
  98. return False
  99. class ImageClassificationProject(Project):
  100. @property
  101. def is_text_project(self) -> bool:
  102. return False
  103. @property
  104. def can_define_label(self) -> bool:
  105. return True
  106. @property
  107. def can_define_category(self) -> bool:
  108. return True
  109. class Tag(models.Model):
  110. text = models.TextField()
  111. project = models.ForeignKey(
  112. to=Project,
  113. on_delete=models.CASCADE,
  114. related_name='tags'
  115. )
  116. def __str__(self):
  117. return self.text