Notebooks
H
Hugging Face
80 Getting Started With Embeddings

80 Getting Started With Embeddings

hf-bloghacktoberfestnotebooks

Getting Started With Embeddings: Notebook Companion

1. Embedding a dataset

[4]

The first time you generate the embeddings it may take a while (approximately 20 seconds) for the API to return them. We use the retry decorator (install with pip install retry) so that if on the first try output = query(dict(inputs = texts)) doesn't work, wait 10 seconds and try again three times. The reason this happens is because on the first request, the model needs to be downloaded and installed in the server, but subsequent calls are much faster.

[2]
[5]
[6]
[7]
[8]
[9]
         0         1         2         3         4         5         6    \
0  -0.023889  0.055259 -0.011655 -0.033414 -0.012261 -0.024873 -0.012663   
1  -0.012688  0.046874 -0.010502 -0.020384 -0.013361  0.042322  0.016628   
2   0.000494  0.119412  0.005229 -0.092734  0.007773 -0.005325  0.034506   
3  -0.029711  0.023298 -0.057041 -0.012183 -0.013710  0.029796  0.063739   
4  -0.025628  0.070389 -0.017380 -0.056567  0.028576  0.052823  0.067062   
5  -0.022656  0.021160  0.005105 -0.046494  0.009074  0.041495  0.054268   
6  -0.002911  0.060791 -0.009176 -0.006133  0.040492  0.036594  0.002054   
7  -0.080526  0.059888 -0.048847 -0.040176 -0.063342  0.041848  0.119045   
8  -0.034388  0.072501  0.014440 -0.036695  0.014019  0.063070  0.034683   
9  -0.005964  0.025044 -0.003182 -0.025243 -0.039823 -0.012772  0.044713   
10 -0.039008 -0.010610 -0.007383 -0.050190 -0.002518 -0.041641  0.026969   
11 -0.095983 -0.063012 -0.116906 -0.059075 -0.051323 -0.003439  0.018687   
12 -0.011629  0.059619  0.016509 -0.094747 -0.008346  0.070966  0.042429   

         7         8         9    ...       374       375       376       377  \
0   0.025346  0.018509 -0.083508  ... -0.161688 -0.046426  0.006004  0.005281   
1  -0.004099 -0.002607 -0.010188  ... -0.061594 -0.020717 -0.009082 -0.029260   
2  -0.051981 -0.006265 -0.006111  ... -0.108326 -0.049646 -0.073399 -0.029898   
3   0.001101 -0.045124 -0.040747  ... -0.117682  0.031924  0.000854  0.020200   
4  -0.052618 -0.054702 -0.116230  ... -0.118145  0.013343 -0.055188 -0.032723   
5  -0.024185 -0.013483 -0.075966  ... -0.100110  0.010750 -0.031469 -0.004822   
6  -0.031345  0.031806 -0.023495  ... -0.028763 -0.060458 -0.018598 -0.040189   
7   0.010652 -0.030095 -0.004561  ... -0.144566  0.020404  0.023088  0.005077   
8  -0.014531 -0.059862 -0.045383  ... -0.114763 -0.035894 -0.019877 -0.033375   
9   0.014535 -0.038213 -0.041149  ... -0.057621  0.021594  0.048983 -0.044541   
10 -0.014801 -0.014127 -0.061637  ... -0.098168 -0.031693 -0.052128  0.014774   
11  0.006544 -0.049057 -0.031649  ... -0.041085 -0.008593 -0.021544 -0.021112   
12 -0.041212 -0.038502 -0.099356  ... -0.135191  0.011535 -0.050499 -0.007376   

         378       379       380       381       382       383  
0  -0.003342  0.027754  0.020411  0.005778  0.034098 -0.006889  
1  -0.066253  0.065257  0.013229 -0.023103 -0.002785  0.010474  
2  -0.102734  0.062121  0.034606  0.016877 -0.023861  0.005264  
3  -0.020666 -0.005167  0.038370  0.003617  0.033993 -0.010255  
4   0.008436  0.019169  0.048212 -0.040412  0.083346  0.026855  
5   0.039657  0.026384  0.045514  0.059089 -0.017509  0.007166  
6  -0.031486 -0.018299  0.002286 -0.073420  0.016235 -0.000244  
7  -0.055645 -0.007675  0.050791 -0.005989  0.134562  0.034817  
8  -0.030168  0.039412  0.044993  0.000578 -0.025124  0.034191  
9  -0.030137  0.006779  0.054854  0.029937  0.070214  0.041565  
10 -0.091150  0.001324  0.053866 -0.083904  0.037684  0.002314  
11 -0.019502  0.050039 -0.029175  0.005498  0.152892  0.024720  
12  0.084258 -0.008294  0.034186 -0.028212 -0.001166  0.001067  

