Hugging Face 🤗

The Hugging Face Hub

In addition to the official pre-trained models, you can find over 500 sentence-transformer models on the Hugging Face Hub.

All models on the Hugging Face Hub come with the following:

  1. An automatically generated model card with a description, example code snippets, architecture overview, and more.

  2. Metadata tags that help for discoverability and contain additional information such as a usage license.

  3. An interactive widget you can use to play with the model directly in the browser.

  4. An Inference API that allows you to make inference requests.

Using Hugging Face models

Any pre-trained models from the Hub can be loaded with a single line of code:

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('model_name')

You can even click Use in sentence-transformers to get a code snippet that you can copy and paste!

Here is an example that loads the multi-qa-MiniLM-L6-cos-v1 model and uses it to encode sentences and then compute the distance between them for doing semantic search.

from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer('multi-qa-MiniLM-L6-cos-v1')

query_embedding = model.encode('How big is London')
passage_embedding = model.encode(['London has 9,787,426 inhabitants at the 2011 census',
                                  'London is known for its finacial district'])

print("Similarity:", util.dot_score(query_embedding, passage_embedding))

Here is another example, this time using the clips/mfaq model for multilingual FAQ retrieval. After embedding the query and the answers, we perform a semantic search to find the most relevant answer.

from sentence_transformers import SentenceTransformer, util

question = "<Q>How many models can I host on HuggingFace?"
answer_1 = "<A>All plans come with unlimited private models and datasets."
answer_2 = "<A>AutoNLP is an automatic way to train and deploy state-of-the-art NLP models, seamlessly integrated with the Hugging Face ecosystem."
answer_3 = "<A>Based on how much training data and model variants are created, we send you a compute cost and payment link - as low as $10 per job."

model = SentenceTransformer('clips/mfaq')
query_embedding = model.encode(question)
corpus_embeddings = model.encode([answer_1, answer_2, answer_3])

print(util.semantic_search(query_embedding, corpus_embeddings))

Sharing your models

Once you’ve installed the Hub Client Library, you can login through your terminal with your Hugging Face account.

pip install huggingface_hub
huggingface-cli login

Then, you can share your SentenceTransformers models by calling the save_to_hub method from a trained model. By default, the model will be uploaded to your account, but you can upload to an organization by passing setting an organization parameter. save_to_hub automatically generates a model card, an inference widget, example code snippets, and more.

from sentence_transformers import SentenceTransformer

# Load or train a model

You can automatically add to the Hub’s model card a list of datasets you used to train the model with the argument train_datasets: Optional[List[str]] = None). See the “Datasets used to train” section in the ITESM/sentece-embeddings-BETO model for an example of the final result.

model.save_to_hub("my_new_model", train_datasets=["GEM/wiki_lingua", "code_search_net"])

Sharing your embeddings

The Hugging Face Hub can also be used to store and share any embeddings you generate. You can export your embeddings to CSV, ZIP, Pickle, or any other format, and then upload them to the Hub as a Dataset. Read the “Getting Started With Embeddings” blog post for more information.

Additional resources