From ec53e53eb72c5f65a19786246007410688c7c2fa Mon Sep 17 00:00:00 2001 From: birbbit Date: Sat, 3 Feb 2024 18:07:22 -0800 Subject: [PATCH] populate run.py --- OS/01/computer/run.py | 28 ++++++++++++++++++++++++++++ OS/01/start.sh | 0 2 files changed, 28 insertions(+) mode change 100644 => 100755 OS/01/start.sh diff --git a/OS/01/computer/run.py b/OS/01/computer/run.py index e69de29..2c7adaa 100644 --- a/OS/01/computer/run.py +++ b/OS/01/computer/run.py @@ -0,0 +1,28 @@ +""" +Exposes a SSE streaming server endpoint at /run, which recieves language and code, +and streams the output. +""" + +import json +from interpreter import interpreter +import uvicorn + +from fastapi import FastAPI +from fastapi.responses import StreamingResponse +from pydantic import BaseModel + +class Code(BaseModel): + language: str + code: str + +app = FastAPI() + +@app.post("/run") +async def run_code(code: Code): + def generator(): + for chunk in interpreter.computer.run(code.language, code.code, stream=True): + yield json.dumps(chunk) + return StreamingResponse(generator()) + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=9000) diff --git a/OS/01/start.sh b/OS/01/start.sh old mode 100644 new mode 100755