Skip to content

Commit 5e35e48

Browse files
authored
feat: Model cache (#6)
Closes #5
1 parent 9487223 commit 5e35e48

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,41 @@ The purpose of this docker image is to provide a convenient way to download mode
44

55
## Usage
66

7+
### Basic
8+
79
```bash
810
docker run -it --rm -v $(PWD):/models amikos/hf-model-downloader sentence-transformers/all-MiniLM-L6-v2
911
```
1012

11-
With HF Token:
13+
### With cache:
14+
15+
You can use the image to download models to a existing HF cache (`~/.cache/torch/sentence_transformers`) by setting `USE_CACHE=true` and mounting the cache directory.
16+
17+
```bash
18+
docker run -it --rm -e USE_CACHE=true -v ~/.cache/torch/sentence_transformers:/models amikos/hf-model-downloader sentence-transformers/all-MiniLM-L6-v2
19+
```
20+
21+
> Note: An alternative location for HF models is `~/.cache/huggingface/hub/`
22+
23+
> Note: You can use a different cache directory by mounting a different directory.
24+
25+
Using HF sentence-transformers with custom cache dir:
26+
27+
```py
28+
from sentence_transformers import SentenceTransformer
29+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", cache_folder="/models")
30+
```
31+
32+
### With HF Token:
33+
34+
Using HF token can be useful if your model is private.
1235

1336
```bash
1437
export HF_TOKEN="your_huggingface_token"
1538
docker run -it --rm -e HF_TOKEN="$HF_TOKEN" -v $(PWD):/models amikos/hf-model-downloader sentence-transformers/all-MiniLM-L6-v2
1639
```
1740

18-
Using in Docker Compose:
41+
### Using in Docker Compose:
1942

2043
```yaml
2144
version: '3.8'

docker-entrypoint.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ do
1010
token="--token ${HF_TOKEN}"
1111
echo "Found HF token, using it to download $model"
1212
fi
13+
target_dir_cmd="--local-dir /models/$sanitized_model"
14+
if [ -n "$USE_CACHE" ] && [ "$(echo "$USE_CACHE" | tr '[:upper:]' '[:lower:]')" = "true" ]; then
15+
target_dir_cmd="--cache-dir /models"
16+
fi
1317
# Use printf to safely pass arguments
14-
printf '%s\0' "--local-dir" "/models/$sanitized_model" "$model" | xargs -0 huggingface-cli download $token
18+
printf '%s\0' $target_dir_cmd "$model" | xargs -0 huggingface-cli download $token
1519
done

0 commit comments

Comments
 (0)