Spaces:
Sleeping
Sleeping
blobba
commited on
Commit
•
6672b5e
0
Parent(s):
Duplicate from blobba/sentence-transformers-test-3
Browse files- .gitattributes +34 -0
- .gitignore +2 -0
- Dockerfile +20 -0
- README.md +9 -0
- justfile +8 -0
- main.py +45 -0
- requirements.txt +7 -0
- shell.nix +31 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
venv
|
2 |
+
__pycache__
|
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
RUN useradd -m -u 1000 user
|
10 |
+
|
11 |
+
USER user
|
12 |
+
|
13 |
+
ENV HOME=/home/user \
|
14 |
+
PATH=/home/user/.local/bin:$PATH
|
15 |
+
|
16 |
+
WORKDIR $HOME/app
|
17 |
+
|
18 |
+
COPY --chown=user . $HOME/app
|
19 |
+
|
20 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers=4"]
|
README.md
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Fastapi Dummy
|
3 |
+
emoji: 🐢
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: blue
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
duplicated_from: blobba/sentence-transformers-test-3
|
9 |
+
---
|
justfile
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
set fallback
|
2 |
+
|
3 |
+
run:
|
4 |
+
|
5 |
+
dev:
|
6 |
+
source venv/bin/activate
|
7 |
+
nix-shell
|
8 |
+
uvicorn src.main:app --reload
|
main.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Generic, List, Optional, TypeVar
|
2 |
+
from functools import partial
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
from fastapi import FastAPI
|
6 |
+
import numpy
|
7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
8 |
+
from fastapi.responses import ORJSONResponse
|
9 |
+
|
10 |
+
MODEL = SentenceTransformer("all-mpnet-base-v2")
|
11 |
+
|
12 |
+
def cache(func):
|
13 |
+
inner_cache = dict()
|
14 |
+
def inner(sentences: List[str]):
|
15 |
+
if len(sentences) == 0:
|
16 |
+
return []
|
17 |
+
not_in_cache = list(filter(lambda s: s not in inner_cache.keys(), sentences))
|
18 |
+
if len(not_in_cache) > 0:
|
19 |
+
processed_sentences = func(list(not_in_cache))
|
20 |
+
for sentence, embedding in zip(not_in_cache, processed_sentences):
|
21 |
+
inner_cache[sentence] = embedding
|
22 |
+
return [inner_cache[s] for s in sentences]
|
23 |
+
return inner
|
24 |
+
|
25 |
+
@cache
|
26 |
+
def _encode(sentences: List[str]):
|
27 |
+
embeddings = MODEL.encode(sentences, normalize_embeddings=True, batch_size=2, show_progress_bar=True)
|
28 |
+
array = [numpy.around(a, 3).tolist() for a in embeddings]
|
29 |
+
return array
|
30 |
+
|
31 |
+
class EmbedReq(BaseModel):
|
32 |
+
sentences: List[str]
|
33 |
+
|
34 |
+
app = FastAPI()
|
35 |
+
|
36 |
+
@app.post("/embed", response_class=ORJSONResponse)
|
37 |
+
def embed(embed: EmbedReq):
|
38 |
+
return _encode(embed.sentences)
|
39 |
+
|
40 |
+
app.add_middleware(
|
41 |
+
CORSMiddleware,
|
42 |
+
allow_origins=["*"],
|
43 |
+
allow_methods=["*"],
|
44 |
+
allow_headers=["*"],
|
45 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sentence-transformers==2.2
|
2 |
+
numpy==1.24
|
3 |
+
joblib==1.2
|
4 |
+
fastapi==0.89
|
5 |
+
uvicorn[standard]==0.20
|
6 |
+
huggingface-hub==0.10.1
|
7 |
+
orjson==3.8.9
|
shell.nix
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
with import <nixpkgs> {};
|
2 |
+
mkShell {
|
3 |
+
packages = [
|
4 |
+
rustup
|
5 |
+
cargo
|
6 |
+
|
7 |
+
openssl
|
8 |
+
cacert
|
9 |
+
libiconv
|
10 |
+
pkg-config
|
11 |
+
libunistring
|
12 |
+
openblas
|
13 |
+
gfortran
|
14 |
+
gcc
|
15 |
+
sentencepiece
|
16 |
+
python39Packages.sentencepiece
|
17 |
+
|
18 |
+
wget
|
19 |
+
|
20 |
+
xz
|
21 |
+
gzip
|
22 |
+
bzip2
|
23 |
+
curlFull
|
24 |
+
];
|
25 |
+
buildInputs = [] ++ lib.optionals stdenv.isDarwin [
|
26 |
+
darwin.apple_sdk.frameworks.IOKit
|
27 |
+
darwin.apple_sdk.frameworks.Security
|
28 |
+
darwin.apple_sdk.frameworks.CoreServices
|
29 |
+
darwin.apple_sdk.frameworks.CoreFoundation
|
30 |
+
] ;
|
31 |
+
}
|