-
Notifications
You must be signed in to change notification settings - Fork 864
/
torchserve_server_app.py
173 lines (140 loc) · 5.15 KB
/
torchserve_server_app.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
import os
import requests
import streamlit as st
MODEL_NAME = "llamacpp"
# App title
st.set_page_config(page_title="🦙💬 Llama 2 TorchServe Serve")
def start_server():
os.system(
"torchserve --start --model-store model_store --ncs --disable-token-auth --enable-model-api"
)
st.session_state.started = True
st.session_state.stopped = False
st.session_state.registered = False
def stop_server():
os.system("torchserve --stop")
st.session_state.stopped = True
st.session_state.started = False
st.session_state.registered = False
def _register_model(url):
res = requests.post(url)
if res.status_code != 200:
server_state_container.error("Error registering model", icon="🚫")
st.session_state.started = True
return
st.session_state.registered = True
st.session_state.started = False
st.session_state.stopped = False
server_state_container.caption(res.text)
def register_model():
if not st.session_state.started:
server_state_container.caption("TorchServe is not running. Start it")
return
url = (
f"http://localhost:8081/models?model_name={MODEL_NAME}&url={MODEL_NAME}"
f".tar.gz&initial_workers=1&synchronous=true"
)
_register_model(url)
def get_status():
if st.session_state.registered:
url = f"http://localhost:8081/models/{MODEL_NAME}"
res = requests.get(url)
if res.status_code != 200:
model_state_container.error("Error getting model status", icon="🚫")
return
status = json.loads(res.text)[0]
model_state_container.write(status)
def scale_workers(workers):
if st.session_state.registered:
num_workers = st.session_state[workers]
url = (
f"http://localhost:8081/models/{MODEL_NAME}?min_worker="
f"{str(num_workers)}&synchronous=true"
)
res = requests.put(url)
server_state_container.caption(res.text)
def set_batch_size(batch_size):
if st.session_state.registered:
url = f"http://localhost:8081/models/{MODEL_NAME}/1.0"
res = requests.delete(url)
server_state_container.caption(res.text)
st.session_state.registered = False
batch_size = st.session_state[batch_size]
url = (
f"http://localhost:8081/models?model_name={MODEL_NAME}&url={MODEL_NAME}"
f".tar.gz&batch_size={str(batch_size)}&initial_workers={str(workers)}"
f"&synchronous=true&max_batch_delay={str(max_batch_delay)}"
)
_register_model(url)
def set_max_batch_delay(max_batch_delay):
if st.session_state.registered:
url = f"http://localhost:8081/models/{MODEL_NAME}/1.0"
res = requests.delete(url)
server_state_container.caption(res.text)
st.session_state.registered = False
max_batch_delay = st.session_state[max_batch_delay]
url = (
f"http://localhost:8081/models?model_name={MODEL_NAME}&url="
f"{MODEL_NAME}.tar.gz&batch_size={str(batch_size)}&initial_workers="
f"{str(workers)}&synchronous=true&max_batch_delay={str(max_batch_delay)}"
)
_register_model(url)
if "started" not in st.session_state:
st.session_state.started = False
if "stopped" not in st.session_state:
st.session_state.stopped = False
if "registered" not in st.session_state:
st.session_state.registered = False
with st.sidebar:
st.title("🦙💬 Llama 2 TorchServe Server ")
st.button("Start Server", on_click=start_server)
st.button("Stop Server", on_click=stop_server)
st.button("Register Llama2", on_click=register_model)
workers = st.sidebar.slider(
"Num Workers",
key="Num Workers",
min_value=1,
max_value=4,
value=1,
step=1,
on_change=scale_workers,
args=("Num Workers",),
)
batch_size = st.sidebar.select_slider(
"Batch Size",
key="Batch Size",
options=[2**j for j in range(0, 8)],
on_change=set_batch_size,
args=("Batch Size",),
)
max_batch_delay = st.sidebar.slider(
"Max Batch Delay",
key="Max Batch Delay",
min_value=100,
max_value=10000,
value=100,
step=100,
on_change=set_max_batch_delay,
args=("Max Batch Delay",),
)
if st.session_state.started:
st.success("Started TorchServe", icon="✅")
if st.session_state.stopped:
st.success("Stopped TorchServe", icon="✅")
if st.session_state.registered:
st.success("Registered model", icon="✅")
st.title("TorchServe Status")
server_state_container = st.container()
server_state_container.subheader("Server status:")
if st.session_state.started:
server_state_container.success("Started TorchServe", icon="✅")
if st.session_state.stopped:
server_state_container.success("Stopped TorchServe", icon="✅")
if st.session_state.registered:
server_state_container.success("Registered model", icon="✅")
model_state_container = st.container()
with model_state_container:
st.subheader("Model Status")
with model_state_container:
st.button("Model Status", on_click=get_status)