Skip to content

Commit ec62712

Browse files
committed
Update litserve example
1 parent 10f9e03 commit ec62712

File tree

5 files changed

+45
-88
lines changed

5 files changed

+45
-88
lines changed

deploy-model-with-fastapi/server.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
import pandas as pd
77
from fastapi import FastAPI
88

9+
10+
def _get_model_dir():
11+
if "MODEL_DIR" not in os.environ:
12+
raise Exception(
13+
"MODEL_DIR environment variable is not set. Please set it to the directory containing the model."
14+
)
15+
return os.environ["MODEL_DIR"]
16+
17+
918
model = None
10-
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
11-
MODEL_DIR = os.getenv("MODEL_DIR", THIS_DIR)
12-
MODEL_PATH = os.path.join(MODEL_DIR, "iris_classifier.joblib")
19+
MODEL_PATH = os.path.join(_get_model_dir(), "iris_classifier.joblib")
1320

1421

1522
def load_model():
@@ -33,7 +40,9 @@ async def health() -> Dict[str, bool]:
3340

3441

3542
@app.post("/predict")
36-
def predict(sepal_length: float, sepal_width: float, petal_length: float, petal_width: float):
43+
def predict(
44+
sepal_length: float, sepal_width: float, petal_length: float, petal_width: float
45+
):
3746
global model
3847
class_names = ["setosa", "versicolor", "virginica"]
3948
data = dict(

deploy-model-with-litserve/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
# Deploy Whisper Model with Litserve
22

3-
## Setup
3+
---
4+
5+
### Install requirements
46

57
```bash
68
pip install -r requirements.txt
79
```
810

9-
## Deploy
11+
### Start the server
1012

1113
```bash
12-
python deploy.py --workspace-fqn ... --host ... --port ...
14+
export MODEL_DIR="Systran/faster-whisper-tiny"
15+
python whisper_server.py
1316
```
1417

15-
## Test
18+
### Example inference call
1619

1720
```bash
18-
curl -X POST http://<endpoint>/predict -F "request=@./audio.mp3"
21+
curl -X POST http://0.0.0.0:8000/predict -F "request=@./audio.mp3"
1922
```
2023

2124
You should get the following response:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is auto-generated by LitServe.
2+
# Disable auto-generation by setting `generate_client_file=False` in `LitServer.run()`.
3+
4+
import requests
5+
6+
response = requests.post("http://127.0.0.1:8000/predict", json={"input": 4.0})
7+
print(f"Status: {response.status_code}\nResponse:\n {response.text}")

deploy-model-with-litserve/deploy.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

deploy-model-with-litserve/whisper_server.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import os
12
import litserve as ls
23
from fastapi import UploadFile
34
from faster_whisper import WhisperModel
45

6+
def _get_model_dir():
7+
if "MODEL_DIR" not in os.environ:
8+
raise Exception(
9+
"MODEL_DIR environment variable is not set. Please set it to the directory containing the model."
10+
)
11+
return os.environ["MODEL_DIR"]
12+
13+
MODEL_DIR = _get_model_dir()
514

615
class WhisperLitAPI(ls.LitAPI):
716
def setup(self, device):
817
# Load the OpenAI Whisper model. You can specify other models like "base", "small", etc.
9-
self.model = WhisperModel("tiny", device="cpu")
18+
self.model = WhisperModel(MODEL_DIR, device="cpu")
1019

1120
def decode_request(self, request: UploadFile):
1221
# Assuming the request sends the path to the audio file
@@ -28,5 +37,11 @@ def encode_response(self, output):
2837

2938
if __name__ == "__main__":
3039
api = WhisperLitAPI()
31-
server = ls.LitServer(api, fast_queue=True, accelerator="cpu", timeout=1000, workers_per_device=1)
40+
server = ls.LitServer(
41+
api,
42+
fast_queue=True,
43+
accelerator="cpu",
44+
timeout=1000,
45+
workers_per_device=1
46+
)
3247
server.run(port=8000)

0 commit comments

Comments
 (0)