maitri01 commited on
Commit
a90c8af
·
verified ·
1 Parent(s): cbc96dd

Add submit.py (31-unlearning only)

Browse files
Files changed (1) hide show
  1. submit.py +73 -0
submit.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Submit a file to the hackathon server and print the result.
2
+
3
+ Usage:
4
+ python submit.py 31-unlearning submission.pt --key <your API key>
5
+
6
+ The API key can also come from the HACKATHON_API_KEY environment variable, and
7
+ the server address from HACKATHON_URL. Needs: pip install requests
8
+ """
9
+ import argparse
10
+ import os
11
+ import sys
12
+ from pathlib import Path
13
+
14
+ import requests
15
+
16
+ DEFAULT_URL = "http://34.147.229.55" # dry-run server; changes for the event
17
+
18
+ # File type the task expects.
19
+ TASKS = {
20
+ "31-unlearning": (".pt", ".pth"),
21
+ }
22
+
23
+
24
+ def main() -> int:
25
+ parser = argparse.ArgumentParser(description="Submit a file to the hackathon server.")
26
+ parser.add_argument("task_id", choices=sorted(TASKS))
27
+ parser.add_argument("file", type=Path)
28
+ parser.add_argument("--key", default=os.getenv("HACKATHON_API_KEY"), help="your team's API key")
29
+ parser.add_argument("--url", default=os.getenv("HACKATHON_URL", DEFAULT_URL), help="server address")
30
+ args = parser.parse_args()
31
+
32
+ if not args.key:
33
+ parser.error("no API key: pass --key or set HACKATHON_API_KEY")
34
+ if not args.file.is_file():
35
+ parser.error(f"file not found: {args.file}")
36
+ if args.file.suffix.lower() not in TASKS[args.task_id]:
37
+ parser.error(f"{args.task_id} expects a {' or '.join(TASKS[args.task_id])} file, got {args.file.name}")
38
+
39
+ print(f"Submitting {args.file.name} to {args.task_id}. Scoring can take a few minutes...")
40
+ with args.file.open("rb") as f:
41
+ try:
42
+ reply = requests.post(
43
+ f"{args.url.rstrip('/')}/submit/{args.task_id}",
44
+ headers={"X-API-Key": args.key},
45
+ files={"file": (args.file.name, f)},
46
+ timeout=(10, 600), # the server waits up to 500 s for the score
47
+ )
48
+ except requests.RequestException as exc:
49
+ print(f"Could not reach the server: {exc}", file=sys.stderr)
50
+ return 1
51
+
52
+ try:
53
+ body = reply.json()
54
+ except ValueError:
55
+ print(f"HTTP {reply.status_code}: {reply.text[:300]}", file=sys.stderr)
56
+ return 1
57
+
58
+ if reply.status_code != 200:
59
+ print(f"Rejected (HTTP {reply.status_code}): {body.get('detail', body)}", file=sys.stderr)
60
+ return 1
61
+ status = body.get("status")
62
+ if status == "success":
63
+ print(f"Score (public split): {body.get('score')}")
64
+ if "previous_best" in body:
65
+ print(f"Previous best: {body['previous_best']} (improved: {body.get('improved')})")
66
+ print(body.get("message", ""))
67
+ return 0
68
+ print(f"{status}: {body.get('message')}", file=sys.stderr)
69
+ return 1
70
+
71
+
72
+ if __name__ == "__main__":
73
+ sys.exit(main())