Add support for the ClearML HTTP router using Task.get_http_router()

This commit is contained in:
clearml
2024-12-07 17:22:48 +02:00
parent 749a80a70a
commit ba492dd65d
11 changed files with 881 additions and 3 deletions

View File

@@ -0,0 +1,42 @@
"""
Example on how to use the ClearML HTTP router.
For this example, you would first need a webserver to route the traffic to:
`simple_webserver.py` launches such a server. Running the script will start a
webserver, bound to localhost:8000.
Then, when running this example, it creates a router which binds to 0.0.0.0:9000.
A local route is then created, which will proxy all traffic from
`http://<PRIVATE_IP>:9000/example_source` to `http://localhost:8000/serve`.
Trafic can be intercepted both on request and response via callbacks. See
`request_callback` and `response_callback`.
By default, the route traffic is monitored and telemetry is sent to the ClearML
server. To disable this, pass `endpoint_telemetry=False` when creating the route
"""
import time
from clearml import Task
def request_callback(request, persistent_state):
persistent_state["last_request_time"] = time.time()
def response_callback(response, request, persistent_state):
print("Latency:", time.time() - persistent_state["last_request_time"])
if __name__ == "__main__":
task = Task.init(project_name="Router Example", task_name="Router Example")
router = task.get_http_router()
router.set_local_proxy_parameters(incoming_port=9000, default_target="http://localhost:8000")
router.create_local_route(
source="/example_source",
target="http://localhost:8000/serve", # route traffic to this address
request_callback=request_callback, # intercept requests
response_callback=response_callback, # intercept responses
endpoint_telemetry={"model": "MyModel"} # set this to False to disable telemetry
)
router.deploy(wait=True)
# run `curl http://localhost:9000/example_source/1`

View File

@@ -0,0 +1,2 @@
clearml
clearml[router]

View File

@@ -0,0 +1,44 @@
"""
A simple webserver, used as a tool to showcase the capabilities of
ClearML HTTP router. See `http_router.py` for more details.
"""
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uvicorn
app = FastAPI()
actions = {
"1": {"name": "Action 1", "description": "This is model action 1"},
"2": {"name": "Action 2", "description": "This is model action 2"},
"3": {"name": "Action 3", "description": "This is model action 3"},
}
class Item(BaseModel):
name: str
description: str
@app.get("/")
def read_root():
return {"message": "Welcome to the FastAPI application!"}
@app.get("/serve/{action}", response_model=Item)
def read_item(action: str):
if action in actions:
return actions[action]
else:
raise HTTPException(status_code=404, detail="Item not found")
if __name__ == "__main__":
uvicorn.run(
"simple_webserver:app",
host="127.0.0.1",
port=8000,
reload=True
)