-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathstandalone-runner.py
More file actions
53 lines (36 loc) · 1.07 KB
/
standalone-runner.py
File metadata and controls
53 lines (36 loc) · 1.07 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
import asyncio
import os
from dataclasses import asdict
from pydantic import BaseModel
import uvicorn
from run_eval import run_config
from fastapi import FastAPI, HTTPException
app = FastAPI()
_serial_run = asyncio.Semaphore(1)
_runner_token = None
class RunRequest(BaseModel):
config: dict
token: str
@app.post("/run")
async def run(request: RunRequest) -> dict:
# only one submission can run at any given time
if request.token != _runner_token:
raise HTTPException(status_code=401, detail="Invalid token")
async with _serial_run:
return asdict(run_config(request.config))
async def run_server(port):
config = uvicorn.Config(
app,
host="0.0.0.0",
port=port,
log_level="info",
limit_concurrency=2,
)
server = uvicorn.Server(config)
# we need this as discord and fastapi both run on the same event loop
await server.serve()
def main():
with asyncio.Runner() as runner:
runner.run(run_server(port=int(os.environ.get("PORT") or 8000)))
if __name__ == "__main__":
main()