-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support server sent event (#333)
* init the sse_inference Signed-off-by: Keming <kemingy94@gmail.com> * impl Signed-off-by: Keming <kemingy94@gmail.com> * handle sse error Signed-off-by: Keming <kemingy94@gmail.com> * fix sse error event type Signed-off-by: Keming <kemingy94@gmail.com> * add sse metrics Signed-off-by: Keming <kemingy94@gmail.com> * pass the err msg to the client Signed-off-by: Keming <kemingy94@gmail.com> * add sse test Signed-off-by: Keming <kemingy94@gmail.com> * format rs import Signed-off-by: Keming <kemingy94@gmail.com> --------- Signed-off-by: Keming <kemingy94@gmail.com>
- Loading branch information
Showing
19 changed files
with
736 additions
and
317 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2023 MOSEC Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import httpx | ||
from httpx_sse import connect_sse | ||
|
||
with httpx.Client() as client: | ||
with connect_sse( | ||
client, "POST", "http://127.0.0.1:8000/sse_inference", json={"text": "mosec"} | ||
) as event_source: | ||
for sse in event_source.iter_sse(): | ||
print(f"Event({sse.event}): {sse.data}") | ||
|
||
# error handling | ||
with httpx.Client() as client: | ||
with connect_sse( | ||
client, "POST", "http://127.0.0.1:8000/sse_inference", json={"error": "mosec"} | ||
) as event_source: | ||
for sse in event_source.iter_sse(): | ||
print(f"Event({sse.event}): {sse.data}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2023 MOSEC Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from mosec import Server, ValidationError, Worker, get_logger | ||
|
||
logger = get_logger() | ||
|
||
|
||
class Preprocess(Worker): | ||
def forward(self, data): | ||
text = data.get("text") | ||
if text is None: | ||
raise ValidationError("text is required") | ||
return text | ||
|
||
|
||
class Inference(Worker): | ||
def forward(self, data): | ||
epoch = 5 | ||
for i in range(epoch): | ||
for j in range(len(data)): | ||
self.send_stream_event( | ||
f"inference: ({i + 1}/{epoch}) {data[j]}", index=j | ||
) | ||
|
||
# this return value will be ignored | ||
return data | ||
|
||
|
||
if __name__ == "__main__": | ||
server = Server() | ||
server.append_worker(Preprocess) | ||
server.append_worker(Inference, max_batch_size=2) | ||
server.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ pre-commit>=2.15.0 | |
msgpack>=1.0.5 | ||
numpy>=1.24 | ||
httpx==0.24.1 | ||
httpx-sse==0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
unstable_features = true | ||
reorder_imports = true | ||
format_strings = true | ||
imports_granularity = "Module" | ||
group_imports = "StdExternalCrate" | ||
reorder_impl_items = true |
Oops, something went wrong.