Spaces:
Running
Running
Update to doc & vis question answering
Browse files- README.md +7 -5
- index.html +163 -108
- index.mjs +616 -0
README.md
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
| 9 |
-
description: Showcase
|
| 10 |
duplicated_from: huggingfacejs/image-to-text
|
| 11 |
---
|
| 12 |
|
| 13 |
-
Showcase
|
| 14 |
|
| 15 |
-
Default
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Document and visual question answering
|
| 3 |
+
emoji: ❓
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
| 9 |
+
description: Showcase document & visual question answering using huggingface.js
|
| 10 |
duplicated_from: huggingfacejs/image-to-text
|
| 11 |
---
|
| 12 |
|
| 13 |
+
Showcase document & visual question answering using the `@huggingface/inference` JS lib.
|
| 14 |
|
| 15 |
+
Default models for inference:
|
| 16 |
+
* Documents: https://huggingface.co/impira/layoutlm-document-qa
|
| 17 |
+
* Images: https://huggingface.co/dandelin/vilt-b32-finetuned-vqa
|
index.html
CHANGED
|
@@ -1,120 +1,175 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
{
|
| 11 |
"imports": {
|
| 12 |
-
"@huggingface/inference": "
|
| 13 |
}
|
| 14 |
}
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
<span
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
<a href="https://github.com/huggingface/huggingface.js">
|
| 25 |
<kbd>@huggingface/inference</kbd>
|
| 26 |
</a>
|
| 27 |
</span>
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8"/>
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
| 6 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 7 |
+
<!-- polyfill for firefox + import maps -->
|
| 8 |
+
<script src="https://unpkg.com/es-module-shims@1.7.0/dist/es-module-shims.js"></script>
|
| 9 |
+
<script type="importmap">
|
| 10 |
{
|
| 11 |
"imports": {
|
| 12 |
+
"@huggingface/inference": "./index.mjs"
|
| 13 |
}
|
| 14 |
}
|
| 15 |
+
</script>
|
| 16 |
+
</head>
|
| 17 |
+
<body>
|
| 18 |
+
<form class="w-[90%] mx-auto pt-8" onsubmit="launch(); return false;">
|
| 19 |
+
<h1 class="text-3xl font-bold">
|
| 20 |
<span
|
| 21 |
+
class="bg-clip-text text-transparent bg-gradient-to-r from-pink-500 to-violet-500"
|
| 22 |
+
>
|
| 23 |
+
Document & visual question answering demo with
|
| 24 |
<a href="https://github.com/huggingface/huggingface.js">
|
| 25 |
<kbd>@huggingface/inference</kbd>
|
| 26 |
</a>
|
| 27 |
</span>
|
| 28 |
+
</h1>
|
| 29 |
+
|
| 30 |
+
<p class="mt-8">
|
| 31 |
+
First, input your token if you have one! Otherwise, you may encounter
|
| 32 |
+
rate limiting. You can create a token for free at
|
| 33 |
+
<a
|
| 34 |
+
target="_blank"
|
| 35 |
+
href="https://huggingface.co/settings/tokens"
|
| 36 |
+
class="underline text-blue-500"
|
| 37 |
+
>hf.co/settings/tokens</a
|
| 38 |
+
>
|
| 39 |
+
</p>
|
| 40 |
+
|
| 41 |
+
<input
|
| 42 |
+
type="text"
|
| 43 |
+
id="token"
|
| 44 |
+
class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
|
| 45 |
+
placeholder="token (optional)"
|
| 46 |
+
/>
|
| 47 |
+
|
| 48 |
+
<p class="mt-8">
|
| 49 |
+
Pick the model type and the model you want to run. Check out models for
|
| 50 |
+
<a
|
| 51 |
+
href="https://huggingface.co/tasks/document-question-answering"
|
| 52 |
+
class="underline text-blue-500"
|
| 53 |
+
target="_blank"
|
| 54 |
+
>
|
| 55 |
+
document</a
|
| 56 |
+
> and
|
| 57 |
+
<a
|
| 58 |
+
href="https://huggingface.co/tasks/visual-question-answering"
|
| 59 |
+
class="underline text-blue-500"
|
| 60 |
+
target="_blank"
|
| 61 |
+
>image</a> question answering.
|
| 62 |
+
</p>
|
| 63 |
+
|
| 64 |
+
<div class="space-x-2 flex text-sm mt-8">
|
| 65 |
+
<label>
|
| 66 |
+
<input class="sr-only peer" name="type" type="radio" value="document" onclick="update_model(this.value)" checked />
|
| 67 |
+
<div class="px-3 py-3 rounded-lg shadow-md flex items-center justify-center text-slate-700 bg-gradient-to-r peer-checked:font-semibold peer-checked:from-pink-500 peer-checked:to-violet-500 peer-checked:text-white">
|
| 68 |
+
Document
|
| 69 |
+
</div>
|
| 70 |
+
</label>
|
| 71 |
+
<label>
|
| 72 |
+
<input class="sr-only peer" name="type" type="radio" value="image" onclick="update_model(this.value)" />
|
| 73 |
+
<div class="px-3 py-3 rounded-lg shadow-md flex items-center justify-center text-slate-700 bg-gradient-to-r peer-checked:font-semibold peer-checked:from-pink-500 peer-checked:to-violet-500 peer-checked:text-white">
|
| 74 |
+
Image
|
| 75 |
+
</div>
|
| 76 |
+
</label>
|
| 77 |
+
</div>
|
| 78 |
+
|
| 79 |
+
<input
|
| 80 |
+
id="model"
|
| 81 |
+
class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
|
| 82 |
+
value="impira/layoutlm-document-qa"
|
| 83 |
+
required
|
| 84 |
+
/>
|
| 85 |
+
|
| 86 |
+
<p class="mt-8">The input image</p>
|
| 87 |
+
|
| 88 |
+
<input type="file" required accept="image/*"
|
| 89 |
+
class="rounded border-blue-500 shadow-md px-3 py-2 w-96 mt-6 block"
|
| 90 |
+
rows="5"
|
| 91 |
+
id="image"
|
| 92 |
+
/>
|
| 93 |
+
|
| 94 |
+
<p class="mt-8">The question</p>
|
| 95 |
+
|
| 96 |
+
<input
|
| 97 |
+
type="text"
|
| 98 |
+
id="question"
|
| 99 |
+
class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
|
| 100 |
+
required
|
| 101 |
+
/>
|
| 102 |
+
|
| 103 |
+
<button
|
| 104 |
+
id="submit"
|
| 105 |
+
class="my-8 bg-green-500 rounded py-3 px-5 text-white shadow-md disabled:bg-slate-300"
|
| 106 |
+
>
|
| 107 |
+
Run
|
| 108 |
+
</button>
|
| 109 |
+
|
| 110 |
+
<p class="text-gray-400 text-sm">Output logs</p>
|
| 111 |
+
<div id="logs" class="bg-gray-100 rounded p-3 mb-8 text-sm">
|
| 112 |
+
Output will be here
|
| 113 |
+
</div>
|
| 114 |
+
|
| 115 |
+
<p>Check out the <a class="underline text-blue-500"
|
| 116 |
+
href="#"
|
| 117 |
+
target="_blank">source code</a></p>
|
| 118 |
+
</form>
|
| 119 |
+
|
| 120 |
+
<script type="module">
|
| 121 |
+
import {HfInference} from "@huggingface/inference";
|
| 122 |
+
|
| 123 |
+
const default_models = {
|
| 124 |
+
"document": "impira/layoutlm-document-qa",
|
| 125 |
+
"image": "dandelin/vilt-b32-finetuned-vqa",
|
| 126 |
+
};
|
| 127 |
+
|
| 128 |
+
let running = false;
|
| 129 |
+
|
| 130 |
+
async function launch() {
|
| 131 |
+
if (running) {
|
| 132 |
+
return;
|
| 133 |
+
}
|
| 134 |
+
running = true;
|
| 135 |
+
try {
|
| 136 |
+
const hf = new HfInference(
|
| 137 |
+
document.getElementById("token").value.trim() || undefined
|
| 138 |
+
);
|
| 139 |
+
const model = document.getElementById("model").value.trim();
|
| 140 |
+
const model_type = document.querySelector("[name=type]:checked").value;
|
| 141 |
+
const image = document.getElementById("image").files[0];
|
| 142 |
+
const question = document.getElementById("question").value.trim();
|
| 143 |
+
document.getElementById("logs").textContent = "";
|
| 144 |
+
|
| 145 |
+
const method = model_type === "document" ? hf.documentQuestionAnswering : hf.visualQuestionAnswering;
|
| 146 |
+
const {answer, score} = await method({model, inputs: {
|
| 147 |
+
image, question
|
| 148 |
+
}});
|
| 149 |
+
|
| 150 |
+
document.getElementById("logs").textContent = answer + ": " + score;
|
| 151 |
+
} catch (err) {
|
| 152 |
+
alert("Error: " + err.message);
|
| 153 |
+
} finally {
|
| 154 |
+
running = false;
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
window.launch = launch;
|
| 159 |
+
|
| 160 |
+
window.update_model = (model_type) => {
|
| 161 |
+
const model_input = document.getElementById("model");
|
| 162 |
+
const cur_model = model_input.value.trim();
|
| 163 |
+
let new_model = "";
|
| 164 |
+
if (
|
| 165 |
+
model_type === "document" && cur_model === default_models["image"]
|
| 166 |
+
|| model_type === "image" && cur_model === default_models["document"]
|
| 167 |
+
|| cur_model === ""
|
| 168 |
+
) {
|
| 169 |
+
new_model = default_models[model_type];
|
| 170 |
+
}
|
| 171 |
+
model_input.value = new_model;
|
| 172 |
+
};
|
| 173 |
+
</script>
|
| 174 |
+
</body>
|
| 175 |
</html>
|
index.mjs
ADDED
|
@@ -0,0 +1,616 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
var __defProp = Object.defineProperty;
|
| 2 |
+
var __export = (target, all) => {
|
| 3 |
+
for (var name in all)
|
| 4 |
+
__defProp(target, name, { get: all[name], enumerable: true });
|
| 5 |
+
};
|
| 6 |
+
|
| 7 |
+
// src/tasks/index.ts
|
| 8 |
+
var tasks_exports = {};
|
| 9 |
+
__export(tasks_exports, {
|
| 10 |
+
audioClassification: () => audioClassification,
|
| 11 |
+
automaticSpeechRecognition: () => automaticSpeechRecognition,
|
| 12 |
+
conversational: () => conversational,
|
| 13 |
+
documentQuestionAnswering: () => documentQuestionAnswering,
|
| 14 |
+
featureExtraction: () => featureExtraction,
|
| 15 |
+
fillMask: () => fillMask,
|
| 16 |
+
imageClassification: () => imageClassification,
|
| 17 |
+
imageSegmentation: () => imageSegmentation,
|
| 18 |
+
imageToText: () => imageToText,
|
| 19 |
+
objectDetection: () => objectDetection,
|
| 20 |
+
questionAnswering: () => questionAnswering,
|
| 21 |
+
request: () => request,
|
| 22 |
+
sentenceSimilarity: () => sentenceSimilarity,
|
| 23 |
+
streamingRequest: () => streamingRequest,
|
| 24 |
+
summarization: () => summarization,
|
| 25 |
+
tableQuestionAnswering: () => tableQuestionAnswering,
|
| 26 |
+
textClassification: () => textClassification,
|
| 27 |
+
textGeneration: () => textGeneration,
|
| 28 |
+
textGenerationStream: () => textGenerationStream,
|
| 29 |
+
textToImage: () => textToImage,
|
| 30 |
+
tokenClassification: () => tokenClassification,
|
| 31 |
+
translation: () => translation,
|
| 32 |
+
visualQuestionAnswering: () => visualQuestionAnswering,
|
| 33 |
+
zeroShotClassification: () => zeroShotClassification
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
// src/lib/makeRequestOptions.ts
|
| 37 |
+
var HF_INFERENCE_API_BASE_URL = "https://api-inference.huggingface.co/models/";
|
| 38 |
+
function makeRequestOptions(args, options) {
|
| 39 |
+
const { model, accessToken, ...otherArgs } = args;
|
| 40 |
+
const headers = {};
|
| 41 |
+
if (accessToken) {
|
| 42 |
+
headers["Authorization"] = `Bearer ${accessToken}`;
|
| 43 |
+
}
|
| 44 |
+
const binary = "data" in args && !!args.data;
|
| 45 |
+
if (!binary) {
|
| 46 |
+
headers["Content-Type"] = "application/json";
|
| 47 |
+
} else {
|
| 48 |
+
if (options?.wait_for_model) {
|
| 49 |
+
headers["X-Wait-For-Model"] = "true";
|
| 50 |
+
}
|
| 51 |
+
if (options?.use_cache === false) {
|
| 52 |
+
headers["X-Use-Cache"] = "false";
|
| 53 |
+
}
|
| 54 |
+
if (options?.dont_load_model) {
|
| 55 |
+
headers["X-Load-Model"] = "0";
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
const url = /^http(s?):/.test(model) || model.startsWith("/") ? model : `${HF_INFERENCE_API_BASE_URL}${model}`;
|
| 59 |
+
const info = {
|
| 60 |
+
headers,
|
| 61 |
+
method: "POST",
|
| 62 |
+
body: binary ? args.data : JSON.stringify({
|
| 63 |
+
...otherArgs,
|
| 64 |
+
options
|
| 65 |
+
}),
|
| 66 |
+
credentials: options?.includeCredentials ? "include" : "same-origin"
|
| 67 |
+
};
|
| 68 |
+
return { url, info };
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// src/tasks/custom/request.ts
|
| 72 |
+
async function request(args, options) {
|
| 73 |
+
const { url, info } = makeRequestOptions(args, options);
|
| 74 |
+
const response = await fetch(url, info);
|
| 75 |
+
if (options?.retry_on_error !== false && response.status === 503 && !options?.wait_for_model) {
|
| 76 |
+
return request(args, {
|
| 77 |
+
...options,
|
| 78 |
+
wait_for_model: true
|
| 79 |
+
});
|
| 80 |
+
}
|
| 81 |
+
if (!response.ok) {
|
| 82 |
+
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
|
| 83 |
+
const output = await response.json();
|
| 84 |
+
if (output.error) {
|
| 85 |
+
throw new Error(output.error);
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
throw new Error("An error occurred while fetching the blob");
|
| 89 |
+
}
|
| 90 |
+
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
|
| 91 |
+
return await response.json();
|
| 92 |
+
}
|
| 93 |
+
return await response.blob();
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// src/vendor/fetch-event-source/parse.ts
|
| 97 |
+
function getLines(onLine) {
|
| 98 |
+
let buffer;
|
| 99 |
+
let position;
|
| 100 |
+
let fieldLength;
|
| 101 |
+
let discardTrailingNewline = false;
|
| 102 |
+
return function onChunk(arr) {
|
| 103 |
+
if (buffer === void 0) {
|
| 104 |
+
buffer = arr;
|
| 105 |
+
position = 0;
|
| 106 |
+
fieldLength = -1;
|
| 107 |
+
} else {
|
| 108 |
+
buffer = concat(buffer, arr);
|
| 109 |
+
}
|
| 110 |
+
const bufLength = buffer.length;
|
| 111 |
+
let lineStart = 0;
|
| 112 |
+
while (position < bufLength) {
|
| 113 |
+
if (discardTrailingNewline) {
|
| 114 |
+
if (buffer[position] === 10 /* NewLine */) {
|
| 115 |
+
lineStart = ++position;
|
| 116 |
+
}
|
| 117 |
+
discardTrailingNewline = false;
|
| 118 |
+
}
|
| 119 |
+
let lineEnd = -1;
|
| 120 |
+
for (; position < bufLength && lineEnd === -1; ++position) {
|
| 121 |
+
switch (buffer[position]) {
|
| 122 |
+
case 58 /* Colon */:
|
| 123 |
+
if (fieldLength === -1) {
|
| 124 |
+
fieldLength = position - lineStart;
|
| 125 |
+
}
|
| 126 |
+
break;
|
| 127 |
+
case 13 /* CarriageReturn */:
|
| 128 |
+
discardTrailingNewline = true;
|
| 129 |
+
case 10 /* NewLine */:
|
| 130 |
+
lineEnd = position;
|
| 131 |
+
break;
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
if (lineEnd === -1) {
|
| 135 |
+
break;
|
| 136 |
+
}
|
| 137 |
+
onLine(buffer.subarray(lineStart, lineEnd), fieldLength);
|
| 138 |
+
lineStart = position;
|
| 139 |
+
fieldLength = -1;
|
| 140 |
+
}
|
| 141 |
+
if (lineStart === bufLength) {
|
| 142 |
+
buffer = void 0;
|
| 143 |
+
} else if (lineStart !== 0) {
|
| 144 |
+
buffer = buffer.subarray(lineStart);
|
| 145 |
+
position -= lineStart;
|
| 146 |
+
}
|
| 147 |
+
};
|
| 148 |
+
}
|
| 149 |
+
function getMessages(onId, onRetry, onMessage) {
|
| 150 |
+
let message = newMessage();
|
| 151 |
+
const decoder = new TextDecoder();
|
| 152 |
+
return function onLine(line, fieldLength) {
|
| 153 |
+
if (line.length === 0) {
|
| 154 |
+
onMessage?.(message);
|
| 155 |
+
message = newMessage();
|
| 156 |
+
} else if (fieldLength > 0) {
|
| 157 |
+
const field = decoder.decode(line.subarray(0, fieldLength));
|
| 158 |
+
const valueOffset = fieldLength + (line[fieldLength + 1] === 32 /* Space */ ? 2 : 1);
|
| 159 |
+
const value = decoder.decode(line.subarray(valueOffset));
|
| 160 |
+
switch (field) {
|
| 161 |
+
case "data":
|
| 162 |
+
message.data = message.data ? message.data + "\n" + value : value;
|
| 163 |
+
break;
|
| 164 |
+
case "event":
|
| 165 |
+
message.event = value;
|
| 166 |
+
break;
|
| 167 |
+
case "id":
|
| 168 |
+
onId(message.id = value);
|
| 169 |
+
break;
|
| 170 |
+
case "retry":
|
| 171 |
+
const retry = parseInt(value, 10);
|
| 172 |
+
if (!isNaN(retry)) {
|
| 173 |
+
onRetry(message.retry = retry);
|
| 174 |
+
}
|
| 175 |
+
break;
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
};
|
| 179 |
+
}
|
| 180 |
+
function concat(a, b) {
|
| 181 |
+
const res = new Uint8Array(a.length + b.length);
|
| 182 |
+
res.set(a);
|
| 183 |
+
res.set(b, a.length);
|
| 184 |
+
return res;
|
| 185 |
+
}
|
| 186 |
+
function newMessage() {
|
| 187 |
+
return {
|
| 188 |
+
data: "",
|
| 189 |
+
event: "",
|
| 190 |
+
id: "",
|
| 191 |
+
retry: void 0
|
| 192 |
+
};
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
// src/tasks/custom/streamingRequest.ts
|
| 196 |
+
async function* streamingRequest(args, options) {
|
| 197 |
+
const { url, info } = makeRequestOptions({ ...args, stream: true }, options);
|
| 198 |
+
const response = await fetch(url, info);
|
| 199 |
+
if (options?.retry_on_error !== false && response.status === 503 && !options?.wait_for_model) {
|
| 200 |
+
return streamingRequest(args, {
|
| 201 |
+
...options,
|
| 202 |
+
wait_for_model: true
|
| 203 |
+
});
|
| 204 |
+
}
|
| 205 |
+
if (!response.ok) {
|
| 206 |
+
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
|
| 207 |
+
const output = await response.json();
|
| 208 |
+
if (output.error) {
|
| 209 |
+
throw new Error(output.error);
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
throw new Error(`Server response contains error: ${response.status}`);
|
| 213 |
+
}
|
| 214 |
+
if (response.headers.get("content-type") !== "text/event-stream") {
|
| 215 |
+
throw new Error(
|
| 216 |
+
`Server does not support event stream content type, it returned ` + response.headers.get("content-type")
|
| 217 |
+
);
|
| 218 |
+
}
|
| 219 |
+
if (!response.body) {
|
| 220 |
+
return;
|
| 221 |
+
}
|
| 222 |
+
const reader = response.body.getReader();
|
| 223 |
+
let events = [];
|
| 224 |
+
const onEvent = (event) => {
|
| 225 |
+
events.push(event);
|
| 226 |
+
};
|
| 227 |
+
const onChunk = getLines(
|
| 228 |
+
getMessages(
|
| 229 |
+
() => {
|
| 230 |
+
},
|
| 231 |
+
() => {
|
| 232 |
+
},
|
| 233 |
+
onEvent
|
| 234 |
+
)
|
| 235 |
+
);
|
| 236 |
+
try {
|
| 237 |
+
while (true) {
|
| 238 |
+
const { done, value } = await reader.read();
|
| 239 |
+
if (done)
|
| 240 |
+
return;
|
| 241 |
+
onChunk(value);
|
| 242 |
+
for (const event of events) {
|
| 243 |
+
if (event.data.length > 0) {
|
| 244 |
+
yield JSON.parse(event.data);
|
| 245 |
+
}
|
| 246 |
+
}
|
| 247 |
+
events = [];
|
| 248 |
+
}
|
| 249 |
+
} finally {
|
| 250 |
+
reader.releaseLock();
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
// src/lib/InferenceOutputError.ts
|
| 255 |
+
var InferenceOutputError = class extends TypeError {
|
| 256 |
+
constructor(message) {
|
| 257 |
+
super(
|
| 258 |
+
`Invalid inference output: ${message}. Use the 'request' method with the same parameters to do a custom call with no type checking.`
|
| 259 |
+
);
|
| 260 |
+
this.name = "InferenceOutputError";
|
| 261 |
+
}
|
| 262 |
+
};
|
| 263 |
+
|
| 264 |
+
// src/tasks/audio/audioClassification.ts
|
| 265 |
+
async function audioClassification(args, options) {
|
| 266 |
+
const res = await request(args, options);
|
| 267 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x.label === "string" && typeof x.score === "number");
|
| 268 |
+
if (!isValidOutput) {
|
| 269 |
+
throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
|
| 270 |
+
}
|
| 271 |
+
return res;
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// src/tasks/audio/automaticSpeechRecognition.ts
|
| 275 |
+
async function automaticSpeechRecognition(args, options) {
|
| 276 |
+
const res = await request(args, options);
|
| 277 |
+
const isValidOutput = typeof res?.text === "string";
|
| 278 |
+
if (!isValidOutput) {
|
| 279 |
+
throw new InferenceOutputError("Expected {text: string}");
|
| 280 |
+
}
|
| 281 |
+
return res;
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
// src/tasks/cv/imageClassification.ts
|
| 285 |
+
async function imageClassification(args, options) {
|
| 286 |
+
const res = await request(args, options);
|
| 287 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x.label === "string" && typeof x.score === "number");
|
| 288 |
+
if (!isValidOutput) {
|
| 289 |
+
throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
|
| 290 |
+
}
|
| 291 |
+
return res;
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
// src/tasks/cv/imageSegmentation.ts
|
| 295 |
+
async function imageSegmentation(args, options) {
|
| 296 |
+
const res = await request(args, options);
|
| 297 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x.label === "string" && typeof x.mask === "string" && typeof x.score === "number");
|
| 298 |
+
if (!isValidOutput) {
|
| 299 |
+
throw new InferenceOutputError("Expected Array<{label: string, mask: string, score: number}>");
|
| 300 |
+
}
|
| 301 |
+
return res;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
// src/tasks/cv/imageToText.ts
|
| 305 |
+
async function imageToText(args, options) {
|
| 306 |
+
const res = (await request(args, options))?.[0];
|
| 307 |
+
if (typeof res?.generated_text !== "string") {
|
| 308 |
+
throw new InferenceOutputError("Expected {generated_text: string}");
|
| 309 |
+
}
|
| 310 |
+
return res;
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
// src/tasks/cv/objectDetection.ts
|
| 314 |
+
async function objectDetection(args, options) {
|
| 315 |
+
const res = await request(args, options);
|
| 316 |
+
const isValidOutput = Array.isArray(res) && res.every(
|
| 317 |
+
(x) => typeof x.label === "string" && typeof x.score === "number" && typeof x.box.xmin === "number" && typeof x.box.ymin === "number" && typeof x.box.xmax === "number" && typeof x.box.ymax === "number"
|
| 318 |
+
);
|
| 319 |
+
if (!isValidOutput) {
|
| 320 |
+
throw new InferenceOutputError(
|
| 321 |
+
"Expected Array<{label:string; score:number; box:{xmin:number; ymin:number; xmax:number; ymax:number}}>"
|
| 322 |
+
);
|
| 323 |
+
}
|
| 324 |
+
return res;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
// src/tasks/cv/textToImage.ts
|
| 328 |
+
async function textToImage(args, options) {
|
| 329 |
+
const res = await request(args, options);
|
| 330 |
+
const isValidOutput = res && res instanceof Blob;
|
| 331 |
+
if (!isValidOutput) {
|
| 332 |
+
throw new InferenceOutputError("Expected Blob");
|
| 333 |
+
}
|
| 334 |
+
return res;
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
// src/tasks/nlp/conversational.ts
|
| 338 |
+
async function conversational(args, options) {
|
| 339 |
+
const res = await request(args, options);
|
| 340 |
+
const isValidOutput = Array.isArray(res.conversation.generated_responses) && res.conversation.generated_responses.every((x) => typeof x === "string") && Array.isArray(res.conversation.past_user_inputs) && res.conversation.past_user_inputs.every((x) => typeof x === "string") && typeof res.generated_text === "string" && Array.isArray(res.warnings) && res.warnings.every((x) => typeof x === "string");
|
| 341 |
+
if (!isValidOutput) {
|
| 342 |
+
throw new InferenceOutputError(
|
| 343 |
+
"Expected {conversation: {generated_responses: string[], past_user_inputs: string[]}, generated_text: string, warnings: string[]}"
|
| 344 |
+
);
|
| 345 |
+
}
|
| 346 |
+
return res;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
// src/tasks/nlp/featureExtraction.ts
|
| 350 |
+
async function featureExtraction(args, options) {
|
| 351 |
+
const res = await request(args, options);
|
| 352 |
+
let isValidOutput = true;
|
| 353 |
+
if (Array.isArray(res)) {
|
| 354 |
+
for (const e of res) {
|
| 355 |
+
if (Array.isArray(e)) {
|
| 356 |
+
isValidOutput = e.every((x) => typeof x === "number");
|
| 357 |
+
if (!isValidOutput) {
|
| 358 |
+
break;
|
| 359 |
+
}
|
| 360 |
+
} else if (typeof e !== "number") {
|
| 361 |
+
isValidOutput = false;
|
| 362 |
+
break;
|
| 363 |
+
}
|
| 364 |
+
}
|
| 365 |
+
} else {
|
| 366 |
+
isValidOutput = false;
|
| 367 |
+
}
|
| 368 |
+
if (!isValidOutput) {
|
| 369 |
+
throw new InferenceOutputError("Expected Array<number[] | number>");
|
| 370 |
+
}
|
| 371 |
+
return res;
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
// src/tasks/nlp/fillMask.ts
|
| 375 |
+
async function fillMask(args, options) {
|
| 376 |
+
const res = await request(args, options);
|
| 377 |
+
const isValidOutput = Array.isArray(res) && res.every(
|
| 378 |
+
(x) => typeof x.score === "number" && typeof x.sequence === "string" && typeof x.token === "number" && typeof x.token_str === "string"
|
| 379 |
+
);
|
| 380 |
+
if (!isValidOutput) {
|
| 381 |
+
throw new InferenceOutputError(
|
| 382 |
+
"Expected Array<{score: number, sequence: string, token: number, token_str: string}>"
|
| 383 |
+
);
|
| 384 |
+
}
|
| 385 |
+
return res;
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
// src/tasks/nlp/questionAnswering.ts
|
| 389 |
+
async function questionAnswering(args, options) {
|
| 390 |
+
const res = await request(args, options);
|
| 391 |
+
const isValidOutput = typeof res?.answer === "string" && typeof res.end === "number" && typeof res.score === "number" && typeof res.start === "number";
|
| 392 |
+
if (!isValidOutput) {
|
| 393 |
+
throw new InferenceOutputError("Expected {answer: string, end: number, score: number, start: number}");
|
| 394 |
+
}
|
| 395 |
+
return res;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
// src/tasks/nlp/sentenceSimilarity.ts
|
| 399 |
+
async function sentenceSimilarity(args, options) {
|
| 400 |
+
const res = await request(args, options);
|
| 401 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
|
| 402 |
+
if (!isValidOutput) {
|
| 403 |
+
throw new InferenceOutputError("Expected number[]");
|
| 404 |
+
}
|
| 405 |
+
return res;
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
// src/tasks/nlp/summarization.ts
|
| 409 |
+
async function summarization(args, options) {
|
| 410 |
+
const res = await request(args, options);
|
| 411 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.summary_text === "string");
|
| 412 |
+
if (!isValidOutput) {
|
| 413 |
+
throw new InferenceOutputError("Expected Array<{summary_text: string}>");
|
| 414 |
+
}
|
| 415 |
+
return res?.[0];
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
// src/tasks/nlp/tableQuestionAnswering.ts
|
| 419 |
+
async function tableQuestionAnswering(args, options) {
|
| 420 |
+
const res = await request(args, options);
|
| 421 |
+
const isValidOutput = typeof res?.aggregator === "string" && typeof res.answer === "string" && Array.isArray(res.cells) && res.cells.every((x) => typeof x === "string") && Array.isArray(res.coordinates) && res.coordinates.every((coord) => Array.isArray(coord) && coord.every((x) => typeof x === "number"));
|
| 422 |
+
if (!isValidOutput) {
|
| 423 |
+
throw new InferenceOutputError(
|
| 424 |
+
"Expected {aggregator: string, answer: string, cells: string[], coordinates: number[][]}"
|
| 425 |
+
);
|
| 426 |
+
}
|
| 427 |
+
return res;
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
// src/tasks/nlp/textClassification.ts
|
| 431 |
+
async function textClassification(args, options) {
|
| 432 |
+
const res = (await request(args, options))?.[0];
|
| 433 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.label === "string" && typeof x.score === "number");
|
| 434 |
+
if (!isValidOutput) {
|
| 435 |
+
throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
|
| 436 |
+
}
|
| 437 |
+
return res;
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
// src/tasks/nlp/textGeneration.ts
|
| 441 |
+
async function textGeneration(args, options) {
|
| 442 |
+
const res = await request(args, options);
|
| 443 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.generated_text === "string");
|
| 444 |
+
if (!isValidOutput) {
|
| 445 |
+
throw new InferenceOutputError("Expected Array<{generated_text: string}>");
|
| 446 |
+
}
|
| 447 |
+
return res?.[0];
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
// src/tasks/nlp/textGenerationStream.ts
|
| 451 |
+
async function* textGenerationStream(args, options) {
|
| 452 |
+
yield* streamingRequest(args, options);
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
// src/utils/toArray.ts
|
| 456 |
+
function toArray(obj) {
|
| 457 |
+
if (Array.isArray(obj)) {
|
| 458 |
+
return obj;
|
| 459 |
+
}
|
| 460 |
+
return [obj];
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
// src/tasks/nlp/tokenClassification.ts
|
| 464 |
+
async function tokenClassification(args, options) {
|
| 465 |
+
const res = toArray(await request(args, options));
|
| 466 |
+
const isValidOutput = Array.isArray(res) && res.every(
|
| 467 |
+
(x) => typeof x.end === "number" && typeof x.entity_group === "string" && typeof x.score === "number" && typeof x.start === "number" && typeof x.word === "string"
|
| 468 |
+
);
|
| 469 |
+
if (!isValidOutput) {
|
| 470 |
+
throw new InferenceOutputError(
|
| 471 |
+
"Expected Array<{end: number, entity_group: string, score: number, start: number, word: string}>"
|
| 472 |
+
);
|
| 473 |
+
}
|
| 474 |
+
return res;
|
| 475 |
+
}
|
| 476 |
+
|
| 477 |
+
// src/tasks/nlp/translation.ts
|
| 478 |
+
async function translation(args, options) {
|
| 479 |
+
const res = await request(args, options);
|
| 480 |
+
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.translation_text === "string");
|
| 481 |
+
if (!isValidOutput) {
|
| 482 |
+
throw new InferenceOutputError("Expected type Array<{translation_text: string}>");
|
| 483 |
+
}
|
| 484 |
+
return res?.[0];
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
// src/tasks/nlp/zeroShotClassification.ts
|
| 488 |
+
async function zeroShotClassification(args, options) {
|
| 489 |
+
const res = toArray(
|
| 490 |
+
await request(args, options)
|
| 491 |
+
);
|
| 492 |
+
const isValidOutput = Array.isArray(res) && res.every(
|
| 493 |
+
(x) => Array.isArray(x.labels) && x.labels.every((_label) => typeof _label === "string") && Array.isArray(x.scores) && x.scores.every((_score) => typeof _score === "number") && typeof x.sequence === "string"
|
| 494 |
+
);
|
| 495 |
+
if (!isValidOutput) {
|
| 496 |
+
throw new InferenceOutputError("Expected Array<{labels: string[], scores: number[], sequence: string}>");
|
| 497 |
+
}
|
| 498 |
+
return res;
|
| 499 |
+
}
|
| 500 |
+
|
| 501 |
+
// ../shared/src/base64FromBytes.ts
|
| 502 |
+
function base64FromBytes(arr) {
|
| 503 |
+
if (globalThis.Buffer) {
|
| 504 |
+
return globalThis.Buffer.from(arr).toString("base64");
|
| 505 |
+
} else {
|
| 506 |
+
const bin = [];
|
| 507 |
+
arr.forEach((byte) => {
|
| 508 |
+
bin.push(String.fromCharCode(byte));
|
| 509 |
+
});
|
| 510 |
+
return globalThis.btoa(bin.join(""));
|
| 511 |
+
}
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
// src/tasks/multimodal/documentQuestionAnswering.ts
|
| 515 |
+
async function documentQuestionAnswering(args, options) {
|
| 516 |
+
const reqArgs = {
|
| 517 |
+
...args,
|
| 518 |
+
inputs: {
|
| 519 |
+
question: args.inputs.question,
|
| 520 |
+
// convert Blob to base64
|
| 521 |
+
image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer()))
|
| 522 |
+
}
|
| 523 |
+
};
|
| 524 |
+
const res = (await request(reqArgs, options))?.[0];
|
| 525 |
+
const isValidOutput = typeof res?.answer === "string" && typeof res.end === "number" && typeof res.score === "number" && typeof res.start === "number";
|
| 526 |
+
if (!isValidOutput) {
|
| 527 |
+
throw new InferenceOutputError("Expected Array<{answer: string, end: number, score: number, start: number}>");
|
| 528 |
+
}
|
| 529 |
+
return res;
|
| 530 |
+
}
|
| 531 |
+
|
| 532 |
+
// src/tasks/multimodal/visualQuestionAnswering.ts
|
| 533 |
+
async function visualQuestionAnswering(args, options) {
|
| 534 |
+
const reqArgs = {
|
| 535 |
+
...args,
|
| 536 |
+
inputs: {
|
| 537 |
+
question: args.inputs.question,
|
| 538 |
+
// convert Blob to base64
|
| 539 |
+
image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer()))
|
| 540 |
+
}
|
| 541 |
+
};
|
| 542 |
+
const res = (await request(reqArgs, options))?.[0];
|
| 543 |
+
const isValidOutput = typeof res?.answer === "string" && typeof res.score === "number";
|
| 544 |
+
if (!isValidOutput) {
|
| 545 |
+
throw new InferenceOutputError("Expected Array<{answer: string, score: number}>");
|
| 546 |
+
}
|
| 547 |
+
return res;
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
// src/HfInference.ts
|
| 551 |
+
var HfInference = class {
|
| 552 |
+
accessToken;
|
| 553 |
+
defaultOptions;
|
| 554 |
+
constructor(accessToken = "", defaultOptions = {}) {
|
| 555 |
+
this.accessToken = accessToken;
|
| 556 |
+
this.defaultOptions = defaultOptions;
|
| 557 |
+
for (const [name, fn] of Object.entries(tasks_exports)) {
|
| 558 |
+
Object.defineProperty(this, name, {
|
| 559 |
+
enumerable: false,
|
| 560 |
+
value: (params, options) => (
|
| 561 |
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
| 562 |
+
fn({ ...params, accessToken }, { ...defaultOptions, ...options })
|
| 563 |
+
)
|
| 564 |
+
});
|
| 565 |
+
}
|
| 566 |
+
}
|
| 567 |
+
/**
|
| 568 |
+
* Returns copy of HfInference tied to a specified endpoint.
|
| 569 |
+
*/
|
| 570 |
+
endpoint(endpointUrl) {
|
| 571 |
+
return new HfInferenceEndpoint(endpointUrl, this.accessToken, this.defaultOptions);
|
| 572 |
+
}
|
| 573 |
+
};
|
| 574 |
+
var HfInferenceEndpoint = class {
|
| 575 |
+
constructor(endpointUrl, accessToken = "", defaultOptions = {}) {
|
| 576 |
+
accessToken;
|
| 577 |
+
defaultOptions;
|
| 578 |
+
for (const [name, fn] of Object.entries(tasks_exports)) {
|
| 579 |
+
Object.defineProperty(this, name, {
|
| 580 |
+
enumerable: false,
|
| 581 |
+
value: (params, options) => (
|
| 582 |
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
| 583 |
+
fn({ ...params, accessToken, model: endpointUrl }, { ...defaultOptions, ...options })
|
| 584 |
+
)
|
| 585 |
+
});
|
| 586 |
+
}
|
| 587 |
+
}
|
| 588 |
+
};
|
| 589 |
+
export {
|
| 590 |
+
HfInference,
|
| 591 |
+
HfInferenceEndpoint,
|
| 592 |
+
audioClassification,
|
| 593 |
+
automaticSpeechRecognition,
|
| 594 |
+
conversational,
|
| 595 |
+
documentQuestionAnswering,
|
| 596 |
+
featureExtraction,
|
| 597 |
+
fillMask,
|
| 598 |
+
imageClassification,
|
| 599 |
+
imageSegmentation,
|
| 600 |
+
imageToText,
|
| 601 |
+
objectDetection,
|
| 602 |
+
questionAnswering,
|
| 603 |
+
request,
|
| 604 |
+
sentenceSimilarity,
|
| 605 |
+
streamingRequest,
|
| 606 |
+
summarization,
|
| 607 |
+
tableQuestionAnswering,
|
| 608 |
+
textClassification,
|
| 609 |
+
textGeneration,
|
| 610 |
+
textGenerationStream,
|
| 611 |
+
textToImage,
|
| 612 |
+
tokenClassification,
|
| 613 |
+
translation,
|
| 614 |
+
visualQuestionAnswering,
|
| 615 |
+
zeroShotClassification
|
| 616 |
+
};
|