-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge.py
193 lines (155 loc) · 5.39 KB
/
judge.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
from dataclasses import dataclass
import json
from fastapi.responses import PlainTextResponse, StreamingResponse
import modal
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
from fastapi.middleware.cors import CORSMiddleware
web_app = FastAPI()
web_app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app = modal.App(
"usaco-judge",
image=modal.Image.debian_slim().pip_install(
"fastapi[standard]", "pydantic", "requests"
),
volumes={"/root/data_private": modal.Volume.from_name("usaco-problems")},
)
COMPILE_URL = (
"https://v3nuswv3poqzw6giv37wmrt6su0krxvt.lambda-url.us-east-1.on.aws/compile"
)
EXECUTE_URL = (
"https://v3nuswv3poqzw6giv37wmrt6su0krxvt.lambda-url.us-east-1.on.aws/execute"
)
def get_usaco_problems():
with open("data_private/usaco/problems.json", "r") as f:
return json.load(f)
def get_usaco_to_probgate_mapping():
with open("data_private/probgate/usaco_to_probgate_mapping.json", "r") as f:
return json.load(f)
def get_probgate_problem(problem_id: str):
with open(f"data_private/probgate/problems/{problem_id}/config.json", "r") as f:
return json.load(f)
@dataclass
class JudgeOneParams:
executable: dict
timeout_ms: int
file_io_name: str
input_file_path: str
output_file_path: str
result_attrs: dict
@app.function(region="us-east")
def judge_one(
params: JudgeOneParams,
):
with open(params.input_file_path, "r") as f:
input_data = f.read()
with open(params.output_file_path, "r") as f:
output_data = f.read()
response = requests.post(
EXECUTE_URL,
json={
"executable": params.executable,
"options": {
"stdin": input_data,
"timeout_ms": params.timeout_ms,
"file_io_name": params.file_io_name,
},
},
headers={"Content-Type": "application/json"},
)
try:
result = response.json()
except requests.JSONDecodeError:
result = {"internal_error": response.text}
if "internal_error" not in result:
# Naive grader: Check for identical output
if result["verdict"] == "accepted":
output = result["file_output"] or result["stdout"]
if output.strip() != output_data.strip():
result["verdict"] = "wrong_answer"
result = {
**result,
**params.result_attrs,
}
return f"event: execute\ndata: {json.dumps(result)}\n\n"
def compile(source_code: str, compiler_options: str, language: str):
response = requests.post(
COMPILE_URL,
json={
"source_code": source_code,
"compiler_options": compiler_options,
"language": language,
},
headers={"Content-Type": "application/json"},
)
try:
return response.json()
except requests.JSONDecodeError:
return {"internal_error": response.text}
class JudgeRequest(BaseModel):
problem_id: str
source_code: str
compiler_options: str
language: str
@web_app.post("/judge")
def judge(request: JudgeRequest):
problems = get_usaco_problems()
problem_id = request.problem_id
if problem_id not in problems:
raise HTTPException(status_code=404, detail="Problem not found")
usaco_to_probgate_mapping = get_usaco_to_probgate_mapping()
if problem_id not in usaco_to_probgate_mapping:
raise HTTPException(
status_code=404, detail="We don't have test data for this problem yet."
)
probgate_problem_id = usaco_to_probgate_mapping[problem_id]
probgate_problem = get_probgate_problem(probgate_problem_id)
def _judge():
compile_result = compile(
request.source_code, request.compiler_options, request.language
)
if "compile_output" in compile_result:
yield f"event: compile\ndata: {json.dumps(compile_result['compile_output'])}\n\n"
else:
yield f"event: compile\ndata: {json.dumps(compile_result)}\n\n"
if "executable" not in compile_result or compile_result["executable"] is None:
return
yield from judge_one.map(
(
JudgeOneParams(
executable=compile_result["executable"],
timeout_ms=probgate_problem["time_limit_ms"],
file_io_name=probgate_problem["shortname"],
input_file_path=f"data_private/probgate/problems/{probgate_problem_id}/{test_case['input']}",
output_file_path=f"data_private/probgate/problems/{probgate_problem_id}/{test_case['output']}",
result_attrs={
"test_case": i,
"total_test_cases": len(probgate_problem["tests"]),
},
)
for i, test_case in enumerate(probgate_problem["tests"])
),
order_outputs=False,
)
return StreamingResponse(
_judge(),
media_type="text/event-stream",
)
@web_app.get("/")
async def root():
return PlainTextResponse("Judge OK")
@web_app.get("/usaco-problems.json")
async def get_usaco_problems_route():
problems = get_usaco_problems()
return problems
@app.function(region="us-east")
@modal.asgi_app()
def fastapi_app():
return web_app