Public
Edited
Oct 22, 2023
4 forks
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
browse_image.image()
Insert cell
score = cosineSimilarity(
textEmbeddings.text_embeds.data,
embeddings.image_embeds.data
)
Insert cell
html`<textarea style="width: 100%; height: 10em">${JSON.stringify(
Array.from(textEmbeddings.text_embeds.data)
)}</textarea>`
Insert cell
transformers = import("https://cdn.jsdelivr.net/npm/@xenova/transformers")
Insert cell
processor = await transformers.AutoProcessor.from_pretrained(
"Xenova/clip-vit-base-patch16"
)
Insert cell
vision_model = await transformers.CLIPVisionModelWithProjection.from_pretrained(
"Xenova/clip-vit-base-patch16"
)
Insert cell
default_image = await transformers.RawImage.read(
"https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg"
)
Insert cell
image = browse_image
? await transformers.RawImage.fromBlob(await browse_image.blob())
: await transformers.RawImage.read(
"https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg"
)
Insert cell
image
Insert cell
image_inputs = await processor(image)
Insert cell
embeddings = await vision_model(image_inputs)
Insert cell
embeddings.image_embeds.data
Insert cell
tokenizer = await transformers.AutoTokenizer.from_pretrained(
"Xenova/clip-vit-base-patch16"
)
Insert cell
text_model = await transformers.CLIPTextModelWithProjection.from_pretrained(
"Xenova/clip-vit-base-patch16"
)
Insert cell
text_inputs = tokenizer(texts, { padding: true, truncation: true })
Insert cell
textEmbeddings = await text_model(text_inputs)
Insert cell
function dotProduct(a, b) {
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += a[i] * b[i];
}
return sum;
}
Insert cell
function magnitude(a) {
return Math.sqrt(dotProduct(a, a));
}
Insert cell
function cosineSimilarity(a, b) {
if (a.length !== b.length) {
throw new Error("Vectors must have the same dimensions");
}

const magA = magnitude(a);
const magB = magnitude(b);

if (magA === 0 || magB === 0) {
throw new Error(
"Magnitude of one of the vectors is zero, cannot calculate cosine similarity"
);
}

return dotProduct(a, b) / (magA * magB);
}
Insert cell
texts = [text]
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more