Spaces:
Running
Running
title: README | |
emoji: ❤️ | |
colorFrom: red | |
colorTo: red | |
sdk: static | |
pinned: false | |
SentenceTransformers 🤗 is a Python framework for state-of-the-art sentence, text and image embeddings. | |
Install the [Sentence Transformers](https://www.sbert.net/) library. | |
``` | |
pip install -U sentence-transformers | |
``` | |
The usage is as simple as: | |
```python | |
from sentence_transformers import CrossEncoder | |
# Load a pre-trained CrossEncoder model | |
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2") | |
# Predict scores for a pair of sentences | |
scores = model.predict([ | |
("How many people live in Berlin?", "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers."), | |
("How many people live in Berlin?", "Berlin is well known for its museums."), | |
]) | |
# => array([ 8.607138 , -4.3200774], dtype=float32) | |
``` | |
Alternatively, you can also use the [`CrossEncoder.rank`](https://sbert.net/docs/package_reference/cross_encoder/cross_encoder.html#sentence_transformers.cross_encoder.CrossEncoder.rank) argument to rerank documents given a query: | |
<details><summary>Click to see the script</summary> | |
```python | |
from sentence_transformers import CrossEncoder | |
# Load a pre-trained CrossEncoder model | |
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2") | |
# Rank a list of passages for a query | |
query = "How many people live in Berlin?" | |
passages = [ | |
"Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.", | |
"Berlin is well known for its museums.", | |
"In 2014, the city state Berlin had 37,368 live births (+6.6%), a record number since 1991.", | |
"The urban area of Berlin comprised about 4.1 million people in 2014, making it the seventh most populous urban area in the European Union.", | |
"The city of Paris had a population of 2,165,423 people within its administrative city limits as of January 1, 2019", | |
"An estimated 300,000-420,000 Muslims reside in Berlin, making up about 8-11 percent of the population.", | |
"Berlin is subdivided into 12 boroughs or districts (Bezirke).", | |
"In 2015, the total labour force in Berlin was 1.85 million.", | |
"In 2013 around 600,000 Berliners were registered in one of the more than 2,300 sport and fitness clubs.", | |
"Berlin has a yearly total of about 135 million day visitors, which puts it in third place among the most-visited city destinations in the European Union.", | |
] | |
ranks = model.rank(query, passages) | |
# Print the scores | |
print("Query:", query) | |
for rank in ranks: | |
print(f"{rank['score']:.2f}\t{passages[rank['corpus_id']]}") | |
""" | |
Query: How many people live in Berlin? | |
8.92 The urban area of Berlin comprised about 4.1 million people in 2014, making it the seventh most populous urban area in the European Union. | |
8.61 Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers. | |
8.24 An estimated 300,000-420,000 Muslims reside in Berlin, making up about 8-11 percent of the population. | |
7.60 In 2014, the city state Berlin had 37,368 live births (+6.6%), a record number since 1991. | |
6.35 In 2013 around 600,000 Berliners were registered in one of the more than 2,300 sport and fitness clubs. | |
5.42 Berlin has a yearly total of about 135 million day visitors, which puts it in third place among the most-visited city destinations in the European Union. | |
3.45 In 2015, the total labour force in Berlin was 1.85 million. | |
0.33 Berlin is subdivided into 12 boroughs or districts (Bezirke). | |
-4.24 The city of Paris had a population of 2,165,423 people within its administrative city limits as of January 1, 2019 | |
-4.32 Berlin is well known for its museums. | |
""" | |
``` | |
</details> | |
Hugging Face makes it easy to collaboratively build and showcase your [Sentence Transformers](https://www.sbert.net/) models! You can collaborate with your organization, upload and showcase your own models in your profile ❤️ | |
<div class="grid lg:grid-cols-2 gap-x-4 gap-y-7"> | |
<a href="https://sbert.net/docs/cross_encoder/usage/usage.html" class="block overflow-hidden group"> | |
<div | |
class="w-full h-40 object-cover mb-2 rounded-lg flex items-center justify-center bg-[#FA8072]" | |
> | |
<img alt="" src="https://huggingface.co/spaces/sentence-transformers/README/resolve/main/sbertLogo.png" class="w-40" /> | |
</div> | |
<div class="underline">Documentation</div> | |
</a> | |
<a | |
href="https://sbert.net/docs/package_reference/cross_encoder/cross_encoder.html#sentence_transformers.cross_encoder.CrossEncoder.push_to_hub" | |
class="block overflow-hidden group" | |
> | |
<div | |
class="w-full h-40 mb-2 bg-gray-900 group-hover:bg-gray-850 rounded-lg flex items-start justify-start overflow-hidden" | |
> | |
<img | |
alt="" | |
src="https://huggingface.co/spaces/sentence-transformers/README/resolve/main/push-to-hub.png" | |
class="w-full h-40 object-cover overflow-hidden" | |
/> | |
</div> | |
<div class="underline">Push your CrossEncoder models to the Hub ❤️ </div> | |
</a> | |
<!-- <a | |
href="https://huggingface.co/models?library=sentence-transformers&sort=downloads" | |
class="block overflow-hidden group" | |
> | |
<div | |
class="w-full h-40 mb-2 bg-gray-900 group-hover:bg-gray-850 rounded-lg flex items-start justify-start overflow-hidden" | |
> | |
<img | |
alt="" | |
src="https://huggingface.co/spaces/sentence-transformers/README/resolve/main/sbert-hf.png" | |
class="w-full h-40 object-cover overflow-hidden" | |
/> | |
</div> | |
<div class="underline">Find all Sentence Transformers models on the 🤗 Hub</div> | |
</a> --> | |
</div> | |
To upload your CrossEncoder models to the Hugging Face Hub, log in with `huggingface-cli login` and use the [`push_to_hub`](https://sbert.net/docs/package_reference/cross_encoder/cross_encoder.html#sentence_transformers.cross_encoder.CrossEncoder.push_to_hub) method within the Sentence Transformers library. | |
```python | |
from sentence_transformers import CrossEncoder | |
# Load or train a model | |
model = CrossEncoder(...) | |
# Push to Hub | |
model.push_to_hub("my_new_model") | |
``` |