Skip to content

Commit 8338085

Browse files
authored
[BugFix] Remote evaluation script starter example (#68)
* Fix Remote Eval Script * Add changes from review * Add changes from review
1 parent 7131a12 commit 8338085

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

remote_challenge_evaluation/eval_ai_interface.py

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
URLS = {
88
"get_message_from_sqs_queue": "/api/jobs/challenge/queues/{}/",
99
"get_submission_by_pk": "/api/jobs/submission/{}",
10+
"get_challenge_phase_by_pk": "/api/challenges/challenge/phase/{}",
1011
"delete_message_from_sqs_queue": "/api/jobs/queues/{}/",
1112
"update_submission": "/api/jobs/challenge/{}/update_submission/",
1213
}
@@ -139,3 +140,9 @@ def get_submission_by_pk(self, submission_pk):
139140
url = self.return_url_per_environment(url)
140141
response = self.make_request(url, "GET")
141142
return response
143+
144+
def get_challenge_phase_by_pk(self, phase_pk):
145+
url = URLS.get("get_challenge_phase_by_pk").format(phase_pk)
146+
url = self.return_url_per_environment(url)
147+
response = self.make_request(url, "GET")
148+
return response
+20-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import random
21

32

43
def evaluate(user_submission_file, phase_codename, test_annotation_file=None, **kwargs):
@@ -11,7 +10,11 @@ def evaluate(user_submission_file, phase_codename, test_annotation_file=None, **
1110
1211
`test_annotations_file`: Path to test_annotation_file on the server
1312
We recommend setting a default `test_annotation_file` or using `phase_codename`
14-
to select the appropriate file.
13+
to select the appropriate file. For example, you could load test annotation file
14+
for current phase as:
15+
```
16+
test_annotation_file = json.loads(open("{phase_codename}_path", "r"))
17+
```
1518
`**kwargs`: keyword arguments that contains additional submission
1619
metadata that challenge hosts can use to send slack notification.
1720
You can access the submission metadata
@@ -39,43 +42,35 @@ def evaluate(user_submission_file, phase_codename, test_annotation_file=None, **
3942
'submitted_at': u'2017-03-20T19:22:03.880652Z'
4043
}
4144
"""
45+
46+
'''
47+
# Load test annotation file for current phase
48+
test_annotation_file = json.loads(open("{phase_codename}_path", "r"))
49+
'''
4250
output = {}
4351
if phase_codename == "dev":
4452
print("Evaluating for Dev Phase")
4553
output["result"] = [
4654
{
47-
"train_split": {
48-
"Metric1": random.randint(0, 99),
49-
"Metric2": random.randint(0, 99),
50-
"Metric3": random.randint(0, 99),
51-
"Total": random.randint(0, 99),
52-
}
53-
}
55+
"split": "train_split",
56+
"show_to_participant": True,
57+
"accuracies": {"Metric1": 90},
58+
},
5459
]
55-
# To display the results in the result file
56-
output["submission_result"] = output["result"][0]["train_split"]
5760
print("Completed evaluation for Dev Phase")
5861
elif phase_codename == "test":
5962
print("Evaluating for Test Phase")
6063
output["result"] = [
6164
{
62-
"train_split": {
63-
"Metric1": random.randint(0, 99),
64-
"Metric2": random.randint(0, 99),
65-
"Metric3": random.randint(0, 99),
66-
"Total": random.randint(0, 99),
67-
}
65+
"split": "train_split",
66+
"show_to_participant": True,
67+
"accuracies": {"Metric1": 90},
6868
},
6969
{
70-
"test_split": {
71-
"Metric1": random.randint(0, 99),
72-
"Metric2": random.randint(0, 99),
73-
"Metric3": random.randint(0, 99),
74-
"Total": random.randint(0, 99),
75-
}
70+
"split": "test_split",
71+
"show_to_participant": False,
72+
"accuracies": {"Metric1": 50, "Metric2": 40},
7673
},
7774
]
78-
# To display the results in the result file
79-
output["submission_result"] = output["result"][0]
8075
print("Completed evaluation for Test Phase")
8176
return output

remote_challenge_evaluation/main.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import json
12
import os
23
import time
34

45
import requests
6+
57
from eval_ai_interface import EvalAI_Interface
68
from evaluate import evaluate
79

@@ -15,17 +17,18 @@
1517

1618

1719
def download(submission, save_dir):
18-
response = requests.get(submission.input_file.url)
19-
submission_file_path = os.path.join(save_dir, submission.input_file.name)
20+
response = requests.get(submission["input_file"])
21+
submission_file_path = os.path.join(
22+
save_dir, submission["input_file"].split("/")[-1]
23+
)
2024
with open(submission_file_path, "wb") as f:
2125
f.write(response.content)
2226
return submission_file_path
2327

2428

25-
def update_running(evalai, submission, job_name):
29+
def update_running(evalai, submission_pk):
2630
status_data = {
27-
"submission": submission,
28-
"job_name": job_name,
31+
"submission": submission_pk,
2932
"submission_status": "RUNNING",
3033
}
3134
update_status = evalai.update_submission_status(status_data)
@@ -79,7 +82,7 @@ def update_finished(
7982
phase_pk = message_body.get("phase_pk")
8083
# Get submission details -- This will contain the input file URL
8184
submission = evalai.get_submission_by_pk(submission_pk)
82-
85+
challenge_phase = evalai.get_challenge_phase_by_pk(phase_pk)
8386
if (
8487
submission.get("status") == "finished"
8588
or submission.get("status") == "failed"
@@ -89,15 +92,17 @@ def update_finished(
8992
evalai.delete_message_from_sqs_queue(message_receipt_handle)
9093

9194
else:
92-
update_running(submission, job_name="")
95+
if submission.get("status") == "submitted":
96+
update_running(evalai, submission_pk)
9397
submission_file_path = download(submission, save_dir)
9498
try:
9599
results = evaluate(
96-
submission_file_path,
97-
submission.challenge_phase.codename
100+
submission_file_path, challenge_phase["codename"]
101+
)
102+
update_finished(
103+
evalai, phase_pk, submission_pk, json.dumps(results["result"])
98104
)
99-
update_finished(phase_pk, submission_pk, results)
100105
except Exception as e:
101-
update_failed(phase_pk, submission_pk, str(e))
106+
update_failed(evalai, phase_pk, submission_pk, str(e))
102107
# Poll challenge queue for new submissions
103108
time.sleep(60)

0 commit comments

Comments
 (0)