Skip to content

Commit

Permalink
Feature: restore client.py file (#28)
Browse files Browse the repository at this point in the history
* restore client

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add client test

* remove mdformat

* Update README.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test run

* assert generated

* run server from cmd

* reformat comment

* remove generate client test

* increase sleep

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Aniket Maurya <[email protected]>
  • Loading branch information
3 people authored Apr 11, 2024
1 parent 2a47331 commit 01aabe1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/litserve/python_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import requests

response = requests.post("http://127.0.0.1:8000/predict", json={"input": 4.0})
print(f"Status: {response.status_code}\nResponse:\n {response.text}")
22 changes: 22 additions & 0 deletions tests/simple_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from litserve.server import LitServer
from litserve.api import LitAPI
from fastapi import Request, Response


class SimpleLitAPI(LitAPI):
def setup(self, device):
self.model = lambda x: x**2

def decode_request(self, request: Request):
return request["input"]

def predict(self, x):
return self.model(x)

def encode_response(self, output) -> Response:
return {"output": output}


if __name__ == "__main__":
server = LitServer(SimpleLitAPI(), accelerator="cpu", devices=1, timeout=10)
server.run()
21 changes: 21 additions & 0 deletions tests/test_lit_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import subprocess
import time
from multiprocessing import Pipe, Manager


import os

from unittest.mock import patch, MagicMock
from litserve.server import inference_worker, run_single_loop
from litserve.server import LitServer
Expand Down Expand Up @@ -84,3 +90,18 @@ def test_single_loop(simple_litapi, loop_args):

with pytest.raises(StopIteration, match="exit loop"):
run_single_loop(lit_api_mock, requests_queue, request_buffer)


def test_run():
subprocess.Popen(
["python", "tests/simple_server.py"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL,
)

time.sleep(5)
assert os.path.exists("client.py")
output = subprocess.run("python client.py", shell=True, capture_output=True, text=True).stdout
assert '{"output":16.0}' in output
os.remove("client.py")

0 comments on commit 01aabe1

Please sign in to comment.