-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
70 lines (55 loc) · 2.05 KB
/
Copy pathserver.py
File metadata and controls
70 lines (55 loc) · 2.05 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Do not edit if deploying to Banana Serverless
# This file is boilerplate for the http server, and follows a strict interface.
# Instead, edit the init() and inference() functions in app.py
from sanic import Sanic, response
from sanic_ext import Extend
import subprocess
import app as user_src
import traceback
import os
import json
# We do the model load-to-GPU step on server startup
# so the model object is available globally for reuse
user_src.init()
# Create the http server app
server = Sanic("my_app")
server.config.CORS_ORIGINS = os.getenv("CORS_ORIGINS") or "*"
Extend(server)
# Healthchecks verify that the environment is correct on Banana Serverless
@server.route("/healthcheck", methods=["GET"])
def healthcheck(request):
# dependency free way to check if GPU is visible
gpu = False
out = subprocess.run("nvidia-smi", shell=True)
if out.returncode == 0: # success state on shell command
gpu = True
return response.json({"state": "healthy", "gpu": gpu})
# Inference POST handler at '/' is called for every http call from Banana
@server.route("/", methods=["POST"])
async def inference(request):
try:
all_inputs = response.json.loads(request.json)
except:
all_inputs = request.json
call_inputs = all_inputs.get("callInputs", None)
stream_events = call_inputs and call_inputs.get("streamEvents", 0) != 0
streaming_response = None
if stream_events:
streaming_response = await request.respond(content_type="application/x-ndjson")
try:
output = await user_src.inference(all_inputs, streaming_response)
except Exception as err:
output = {
"$error": {
"code": "APP_INFERENCE_ERROR",
"name": type(err).__name__,
"message": str(err),
"stack": traceback.format_exc(),
}
}
if stream_events:
await streaming_response.send(json.dumps(output) + "\n")
else:
return response.json(output)
if __name__ == "__main__":
server.run(host="0.0.0.0", port="8000", workers=1)