diff --git a/packages/hub/src/consts.ts b/packages/hub/src/consts.ts index 57dd8a113..5d34e9cae 100644 --- a/packages/hub/src/consts.ts +++ b/packages/hub/src/consts.ts @@ -1,4 +1 @@ export const HUB_URL = "https://huggingface.co"; -export const TEST_HUB_URL = "https://hub-ci.huggingface.co"; -export const TEST_USER = "hub.js"; -export const TEST_ACCESS_TOKEN = "hf_hub.js"; diff --git a/packages/hub/src/lib/commit.spec.ts b/packages/hub/src/lib/commit.spec.ts index 4561b54e3..600c26144 100644 --- a/packages/hub/src/lib/commit.spec.ts +++ b/packages/hub/src/lib/commit.spec.ts @@ -1,6 +1,6 @@ import { assert, it, describe } from "vitest"; -import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts"; +import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../test/consts"; import type { RepoId } from "../types/public"; import type { CommitFile } from "./commit"; import { commit } from "./commit"; diff --git a/packages/hub/src/lib/create-repo.spec.ts b/packages/hub/src/lib/create-repo.spec.ts index 9d07a45d1..89c7c303f 100644 --- a/packages/hub/src/lib/create-repo.spec.ts +++ b/packages/hub/src/lib/create-repo.spec.ts @@ -1,6 +1,6 @@ import { assert, it, describe, expect } from "vitest"; -import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts"; +import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../test/consts"; import { insecureRandomString } from "../utils/insecureRandomString"; import { createRepo } from "./create-repo"; import { deleteRepo } from "./delete-repo"; diff --git a/packages/hub/src/lib/delete-file.spec.ts b/packages/hub/src/lib/delete-file.spec.ts index 6e551760d..51d2defdd 100644 --- a/packages/hub/src/lib/delete-file.spec.ts +++ b/packages/hub/src/lib/delete-file.spec.ts @@ -1,6 +1,6 @@ import { assert, it, describe } from "vitest"; -import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../consts"; +import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../test/consts"; import type { RepoId } from "../types/public"; import { insecureRandomString } from "../utils/insecureRandomString"; import { createRepo } from "./create-repo"; diff --git a/packages/hub/src/lib/delete-files.spec.ts b/packages/hub/src/lib/delete-files.spec.ts index eba07c1f5..cd0208779 100644 --- a/packages/hub/src/lib/delete-files.spec.ts +++ b/packages/hub/src/lib/delete-files.spec.ts @@ -1,6 +1,6 @@ import { assert, it, describe } from "vitest"; -import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts"; +import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../test/consts"; import type { RepoId } from "../types/public"; import { insecureRandomString } from "../utils/insecureRandomString"; import { createRepo } from "./create-repo"; diff --git a/packages/hub/src/lib/upload-file.spec.ts b/packages/hub/src/lib/upload-file.spec.ts index 394676e64..0d202554f 100644 --- a/packages/hub/src/lib/upload-file.spec.ts +++ b/packages/hub/src/lib/upload-file.spec.ts @@ -1,6 +1,6 @@ import { assert, it, describe } from "vitest"; -import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../consts"; +import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../test/consts"; import type { RepoId } from "../types/public"; import { insecureRandomString } from "../utils/insecureRandomString"; import { createRepo } from "./create-repo"; diff --git a/packages/hub/src/lib/upload-files-with-progress.spec.ts b/packages/hub/src/lib/upload-files-with-progress.spec.ts index c9a371a62..d3eb7f0a5 100644 --- a/packages/hub/src/lib/upload-files-with-progress.spec.ts +++ b/packages/hub/src/lib/upload-files-with-progress.spec.ts @@ -1,6 +1,6 @@ import { assert, it, describe } from "vitest"; -import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../consts"; +import { TEST_HUB_URL, TEST_ACCESS_TOKEN, TEST_USER } from "../test/consts"; import type { RepoId } from "../types/public"; import { insecureRandomString } from "../utils/insecureRandomString"; import { createRepo } from "./create-repo"; diff --git a/packages/hub/src/lib/upload-files.spec.ts b/packages/hub/src/lib/upload-files.spec.ts index 4c9c337da..0be61f2a4 100644 --- a/packages/hub/src/lib/upload-files.spec.ts +++ b/packages/hub/src/lib/upload-files.spec.ts @@ -1,6 +1,6 @@ import { assert, it, describe } from "vitest"; -import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../consts"; +import { TEST_ACCESS_TOKEN, TEST_HUB_URL, TEST_USER } from "../test/consts"; import type { RepoId } from "../types/public"; import { insecureRandomString } from "../utils/insecureRandomString"; import { createRepo } from "./create-repo"; diff --git a/packages/hub/src/lib/who-am-i.spec.ts b/packages/hub/src/lib/who-am-i.spec.ts index 18946d95d..c19fb2b0a 100644 --- a/packages/hub/src/lib/who-am-i.spec.ts +++ b/packages/hub/src/lib/who-am-i.spec.ts @@ -1,5 +1,5 @@ import { assert, it, describe } from "vitest"; -import { TEST_ACCESS_TOKEN, TEST_HUB_URL } from "../consts"; +import { TEST_ACCESS_TOKEN, TEST_HUB_URL } from "../test/consts"; import { whoAmI } from "./who-am-i"; describe("whoAmI", () => { diff --git a/packages/hub/src/test/consts.ts b/packages/hub/src/test/consts.ts new file mode 100644 index 000000000..297ea01d0 --- /dev/null +++ b/packages/hub/src/test/consts.ts @@ -0,0 +1,3 @@ +export const TEST_HUB_URL = "https://hub-ci.huggingface.co"; +export const TEST_USER = "hub.js"; +export const TEST_ACCESS_TOKEN = "hf_hub.js"; diff --git a/packages/inference/src/tasks/nlp/translation.ts b/packages/inference/src/tasks/nlp/translation.ts index 410981195..ea7a3054c 100644 --- a/packages/inference/src/tasks/nlp/translation.ts +++ b/packages/inference/src/tasks/nlp/translation.ts @@ -6,21 +6,23 @@ export type TranslationArgs = BaseArgs & { /** * A string to be translated */ - inputs: string; + inputs: string | string[]; }; -export interface TranslationOutput { +export interface TranslationOutputValue { /** * The string after translation */ translation_text: string; } +export type TranslationOutput = TranslationOutputValue | TranslationOutputValue[]; + /** * This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en. */ export async function translation(args: TranslationArgs, options?: Options): Promise { - const res = await request(args, { + const res = await request(args, { ...options, taskHint: "translation", }); @@ -28,5 +30,5 @@ export async function translation(args: TranslationArgs, options?: Options): Pro if (!isValidOutput) { throw new InferenceOutputError("Expected type Array<{translation_text: string}>"); } - return res?.[0]; + return res?.length === 1 ? res?.[0] : res; } diff --git a/packages/inference/test/HfInference.spec.ts b/packages/inference/test/HfInference.spec.ts index b79bc8bfc..0e3de311e 100644 --- a/packages/inference/test/HfInference.spec.ts +++ b/packages/inference/test/HfInference.spec.ts @@ -293,6 +293,20 @@ describe.concurrent( ).toMatchObject({ translation_text: "Mein Name ist Wolfgang und ich lebe in Berlin", }); + // input is a list + expect( + await hf.translation({ + model: "t5-base", + inputs: ["My name is Wolfgang and I live in Berlin", "I work as programmer"], + }) + ).toMatchObject([ + { + translation_text: "Mein Name ist Wolfgang und ich lebe in Berlin", + }, + { + translation_text: "Ich arbeite als Programmierer", + }, + ]); }); it("zeroShotClassification", async () => { expect.extend({ diff --git a/packages/inference/test/tapes.json b/packages/inference/test/tapes.json index 6d4d3a010..680b56014 100644 --- a/packages/inference/test/tapes.json +++ b/packages/inference/test/tapes.json @@ -942,5 +942,26 @@ "vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers" } } + }, + "f7b0286fa03058c95425a91aa758f949ce7772fbe7bd52f2b777e636251a2b8e": { + "url": "https://api-inference.huggingface.co/models/t5-base", + "init": { + "headers": { + "Content-Type": "application/json" + }, + "method": "POST", + "body": "{\"inputs\":[\"My name is Wolfgang and I live in Berlin\",\"I work as programmer\"],\"options\":{}}" + }, + "response": { + "body": "[{\"translation_text\":\"Mein Name ist Wolfgang und ich lebe in Berlin\"},{\"translation_text\":\"Ich arbeite als Programmierer\"}]", + "status": 200, + "statusText": "OK", + "headers": { + "access-control-allow-credentials": "true", + "connection": "keep-alive", + "content-type": "application/json", + "vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers" + } + } } } \ No newline at end of file