21
21
from labelu .internal .application .response .pre_annotation import PreAnnotationResponse
22
22
from labelu .internal .application .response .attachment import AttachmentResponse
23
23
24
- def read_jsonl_file (attachment : TaskAttachment ) -> List [dict ]:
24
+ def read_pre_annotation_file (attachment : TaskAttachment ) -> List [dict ]:
25
25
if attachment is None :
26
26
return []
27
27
28
28
attachment_path = attachment .path
29
29
file_full_path = settings .MEDIA_ROOT .joinpath (attachment_path .lstrip ("/" ))
30
30
31
31
# check if the file exists
32
- if not file_full_path .exists () or not attachment .filename .endswith ('.jsonl' ):
32
+ if not file_full_path .exists () or ( not attachment .filename .endswith ('.jsonl' ) and not attachment . filename . endswith ( '.json' ) ):
33
33
return []
34
34
35
35
try :
36
- with open (file_full_path , "r" , encoding = "utf-8" ) as f :
37
- data = f .readlines ()
36
+ if attachment .filename .endswith ('.jsonl' ):
37
+ with open (file_full_path , "r" , encoding = "utf-8" ) as f :
38
+ data = f .readlines ()
39
+ return [json .loads (line ) for line in data ]
40
+ else :
41
+ with open (file_full_path , "r" , encoding = "utf-8" ) as f :
42
+ # parse result
43
+ parsed_data = json .load (f )
44
+
45
+ return [{** item , "result" : json .loads (item ["result" ])} for item in parsed_data ]
46
+
38
47
except FileNotFoundError :
39
48
raise LabelUException (status_code = 404 , code = ErrorCode .CODE_51001_TASK_ATTACHMENT_NOT_FOUND )
40
49
41
- parsed_data = [json .loads (line ) for line in data ]
42
- return parsed_data
43
-
44
50
async def create (
45
51
db : Session , task_id : int , cmd : List [CreatePreAnnotationCommand ], current_user : User
46
52
) -> CreatePreAnnotationResponse :
@@ -65,15 +71,15 @@ async def create(
65
71
status_code = status .HTTP_400_BAD_REQUEST ,
66
72
)
67
73
68
- jsonl_contents = read_jsonl_file (jsonl_file )
74
+ pre_annotation_contents = read_pre_annotation_file (jsonl_file )
69
75
70
- for jsonl_content in jsonl_contents :
76
+ for _item in pre_annotation_contents :
71
77
pre_annotations .append (
72
78
TaskPreAnnotation (
73
79
task_id = task_id ,
74
80
file_id = pre_annotation .file_id ,
75
- sample_name = jsonl_content .get ("sample_name" ),
76
- data = json .dumps (jsonl_content , ensure_ascii = False ),
81
+ sample_name = _item .get ("sample_name" ) if jsonl_file . filename . endswith ( ".jsonl" ) else _item . get ( "fileName " ),
82
+ data = json .dumps (_item , ensure_ascii = False ),
77
83
created_by = current_user .id ,
78
84
updated_by = current_user .id ,
79
85
)
@@ -138,20 +144,37 @@ async def list_pre_annotation_files(
138
144
sorting : Optional [str ],
139
145
current_user : User ,
140
146
) -> Tuple [List [TaskAttachment ], int ]:
141
- pre_annotations = crud_pre_annotation .list_by_task_id_and_owner_id (db = db , task_id = task_id , owner_id = current_user .id )
142
- file_ids = [pre_annotation .file_id for pre_annotation in pre_annotations ]
143
-
144
- attachments , total = crud_attachment .list_by (db = db , ids = file_ids , after = after , before = before , pageNo = pageNo , pageSize = pageSize , sorting = sorting )
145
-
146
- return [
147
- PreAnnotationFileResponse (
148
- id = attachment .id ,
149
- url = attachment .url ,
150
- filename = attachment .filename ,
151
- sample_names = [pre_annotation .sample_name for pre_annotation in pre_annotations if pre_annotation .file_id == attachment .id ]
147
+ try :
148
+ pre_annotations = crud_pre_annotation .list_by_task_id_and_owner_id (db = db , task_id = task_id , owner_id = current_user .id )
149
+ file_ids = [pre_annotation .file_id for pre_annotation in pre_annotations ]
150
+
151
+ attachments , total = crud_attachment .list_by (db = db , ids = file_ids , after = after , before = before , pageNo = pageNo , pageSize = pageSize , sorting = sorting )
152
+
153
+ _attachment_ids = [attachment .id for attachment in attachments ]
154
+ def get_sample_names ():
155
+ _names = []
156
+ for pre_annotation in pre_annotations :
157
+ if pre_annotation .file_id in _attachment_ids and pre_annotation .sample_name is not None :
158
+ _names .append (pre_annotation .sample_name )
159
+
160
+ return _names
161
+
162
+ return [
163
+ PreAnnotationFileResponse (
164
+ id = attachment .id ,
165
+ url = attachment .url ,
166
+ filename = attachment .filename ,
167
+ sample_names = get_sample_names (),
168
+ )
169
+ for attachment in attachments
170
+ ], total
171
+
172
+ except Exception as e :
173
+ logger .error ("list pre annotation files error: {}" , e )
174
+ raise LabelUException (
175
+ code = ErrorCode .CODE_51001_TASK_ATTACHMENT_NOT_FOUND ,
176
+ status_code = status .HTTP_404_NOT_FOUND ,
152
177
)
153
- for attachment in attachments
154
- ], total
155
178
156
179
157
180
async def get (
@@ -189,7 +212,7 @@ async def delete_pre_annotation_file(
189
212
db : Session , task_id : int , file_id : int , current_user : User
190
213
) -> CommonDataResp :
191
214
with db .begin ():
192
- pre_annotations = crud_pre_annotation .list_by_task_id_and_owner_id_and_sample_name (db = db , task_id = task_id , owner_id = current_user .id , sample_name = crud_attachment . get ( db , file_id ). filename )
215
+ pre_annotations = crud_pre_annotation .list_by_task_id_and_file_id (db = db , task_id = task_id , owner_id = current_user .id , file_id = file_id )
193
216
pre_annotation_ids = [pre_annotation .id for pre_annotation in pre_annotations ]
194
217
crud_pre_annotation .delete (db = db , pre_annotation_ids = pre_annotation_ids )
195
218
crud_attachment .delete (db = db , attachment_ids = [file_id ])
0 commit comments