-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.py
45 lines (34 loc) · 1.29 KB
/
main.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
import asyncio
import logging
import os
from InstructorEmbedding import INSTRUCTOR
from leapfrogai_sdk import (
Embedding,
EmbeddingRequest,
EmbeddingResponse,
GrpcContext,
serve,
)
logging.basicConfig(
level=os.getenv("LFAI_LOG_LEVEL", logging.INFO),
format="%(name)s: %(asctime)s | %(levelname)s | %(filename)s:%(lineno)s >>> %(message)s",
)
logger = logging.getLogger(__name__)
model_dir = os.environ.get("LFAI_MODEL_PATH", ".model")
model = INSTRUCTOR(model_dir)
class InstructorEmbedding:
async def CreateEmbedding(self, request: EmbeddingRequest, context: GrpcContext):
logger.info(
f"processing CreateEmbedding request: char-length: {len(str(request.inputs))} word-count: {len(str(request.inputs).split())}"
)
# Run the CPU-intensive encoding in a separate thread
embeddings = await asyncio.to_thread(
model.encode, sentences=request.inputs, show_progress_bar=True
)
embeddings = [Embedding(embedding=inner_list) for inner_list in embeddings]
logger.info(
f"finished processing CreateEmbedding request, created {len(embeddings)} embeddings"
)
return EmbeddingResponse(embeddings=embeddings)
if __name__ == "__main__":
asyncio.run(serve(InstructorEmbedding()))