Integrate with Sentence Transformers

#1
by tomaarsen HF Staff - opened

Hello @wl1982 @yanwj @kevinke !

Pull Request overview

  • Add Sentence Transformers v5.4 support with configuration files and a usage example.

Details

See https://huggingface.co/ATH-MaaS/Ovis-VL-Embedding-2B/discussions/1 for more details, as this is just the same integration as that one, but applied to the 9B model.

Install with pip install "sentence-transformers[image]>=5.4.0" "transformers>=5.12.1":

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("ATH-MaaS/Ovis-VL-Embedding-9B", revision="refs/pr/1")

query = "Which planet is known as the Red Planet?"
documents = [
    "Venus is often called Earth's twin because of its similar size and proximity.",
    "Mars, known for its reddish appearance, is often referred to as the Red Planet.",
    "Jupiter, the largest planet in our solar system, has a prominent red spot.",
    "Saturn, famous for its rings, is sometimes mistaken for the Red Planet.",
]

query_embedding = model.encode_query(query)
document_embeddings = model.encode_document(documents)
print(query_embedding.shape, document_embeddings.shape)
# (4096,) (4, 4096)

print(model.similarity(query_embedding, document_embeddings))
# tensor([[0.8671, 0.9523, 0.8929, 0.8882]])

# And a multimodal example:
text_embeddings = model.encode(["Ovis logo", "A green car", "children playing"])
image_embeddings = model.encode(
    "https://huggingface.co/ATH-MaaS/Ovis-VL-Embedding-9B/resolve/main/figures/ovis_logo.png"
)
print(text_embeddings.shape, image_embeddings.shape)
# (3, 4096) (4096,)

print(model.similarity(image_embeddings, text_embeddings))
# tensor([[0.8765, 0.7191, 0.7524]])

Note that the revision="refs/pr/1" allows you to run this code before even merging it, so you can compare against your reference implementation. Please check and let me know what your implementation produces, so I can make sure it matches.

  • Tom Aarsen
tomaarsen changed pull request status to open
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment