-
Notifications
You must be signed in to change notification settings - Fork 0
/
darwinpyspark.py
264 lines (209 loc) · 8.35 KB
/
darwinpyspark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import json
import urllib.parse
import urllib.request
import zipfile
from io import BytesIO
import requests
from pyspark.sql import SparkSession
from pyspark.sql.functions import from_json
class DarwinPyspark:
def __init__(self, API_KEY, team_slug, dataset_slug):
"""
Method to initialise Darwin Pyspark
Parameters
----------
API_KEY (str): Your Darwin API Key
team_slug (str): The slug name of the team in Darwin you want to interact with
dataset_slug (str): The slug name of the dataset in Darwin you want to interact with
Returns
-------
DarwinPyspark class
"""
self.headers = {
"accept": "application/json",
"Authorization": f"ApiKey {API_KEY}",
}
self.team_slug = team_slug.lower().strip().replace(" ", "-")
self.dataset_slug = dataset_slug.lower().strip().replace(" ", "-")
def upload_items(self, df):
"""
Method to upload a pyspark dataframes data to V7
Parameters
----------
df (pyspark dataframe): A dataframe, with columns 'object_url' (accessible open or presigned url for the image) and 'file_name' (the name you want the file to be listed as in V7)
Returns
-------
None
"""
df.select("file_name", "object_url").foreach(
lambda row: self._upload_item(row[0], row[1])
)
def download_export(self, export_name):
"""
Calls all download methods to get and write an export to a pyspark dataframe
Parameters
----------
export_name (str): Name of the export in V7 that is to be downloaded
Returns
-------
export_df (pyspark dataframe): pyspark dataframe of the exported darwin json data
"""
export_url = self._get_export_url(export_name)
# create a SparkSession object
spark = SparkSession.builder.appName("darwinpyspark").getOrCreate()
return self._extract_export(self._download_export_zip(export_url), spark)
def _data_registration(self, item_name):
"""
Method to register items and slots
Parameters
----------
item_name (str): Name of the file to be uploaded
Returns
-------
upload_id (str): the upload id for the file to be uploaded
"""
url = f"https://darwin.v7labs.com/api/v2/teams/{self.team_slug}/items/register_upload"
payload = {
"items": [
{
"slots": [{"tags": [], "file_name": item_name, "slot_name": "0"}],
"name": item_name,
"layout": None,
"path": "",
"tags": [],
}
],
"dataset_slug": self.dataset_slug,
}
response = requests.post(url, headers=self.headers, json=payload)
json_response = response.json()
if json_response["blocked_items"]:
raise RuntimeError(f"{json_response}")
return json_response["items"][0]["slots"][0]["upload_id"]
def _sign_upload(self, upload_id):
"""
Method to sign upload for an item
Parameters
----------
upload_id (str): Upload id generated by data_registration() for file to be uploaded
Returns
-------
upload_url (str): the upload url for the file
"""
url = f"https://darwin.v7labs.com/api/v2/teams/{self.team_slug}/items/uploads/{upload_id}/sign"
response = requests.get(url, headers=self.headers)
return response.json()["upload_url"]
def _upload_binary(self, item_path, upload_url):
"""
Method to upload item data to the V7 platform
Parameters
----------
item_path (str): Accessible open or presigned url for the image
upload_url (str): The upload url for the file
Returns
-------
None
Rasies
--------
RuntimeError if upload failed
"""
encoded_url = urllib.parse.quote(item_path, safe=":/")
with urllib.request.urlopen(encoded_url) as response:
data = response.read()
response = requests.put(
url=upload_url,
data=data,
headers={"Content-Type": "application/octet-stream"},
)
if not response.ok:
raise RuntimeError(f"Issue uploading {item_path} data to V7")
def _confirm(self, upload_id):
"""
Method to confirm an upload for a particular upload_id
Parameters
----------
upload_id (str): The generated id for the file to be loaded
Returns
-------
response (str): the response from the request to upload the binary image data to V7
"""
url = f"https://darwin.v7labs.com/api/v2/teams/{self.team_slug}/items/uploads/{upload_id}/confirm"
return requests.post(url, headers=self.headers)
def _upload_item(self, item_name, item_path):
"""
Method to call all upload methods and upload a specific item to V7
Parameters
----------
item_name (str): Name of the file to be uploaded
item_path (str): Accessible open or presigned url for the image
Returns
-------
None
"""
upload_id = self._data_registration(item_name)
if upload_id == None:
return
upload_url = self._sign_upload(upload_id)
self._upload_binary(item_path, upload_url)
self._confirm(upload_id)
def _get_export_url(self, export_name):
"""
Method to get the url for the export to be downloaded
Parameters
----------
export_name (str): Name of the export in V7 that is to be downloaded
Returns
-------
download_url (str): The url for the generated export that is to be downloaded
"""
url = f"https://darwin.v7labs.com/api/v2/teams/{self.team_slug}/datasets/{self.dataset_slug}/exports"
response = requests.get(url, headers=self.headers)
if not response.ok:
raise RuntimeError(f"Failed to fetch export '{export_name}': {response.status_code} - {response.content}")
exports_json = response.json()
# get the export zip url
for export_json in exports_json:
if export_json["name"] == export_name:
return export_json["download_url"]
raise RuntimeError(f"No export with name '{export_name}' found")
def _download_export_zip(self, download_url):
"""
From the export url, method to download the relevant darwin json export
Parameters
----------
download_url (str): The url for the generated export that is to be downloaded
Returns
-------
(zipfile): Zipped set of darwin export JSON's
"""
# download the zip file from the URL
response = urllib.request.urlopen(download_url)
return zipfile.ZipFile(BytesIO(response.read()))
def _extract_export(self, zipfile, spark):
"""
Method to write the darwin json results to a pyspark dataframe
Parameters
----------
zipfile (Zipfile): Zipped set of darwin export JSON's
spark (DarwinPyspark Spark Session): The spark session created to write the results to a pyspark table
Returns
-------
df (pyspark dataframe): pyspark dataframe of the exported darwin json data
"""
# Set Databricks user agent tag
spark.conf.set("spark.databricks.agent.id", "darwinpyspark")
# extract the JSON files and read them into a DataFrame
json_files = []
for filename in zipfile.namelist():
if filename.endswith(".json"):
data = zipfile.read(filename)
json_files.append(data.decode("utf-8"))
# Define the schema for the JSON data
schema = "struct<"
for key in json.loads(json_files[0]).keys():
schema += f"`{key}` string,"
schema = schema[:-1] + ">"
# Create a DataFrame from the JSON data and parse the JSON strings
df = spark.createDataFrame(json_files, "string")
df = df.select(from_json(df.value, schema).alias("data")).select("data.*")
return df