Skip to content

Commit 2c92943

Browse files
authored
[version] Update to 3.1.0
* Formatting * [version] Update to 3.1.0 * Fix jina clip processor * Fix typo * Support partial model inputs for `JinaCLIPModel` * Increase model load test time (avoid timeouts)
1 parent e848907 commit 2c92943

14 files changed

+128
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ npm i @huggingface/transformers
4747
Alternatively, you can use it in vanilla JS, without any bundler, by using a CDN or static hosting. For example, using [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), you can import the library with:
4848
```html
4949
<script type="module">
50-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2';
50+
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.0';
5151
</script>
5252
```
5353

@@ -155,7 +155,7 @@ Check out the Transformers.js [template](https://huggingface.co/new-space?templa
155155

156156

157157

158-
By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2/dist/), which should work out-of-the-box. You can customize this as follows:
158+
By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.0/dist/), which should work out-of-the-box. You can customize this as follows:
159159

160160
### Settings
161161

docs/snippets/2_installation.snippet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ npm i @huggingface/transformers
77
Alternatively, you can use it in vanilla JS, without any bundler, by using a CDN or static hosting. For example, using [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), you can import the library with:
88
```html
99
<script type="module">
10-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2';
10+
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.0';
1111
</script>
1212
```

docs/snippets/4_custom-usage.snippet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2/dist/), which should work out-of-the-box. You can customize this as follows:
3+
By default, Transformers.js uses [hosted pretrained models](https://huggingface.co/models?library=transformers.js) and [precompiled WASM binaries](https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.0/dist/), which should work out-of-the-box. You can customize this as follows:
44

55
### Settings
66

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@huggingface/transformers",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!",
55
"main": "./src/transformers.js",
66
"types": "./types/transformers.d.ts",

src/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import fs from 'fs';
2626
import path from 'path';
2727
import url from 'url';
2828

29-
const VERSION = '3.0.2';
29+
const VERSION = '3.1.0';
3030

3131
// Check if various APIs are available (depends on environment)
3232
const IS_BROWSER_ENV = typeof self !== 'undefined';

src/models.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3759,7 +3759,43 @@ export class ChineseCLIPModel extends ChineseCLIPPreTrainedModel { }
37593759
// JinaCLIP models
37603760
export class JinaCLIPPreTrainedModel extends PreTrainedModel { }
37613761

3762-
export class JinaCLIPModel extends JinaCLIPPreTrainedModel { }
3762+
export class JinaCLIPModel extends JinaCLIPPreTrainedModel {
3763+
async forward(model_inputs) {
3764+
const missing_text_inputs = !model_inputs.input_ids;
3765+
const missing_image_inputs = !model_inputs.pixel_values;
3766+
3767+
if (missing_text_inputs && missing_image_inputs) {
3768+
throw new Error('Either `input_ids` or `pixel_values` should be provided.');
3769+
}
3770+
3771+
// If either `input_ids` or `pixel_values` aren't passed, we need to create dummy input since the model requires a value to be specified.
3772+
if (missing_text_inputs) {
3773+
// NOTE: We cannot pass zero-dimension tensor as input for input_ids.
3774+
// Fortunately, the majority of time is spent in the vision encoder, so this shouldn't significantly impact performance.
3775+
model_inputs.input_ids = ones([model_inputs.pixel_values.dims[0], 1]);
3776+
}
3777+
3778+
if (missing_image_inputs) {
3779+
// NOTE: Since we create a zero-sized tensor, this does not increase computation time.
3780+
// @ts-ignore
3781+
const { image_size } = this.config.vision_config;
3782+
model_inputs.pixel_values = full([0, 3, image_size, image_size], 0.0); // (pass zero-dimension tensor)
3783+
}
3784+
3785+
const { text_embeddings, image_embeddings, l2norm_text_embeddings, l2norm_image_embeddings } = await super.forward(model_inputs);
3786+
3787+
const result = {};
3788+
if (!missing_text_inputs) {
3789+
result.text_embeddings = text_embeddings;
3790+
result.l2norm_text_embeddings = l2norm_text_embeddings;
3791+
}
3792+
if (!missing_image_inputs) {
3793+
result.image_embeddings = image_embeddings;
3794+
result.l2norm_image_embeddings = l2norm_image_embeddings;
3795+
}
3796+
return result
3797+
}
3798+
}
37633799

37643800
export class JinaCLIPTextModel extends JinaCLIPPreTrainedModel {
37653801
/** @type {typeof PreTrainedModel.from_pretrained} */
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
import {
1+
import {
22
ImageProcessor,
33
} from "../../base/image_processors_utils.js";
44

5-
export class JinaCLIPImageProcessor extends ImageProcessor {}
5+
export class JinaCLIPImageProcessor extends ImageProcessor {
6+
constructor(config) {
7+
// JinaCLIPImageProcessor uses a custom preprocessor_config.json, so we configure it here
8+
const { resize_mode, fill_color, interpolation, size, ...other } = config;
9+
10+
const new_size = resize_mode === 'squash'
11+
? { width: size, height: size }
12+
: resize_mode === 'shortest'
13+
? { shortest_edge: size }
14+
: { longest_edge: size };
15+
16+
const resample = interpolation === 'bicubic' ? 3 : 2;
17+
super({
18+
...other,
19+
size: new_size,
20+
resample,
21+
do_center_crop: true,
22+
crop_size: size,
23+
do_normalize: true,
24+
});
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
import { Processor } from "../../base/processing_utils.js";
3+
import { AutoImageProcessor } from "../auto/image_processing_auto.js";
4+
import { AutoTokenizer } from "../../tokenizers.js";
5+
6+
export class JinaCLIPProcessor extends Processor {
7+
static tokenizer_class = AutoTokenizer
8+
static image_processor_class = AutoImageProcessor
9+
10+
async _call(text=null, images=null, kwargs = {}) {
11+
12+
if (!text && !images){
13+
throw new Error('Either text or images must be provided');
14+
}
15+
16+
const text_inputs = text ? this.tokenizer(text, kwargs) : {};
17+
const image_inputs = images ? await this.image_processor(images, kwargs) : {};
18+
19+
return {
20+
...text_inputs,
21+
...image_inputs,
22+
}
23+
}
24+
}

src/models/processors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './florence2/processing_florence2.js';
22
export * from './mgp_str/processing_mgp_str.js';
33
export * from './janus/processing_janus.js';
4+
export * from './jina_clip/processing_jina_clip.js';
45
export * from './owlvit/processing_owlvit.js';
56
export * from './pyannote/processing_pyannote.js';
67
export * from './qwen2_vl/processing_qwen2_vl.js';

0 commit comments

Comments
 (0)