-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdump.py
More file actions
executable file
·62 lines (51 loc) · 1.56 KB
/
dump.py
File metadata and controls
executable file
·62 lines (51 loc) · 1.56 KB
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
#!/bin/env python3
import requests
import argparse
import json
import os
import sys
parser = argparse.ArgumentParser(description="Dump the content of an idios collection")
parser.add_argument(
"model_endpoint",
help='The URL prefix to the collection endpoint (e.g. "https://api.idios.it/models/vit_b32")',
)
parser.add_argument("output_prefix", help="The prefix for the output file name")
parser.add_argument(
"batch_size",
type=int,
nargs="?",
default=1000,
help="The batch size for each request (default 1000) to avoid timeouts",
)
args = parser.parse_args()
output_dir = os.path.dirname(args.output_prefix)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
cursor = ""
batch_count = 1
while True:
if cursor:
print(f"Requesting entries after {cursor}")
else:
print(f"Requesting entries")
try:
response = requests.post(
f"{args.model_endpoint}/dump",
json={"limit": args.batch_size, "cursor": cursor},
)
response.raise_for_status()
entries = response.json()
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"Error: {e} decoding `{response.text}`")
sys.exit(1)
if not entries:
break
output_file = f"{args.output_prefix}_{str(batch_count).zfill(8)}.json"
print(f"Writing {len(entries)} entries to {output_file}")
with open(output_file, "w") as f:
json.dump(entries, f)
cursor = entries[-1]["url"]
batch_count += 1