|
| 1 | +from enum import Enum |
| 2 | +from typing import TYPE_CHECKING, Optional |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +from alignerr.schema.project_rate import ProjectRateV2 |
| 7 | +from alignerr.schema.project_domain import ProjectDomain |
| 8 | +from alignerr.schema.enchanced_resource_tags import ( |
| 9 | + EnhancedResourceTag, |
| 10 | + ResourceTagType, |
| 11 | +) |
| 12 | +from alignerr.schema.project_boost_workforce import ( |
| 13 | + ProjectBoostWorkforce, |
| 14 | +) |
| 15 | +from labelbox.pagination import PaginatedCollection |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from labelbox import Client |
| 22 | + from labelbox.schema.project import Project |
| 23 | + from alignerr.schema.project_domain import ProjectDomain |
| 24 | + |
| 25 | + |
| 26 | +class AlignerrRole(Enum): |
| 27 | + Labeler = "LABELER" |
| 28 | + Reviewer = "REVIEWER" |
| 29 | + Admin = "ADMIN" |
| 30 | + ProjectCoordinator = "PROJECT_COORDINATOR" |
| 31 | + AlignerrLabeler = "ALIGNERR_LABELER" |
| 32 | + EndLabellingRole = "ENDLABELLINGROLE" |
| 33 | + |
| 34 | + |
| 35 | +class AlignerrProject: |
| 36 | + def __init__(self, client: "Client", project: "Project", _internal: bool = False): |
| 37 | + if not _internal: |
| 38 | + raise RuntimeError( |
| 39 | + "AlignerrProject cannot be initialized directly. " |
| 40 | + "Use AlignerrProjectBuilder or AlignerrProjectFactory to create instances." |
| 41 | + ) |
| 42 | + self.client = client |
| 43 | + self.project = project |
| 44 | + |
| 45 | + @property |
| 46 | + def project(self) -> "Project": |
| 47 | + return self._project |
| 48 | + |
| 49 | + @project.setter |
| 50 | + def project(self, project: "Project"): |
| 51 | + self._project = project |
| 52 | + |
| 53 | + def domains(self) -> PaginatedCollection: |
| 54 | + return ProjectDomain.get_by_project_id( |
| 55 | + client=self.client, project_id=self.project.uid |
| 56 | + ) |
| 57 | + |
| 58 | + def add_domain(self, project_domain: ProjectDomain): |
| 59 | + return ProjectDomain.connect_project_to_domains( |
| 60 | + client=self.client, |
| 61 | + project_id=self.project.uid, |
| 62 | + domain_ids=[project_domain.uid], |
| 63 | + ) |
| 64 | + |
| 65 | + def get_project_rates(self) -> list["ProjectRateV2"]: |
| 66 | + return ProjectRateV2.get_by_project_id( |
| 67 | + client=self.client, project_id=self.project.uid |
| 68 | + ) |
| 69 | + |
| 70 | + def set_project_rate(self, project_rate_input): |
| 71 | + return ProjectRateV2.set_project_rate( |
| 72 | + client=self.client, |
| 73 | + project_id=self.project.uid, |
| 74 | + project_rate_input=project_rate_input, |
| 75 | + ) |
| 76 | + |
| 77 | + def set_tags(self, tag_names: list[str], tag_type: ResourceTagType): |
| 78 | + # Convert tag names to tag IDs |
| 79 | + tag_ids = [] |
| 80 | + for tag_name in tag_names: |
| 81 | + # Search for the tag by text to get its ID |
| 82 | + found_tags = EnhancedResourceTag.search_by_text( |
| 83 | + self.client, search_text=tag_name, tag_type=tag_type |
| 84 | + ) |
| 85 | + if found_tags: |
| 86 | + tag_ids.append(found_tags[0].id) |
| 87 | + |
| 88 | + # Use the existing project resource tag functionality with IDs |
| 89 | + self.project.update_project_resource_tags(tag_ids) |
| 90 | + return self |
| 91 | + |
| 92 | + def get_tags(self) -> list[EnhancedResourceTag]: |
| 93 | + """Get enhanced resource tags associated with this project. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + List of EnhancedResourceTag instances |
| 97 | + """ |
| 98 | + # Get project resource tags and convert to EnhancedResourceTag instances |
| 99 | + project_resource_tags = self.project.get_resource_tags() |
| 100 | + enhanced_tags = [] |
| 101 | + for tag in project_resource_tags: |
| 102 | + # Search for the corresponding EnhancedResourceTag by text (try different types) |
| 103 | + found_tags = [] |
| 104 | + for tag_type in [ResourceTagType.Default, ResourceTagType.Billing]: |
| 105 | + found_tags = EnhancedResourceTag.search_by_text( |
| 106 | + self.client, search_text=tag.text, tag_type=tag_type |
| 107 | + ) |
| 108 | + if found_tags: |
| 109 | + break |
| 110 | + if found_tags: |
| 111 | + enhanced_tags.extend(found_tags) |
| 112 | + return enhanced_tags |
| 113 | + |
| 114 | + def add_tag(self, tag: EnhancedResourceTag): |
| 115 | + """Add a single enhanced resource tag to the project. |
| 116 | +
|
| 117 | + Args: |
| 118 | + tag: EnhancedResourceTag instance to add |
| 119 | +
|
| 120 | + Returns: |
| 121 | + Self for method chaining |
| 122 | + """ |
| 123 | + current_tags = self.get_tags() |
| 124 | + current_tag_names = [t.text for t in current_tags] |
| 125 | + |
| 126 | + if tag.text not in current_tag_names: |
| 127 | + current_tag_names.append(tag.text) |
| 128 | + self.set_tags(current_tag_names, tag.type) |
| 129 | + |
| 130 | + return self |
| 131 | + |
| 132 | + def remove_tag(self, tag: EnhancedResourceTag): |
| 133 | + """Remove a single enhanced resource tag from the project. |
| 134 | +
|
| 135 | + Args: |
| 136 | + tag: EnhancedResourceTag instance to remove |
| 137 | +
|
| 138 | + Returns: |
| 139 | + Self for method chaining |
| 140 | + """ |
| 141 | + current_tags = self.get_tags() |
| 142 | + current_tag_names = [t.text for t in current_tags if t.uid != tag.uid] |
| 143 | + self.set_tags(current_tag_names, tag.type) |
| 144 | + return self |
| 145 | + |
| 146 | + def get_project_owner(self) -> Optional[ProjectBoostWorkforce]: |
| 147 | + """Get the ProjectBoostWorkforce for this project. |
| 148 | +
|
| 149 | + Returns: |
| 150 | + ProjectBoostWorkforce instance or None if not found |
| 151 | + """ |
| 152 | + return ProjectBoostWorkforce.get_by_project_id( |
| 153 | + client=self.client, project_id=self.project.uid |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +class AlignerrWorkspace: |
| 158 | + def __init__(self, client: "Client"): |
| 159 | + self.client = client |
| 160 | + |
| 161 | + def project_builder(self): |
| 162 | + from alignerr.alignerr_project_builder import ( |
| 163 | + AlignerrProjectBuilder, |
| 164 | + ) |
| 165 | + |
| 166 | + return AlignerrProjectBuilder(self.client) |
| 167 | + |
| 168 | + def project_prototype(self): |
| 169 | + from alignerr.alignerr_project_factory import ( |
| 170 | + AlignerrProjectFactory, |
| 171 | + ) |
| 172 | + |
| 173 | + return AlignerrProjectFactory(self.client) |
| 174 | + |
| 175 | + @classmethod |
| 176 | + def from_labelbox(cls, client: "Client") -> "AlignerrWorkspace": |
| 177 | + return cls(client) |
0 commit comments