Public
Edited
Oct 8, 2023
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
html`<div class="camera">
<video id="video" style="width: 400px; height: 300px">Video stream not available.</video>
</div>
<canvas id="canvas"> </canvas>
<div class="output">
<img id="photo" alt="The screen capture will appear in this box." />
</div>`
Insert cell
img_5933 = FileAttachment("IMG_5933.jpeg").image()
Insert cell
img_5928 = FileAttachment("IMG_5928.jpeg").image()
Insert cell
img_5929 = FileAttachment("IMG_5929.jpeg").image()
Insert cell
img_5930 = FileAttachment("IMG_5930.jpeg").image()
Insert cell
img_5934 = FileAttachment("IMG_5934.jpeg").image()
Insert cell
img_5935 = FileAttachment("IMG_5935.jpeg").image()
Insert cell
capture
Insert cell
photos = [img_5928, img_5929, img_5930, img_5933, img_5934, img_5935]
Insert cell
photo_embeddings = {
var embeddings = [];
for (var photo of photos) {
let embedding = await vision_model(
await processor(await transformers.RawImage.fromBlob(photo))
);
embeddings.push(embedding.image_embeds.data);
}
return embeddings;
}
Insert cell
navigator.mediaDevices
.getUserMedia({ video: true, audio: false })
.then((stream) => {
document.querySelector("#video").srcObject = stream;
document.querySelector("#video").play();
})
.catch((err) => {
console.error(`An error occurred: ${err}`);
})
Insert cell
viewof browse_image = Inputs.file()
Insert cell
Insert cell
md`Similarity score: ${(score * 100).toLocaleString()}%`
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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more