Skip to content

Commit 6fa9187

Browse files
Compound File SDK Support (#48)
* wip: compound file helpers * wip: compound file upload * fix: fix chunk size (#49) * fix: better handler null url error cases * feat: add signer param to dataset object (#50) * Add compound file upload example Co-authored-by: Vitalii Bulyzhyn <vitalybulyzhin@gmail.com>
1 parent 0cd0b49 commit 6fa9187

File tree

3 files changed

+221
-8
lines changed

3 files changed

+221
-8
lines changed

sdk/diffgram/file/compound_file.py

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
from diffgram.core.core import Project
2+
from typing import List
3+
from diffgram.file.file import File
4+
from diffgram.job.job import Job
5+
6+
7+
class CompoundChildFile:
8+
path: str
9+
url: str
10+
blob_path: str
11+
file_name: str
12+
bucket_name: str
13+
child_file_type: str
14+
connection_id: int
15+
media_type: str
16+
job: Job
17+
job_id: int
18+
directory_id: int
19+
instance_list: list
20+
frame_packet_map: dict
21+
assume_new_instances_machine_made: bool
22+
convert_names_to_label_files: bool
23+
video_split_duration: int
24+
25+
def __init__(self,
26+
child_file_type: str,
27+
path: str = None,
28+
url: str = None,
29+
blob_path: str = None,
30+
file_name: str = None,
31+
bucket_name: str = None,
32+
connection_id: int = None,
33+
media_type: str = None,
34+
job: Job = None,
35+
job_id: int = None,
36+
directory_id: int = None,
37+
instance_list: list = None,
38+
frame_packet_map: dict = None,
39+
assume_new_instances_machine_made: bool = None,
40+
convert_names_to_label_files: bool = None,
41+
video_split_duration: int = None):
42+
self.child_file_type = child_file_type
43+
self.path = path
44+
self.url = url
45+
self.blob_path = blob_path
46+
self.file_name = file_name
47+
self.bucket_name = bucket_name
48+
self.connection_id = connection_id
49+
self.media_type = media_type
50+
self.job = job
51+
self.job_id = job_id
52+
self.directory_id = directory_id
53+
self.instance_list = instance_list
54+
self.frame_packet_map = frame_packet_map
55+
self.assume_new_instances_machine_made = assume_new_instances_machine_made
56+
self.convert_names_to_label_files = convert_names_to_label_files
57+
self.video_split_duration = video_split_duration
58+
59+
60+
class CompoundFile:
61+
project: Project
62+
parent_file_data: dict
63+
child_files_to_upload: List[CompoundChildFile]
64+
65+
def __init__(self, project: Project, name: str, directory_id: int):
66+
self.project = project
67+
self.name = name
68+
self.directory_id = directory_id
69+
self.child_files_to_upload = []
70+
71+
def remove_compound_file(self, child_file: CompoundChildFile) -> List[CompoundChildFile]:
72+
self.child_files_to_upload.remove(child_file)
73+
return self.child_files_to_upload
74+
75+
def __create_compound_parent_file(self):
76+
url = f'/api/v1/project/{self.project.project_string_id}/file/new-compound'
77+
data = {
78+
'name': self.name,
79+
'directory_id': self.directory_id
80+
}
81+
response = self.project.session.post(url = self.project.host + url,
82+
json = data)
83+
self.project.handle_errors(response)
84+
data = response.json()
85+
self.parent_file_data = data.get('file')
86+
print('self,', self.parent_file_data)
87+
return data.get('file')
88+
89+
def __create_child_file(self, child_file: CompoundChildFile):
90+
if child_file.child_file_type == 'from_local':
91+
return self.project.file.from_local(
92+
path = child_file.path,
93+
directory_id = self.directory_id,
94+
instance_list = child_file.instance_list,
95+
frame_packet_map = child_file.frame_packet_map,
96+
assume_new_instances_machine_made = child_file.assume_new_instances_machine_made,
97+
convert_names_to_label_files = child_file.convert_names_to_label_files,
98+
parent_file_id = self.parent_file_data.get('id')
99+
)
100+
elif child_file.child_file_type == 'from_url':
101+
return self.project.file.from_url(
102+
url = child_file.path,
103+
media_type = child_file.media_type,
104+
job_id = child_file.job_id,
105+
job = child_file.job,
106+
video_split_duration = child_file.video_split_duration,
107+
instance_list = child_file.instance_list,
108+
frame_packet_map = child_file.frame_packet_map,
109+
parent_file_id = self.parent_file_data.get('id')
110+
)
111+
elif child_file.child_file_type == 'from_blob_path':
112+
return self.project.file.from_blob_path(
113+
blob_path = child_file.blob_path,
114+
bucket_name = child_file.bucket_name,
115+
connection_id = child_file.connection_id,
116+
media_type = child_file.media_type,
117+
instance_list = child_file.instance_list,
118+
file_name = child_file.file_name,
119+
frame_packet_map = child_file.frame_packet_map,
120+
parent_file_id = self.parent_file_data.get('id')
121+
)
122+
123+
def add_child_from_local(self,
124+
path: str,
125+
instance_list: list = None,
126+
frame_packet_map: dict = None,
127+
assume_new_instances_machine_made: bool = True,
128+
convert_names_to_label_files: bool = True):
129+
new_child_file = CompoundChildFile(
130+
child_file_type = "from_local",
131+
path = path,
132+
directory_id = self.directory_id,
133+
instance_list = instance_list,
134+
frame_packet_map = frame_packet_map,
135+
assume_new_instances_machine_made = assume_new_instances_machine_made,
136+
convert_names_to_label_files = convert_names_to_label_files
137+
)
138+
self.child_files_to_upload.append(new_child_file)
139+
return new_child_file
140+
141+
def add_child_file_from_url(self,
142+
url: str,
143+
media_type: str = "image",
144+
job: Job = None,
145+
job_id: int = None,
146+
video_split_duration: int = None,
147+
instance_list: list = None,
148+
frame_packet_map: dict = None):
149+
new_child_file = CompoundChildFile(
150+
child_file_type = "from_url",
151+
url = url,
152+
media_type = media_type,
153+
job = job,
154+
directory_id = self.directory_id,
155+
job_id = job_id,
156+
video_split_duration = video_split_duration,
157+
instance_list = instance_list,
158+
frame_packet_map = frame_packet_map,
159+
)
160+
self.child_files_to_upload.append(new_child_file)
161+
return new_child_file
162+
163+
def add_child_from_blob_path(self,
164+
blob_path: str,
165+
bucket_name: str,
166+
connection_id: int,
167+
media_type: str = 'image',
168+
instance_list: list = None,
169+
file_name: str = None,
170+
frame_packet_map: dict = None
171+
):
172+
new_child_file = CompoundChildFile(
173+
child_file_type = "from_blob_path",
174+
blob_path = blob_path,
175+
bucket_name = bucket_name,
176+
connection_id = connection_id,
177+
directory_id = self.directory_id,
178+
media_type = media_type,
179+
instance_list = instance_list,
180+
file_name = file_name,
181+
frame_packet_map = frame_packet_map,
182+
)
183+
self.child_files_to_upload.append(new_child_file)
184+
return new_child_file
185+
186+
def upload(self):
187+
parent_file_data: dict = self.__create_compound_parent_file()
188+
for child_file in self.child_files_to_upload:
189+
self.__create_child_file(child_file)
190+
return parent_file_data

sdk/diffgram/file/file_constructor.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def from_local(
4343
instance_list: list = None,
4444
frame_packet_map: dict = None,
4545
assume_new_instances_machine_made: bool = True,
46-
convert_names_to_label_files: bool = True
46+
convert_names_to_label_files: bool = True,
47+
parent_file_id: int = None
4748
):
4849
"""
4950
Create a Project file from local path
@@ -75,9 +76,8 @@ def from_local(
7576
assume_new_instances_machine_made = assume_new_instances_machine_made,
7677
convert_names_to_label_files = convert_names_to_label_files
7778
)
78-
79+
json_payload['parent_file_id'] = parent_file_id
7980
files['json'] = (None, json.dumps(json_payload), 'application/json')
80-
8181
endpoint = "/api/walrus/v1/project/" + self.client.project_string_id \
8282
+ "/input/from_local"
8383

@@ -106,7 +106,8 @@ def __build_packet_payload(self,
106106
directory_id: int = None,
107107
bucket_name: str = None,
108108
file_name: str = None,
109-
blob_path: str = None):
109+
blob_path: str = None,
110+
parent_file_id: int = None):
110111
packet = {'media': {}}
111112
packet['media']['url'] = url
112113
packet['media']['type'] = media_type
@@ -115,6 +116,7 @@ def __build_packet_payload(self,
115116
packet['frame_packet_map'] = frame_packet_map
116117
packet['type'] = type
117118
packet['connection_id'] = connection_id
119+
packet['parent_file_id'] = parent_file_id
118120
packet['directory_id'] = directory_id
119121
packet['original_filename'] = file_name
120122
packet['bucket_name'] = bucket_name
@@ -137,7 +139,8 @@ def from_blob_path(self,
137139
media_type: str = 'image',
138140
instance_list: list = None,
139141
file_name: str = None,
140-
frame_packet_map: dict = None):
142+
frame_packet_map: dict = None,
143+
parent_file_id: int = None):
141144
"""
142145
Bind a blob path in the given connection ID into Diffgram
143146
:param blob_path:
@@ -163,7 +166,8 @@ def from_blob_path(self,
163166
connection_id = connection_id,
164167
file_name = name,
165168
directory_id = directory_id,
166-
type = "from_blob_path"
169+
type = "from_blob_path",
170+
parent_file_id=parent_file_id
167171
)
168172
self.from_packet(packet = packet)
169173
return True
@@ -176,7 +180,8 @@ def from_url(
176180
job_id: int = None,
177181
video_split_duration: int = None,
178182
instance_list: list = None, # for Images
179-
frame_packet_map: dict = None # for Video
183+
frame_packet_map: dict = None, # for Video
184+
parent_file_id: int = None
180185
):
181186
"""
182187
@@ -208,7 +213,8 @@ def from_url(
208213
job_id = job_id,
209214
video_split_duration = video_split_duration,
210215
instance_list = instance_list,
211-
frame_packet_map = frame_packet_map
216+
frame_packet_map = frame_packet_map,
217+
parent_file_id = parent_file_id
212218
)
213219
self.from_packet(packet = packet)
214220

sdk/samples/import_compound_file.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from diffgram import Project
2+
from diffgram.file.compound_file import CompoundFile
3+
4+
project = Project(host = "https://diffgram.com",
5+
project_string_id = "replace_with_project_string",
6+
client_id = "replace_with_client_id",
7+
client_secret = "replace_with_client_secret")
8+
9+
parent = CompoundFile(
10+
project=project,
11+
name='myFirstCompoundFile',
12+
directory_id=project.default_directory.id
13+
)
14+
15+
parent.add_child_from_local(path='path/to/your_file.jpg')
16+
parent.add_child_from_local(path='path/to/your_second_file.jpg')
17+
parent.upload()

0 commit comments

Comments
 (0)