-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
37 lines (28 loc) · 1.04 KB
/
service.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
from __future__ import annotations
import typing as t
import numpy as np
import bentoml
SAMPLE_SENTENCES = [
"The sun dips below the horizon, painting the sky orange.",
"A gentle breeze whispers through the autumn leaves.",
"The moon casts a silver glow on the tranquil lake.",
"A solitary lighthouse stands guard on the rocky shore.",
]
MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
@bentoml.service(
traffic={"timeout": 60},
resources={"gpu": 1, "gpu_type": "nvidia-t4"},
)
class SentenceTransformers:
def __init__(self) -> None:
import torch
from sentence_transformers import SentenceTransformer, models
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.model = SentenceTransformer(MODEL_ID, device=self.device)
print(f"Model '{MODEL_ID}' loaded on device: '{self.device}'.")
@bentoml.api(batchable=True)
def encode(
self,
sentences: t.List[str] = SAMPLE_SENTENCES,
) -> np.ndarray:
return self.model.encode(sentences)