Skip to content

Commit

Permalink
faster api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Adly committed Nov 29, 2024
1 parent e4b1070 commit dc5a6ef
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions agentrun-api/src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
from contextlib import asynccontextmanager

from agentrun import AgentRun

# Create a global runner variable
runner = None


@asynccontextmanager
async def lifespan(app: FastAPI):
# Initialize the runner when FastAPI starts
global runner
runner = AgentRun(
container_name=os.environ.get("CONTAINER_NAME", "agentrun-api-python-runner-1"),
cached_dependencies=["requests", "yfinance"],
default_timeout=60 * 5,
)
yield


class CodeSchema(BaseModel):
code: str
Expand All @@ -18,7 +34,7 @@ class OutputSchema(BaseModel):
output: str


app = FastAPI()
app = FastAPI(lifespan=lifespan)

# allow all origins
app.add_middleware(
Expand All @@ -44,11 +60,6 @@ async def redirect_docs():

@app.post("/v1/run/", response_model=OutputSchema)
async def run_code(code_schema: CodeSchema):
runner = AgentRun(
container_name=os.environ.get("CONTAINER_NAME", "agentrun-api-python-runner-1"),
cached_dependencies=["requests", "yfinance"],
default_timeout=60 * 5,
)
python_code = code_schema.code
with ThreadPoolExecutor() as executor:
future = executor.submit(runner.execute_code_in_container, python_code)
Expand Down

0 comments on commit dc5a6ef

Please sign in to comment.