Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
base_model:
|
| 4 |
+
- deepseek-ai/Janus-Pro-1B
|
| 5 |
+
pipeline_tag: any-to-any
|
| 6 |
+
library_name: transformers.js
|
| 7 |
+
tags:
|
| 8 |
+
- text-to-image
|
| 9 |
+
- image-to-text
|
| 10 |
+
- image-text-to-text
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
https://huggingface.co/deepseek-ai/Janus-Pro-1B with ONNX weights to be compatible with Transformers.js.
|
| 14 |
+
|
| 15 |
+
## Usage (Transformers.js)
|
| 16 |
+
|
| 17 |
+
If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@huggingface/transformers) using:
|
| 18 |
+
```bash
|
| 19 |
+
npm i @huggingface/transformers
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
**Example:** Image+text to text
|
| 23 |
+
|
| 24 |
+
```js
|
| 25 |
+
import { AutoProcessor, MultiModalityCausalLM } from "@huggingface/transformers";
|
| 26 |
+
|
| 27 |
+
// Load processor and model
|
| 28 |
+
const model_id = "onnx-community/Janus-Pro-1B-ONNX";
|
| 29 |
+
const processor = await AutoProcessor.from_pretrained(model_id);
|
| 30 |
+
const model = await MultiModalityCausalLM.from_pretrained(model_id);
|
| 31 |
+
|
| 32 |
+
// Prepare inputs
|
| 33 |
+
const conversation = [
|
| 34 |
+
{
|
| 35 |
+
role: "<|User|>",
|
| 36 |
+
content: "<image_placeholder>\nConvert the formula into latex code.",
|
| 37 |
+
images: ["https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/quadratic_formula.png"],
|
| 38 |
+
},
|
| 39 |
+
];
|
| 40 |
+
const inputs = await processor(conversation);
|
| 41 |
+
|
| 42 |
+
// Generate response
|
| 43 |
+
const outputs = await model.generate({
|
| 44 |
+
...inputs,
|
| 45 |
+
max_new_tokens: 150,
|
| 46 |
+
do_sample: false,
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
// Decode output
|
| 50 |
+
const new_tokens = outputs.slice(null, [inputs.input_ids.dims.at(-1), null]);
|
| 51 |
+
const decoded = processor.batch_decode(new_tokens, { skip_special_tokens: true });
|
| 52 |
+
console.log(decoded[0]);
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
**Example:** Text to image
|
| 56 |
+
|
| 57 |
+
```js
|
| 58 |
+
import { AutoProcessor, MultiModalityCausalLM } from "@huggingface/transformers";
|
| 59 |
+
|
| 60 |
+
// Load processor and model
|
| 61 |
+
const model_id = "onnx-community/Janus-Pro-1B-ONNX";
|
| 62 |
+
const processor = await AutoProcessor.from_pretrained(model_id);
|
| 63 |
+
const model = await MultiModalityCausalLM.from_pretrained(model_id);
|
| 64 |
+
|
| 65 |
+
// Prepare inputs
|
| 66 |
+
const conversation = [
|
| 67 |
+
{
|
| 68 |
+
role: "<|User|>",
|
| 69 |
+
content: "A stunning princess from kabul in red, white traditional clothing, blue eyes, brown hair",
|
| 70 |
+
},
|
| 71 |
+
];
|
| 72 |
+
const inputs = await processor(conversation, { chat_template: "text_to_image" });
|
| 73 |
+
|
| 74 |
+
// Generate response
|
| 75 |
+
const num_image_tokens = processor.num_image_tokens;
|
| 76 |
+
const outputs = await model.generate_images({
|
| 77 |
+
...inputs,
|
| 78 |
+
min_new_tokens: num_image_tokens,
|
| 79 |
+
max_new_tokens: num_image_tokens,
|
| 80 |
+
do_sample: true,
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
// Save the generated image
|
| 84 |
+
await outputs[0].save("test.png");
|
| 85 |
+
```
|