[13 rows x 384 columns]

2. Host embeddings for free on the Hugging Face Hub

[ ]
[ ]

        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/tokens .
        (Deprecated, will be removed in v0.3.0) To login with username and password instead, interrupt with Ctrl+C.
        
Token: 
Login successful
Your token has been saved to /root/.huggingface/token
Authenticated through git-credential store but this isn't the helper defined on your machine.
You might have to re-authenticate when pushing to the Hugging Face Hub. Run the following command in your terminal in case you want to set this credential helper as the default

git config --global credential.helper store
[ ]
git version 2.17.1
Error: unknown flag: --version

Sorry, no usage text found for "git-lfs"

You are about to create datasets/ITESM/embedded_faqs_medicare
Proceed? [Y/n] y

Your repo now lives at:
  https://huggingface.co/datasets/ITESM/embedded_faqs_medicare

You can clone it locally with the command below, and commit/push as usual.

  git clone https://huggingface.co/datasets/ITESM/embedded_faqs_medicare

[ ]
Error: Failed to call git rev-parse --git-dir --show-toplevel: "fatal: not a git repository (or any of the parent directories): .git\n"
Git LFS initialized.
[ ]
Cloning into 'embedded_faqs_medicare'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[ ]

Changing directory to our repo embedded_faqs_medicare.

[ ]
/content/embedded_faqs_medicare
[ ]
Tracking "embeddings.csv"
[ ]
[ ]
[main fc6fd98] First version of the embedded_faqs_medicare dataset
 2 files changed, 15 insertions(+)
 create mode 100644 embeddings.csv
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 47.28 KiB | 4.73 MiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To https://huggingface.co/datasets/ITESM/embedded_faqs_medicare
   68bbeb7..fc6fd98  main -> main

3. Get the most similar Frequently Asked Questions to a query

[10]
[11]
Using custom data configuration ITESM--embedded_faqs_medicare-cb978a6449c82d29
Downloading and preparing dataset csv/ITESM--embedded_faqs_medicare to /root/.cache/huggingface/datasets/ITESM___csv/ITESM--embedded_faqs_medicare-cb978a6449c82d29/0.0.0/51cce309a08df9c4d82ffd9363bbe090bf173197fc01a71b034e8594995a1a58...
Downloading data files:   0%|          | 0/1 [00:00<?, ?it/s]
Downloading data:   0%|          | 0.00/106k [00:00<?, ?B/s]
Extracting data files:   0%|          | 0/1 [00:00<?, ?it/s]
0 tables [00:00, ? tables/s]
Dataset csv downloaded and prepared to /root/.cache/huggingface/datasets/ITESM___csv/ITESM--embedded_faqs_medicare-cb978a6449c82d29/0.0.0/51cce309a08df9c4d82ffd9363bbe090bf173197fc01a71b034e8594995a1a58. Subsequent calls will reuse this data.
  0%|          | 0/1 [00:00<?, ?it/s]
[14]
[18]
The size of our embedded dataset is torch.Size([13, 384]) and of our embedded query is torch.Size([1, 384]).
[16]
[19]
/usr/local/lib/python3.7/dist-packages/huggingface_hub/snapshot_download.py:11: FutureWarning: snapshot_download.py has been made private and will no longer be available from version 0.11. Please use `from huggingface_hub import snapshot_download` to import the only public function in this module. Other members of the file may be changed without a deprecation notice.
  FutureWarning,
[20]
['How can I get help with my Medicare Part A and Part B premiums?',
, 'What is Medicare and who can get it?',
, 'How do I sign up for Medicare?',
, 'What are the different parts of Medicare?',
, 'Will my Medicare premiums be higher because of my higher income?']