Skip to content

Commit fc11648

Browse files
authored
➕ Add @huggingface/tasks as a dep of @huggingface/inference (#651)
Only for TS types. Later let's do it without, cf #584 cc @SBrandeis @radames
1 parent 17222f6 commit fc11648

File tree

7 files changed

+68
-207
lines changed

7 files changed

+68
-207
lines changed

.github/workflows/inference-publish.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,44 @@ jobs:
5252
git add ../..
5353
git commit -m "🔖 @hugginface/inference $BUMPED_VERSION"
5454
git tag "inference-v$BUMPED_VERSION"
55+
56+
- name: Make sure that the latest version of @huggingface/tasks is consistent with the local version
57+
run: |
58+
LOCAL_TASKS_VERSION=$(node -p "require('./package.json').version")
59+
REMOTE_TASKS_VERSION=$(npm view @huggingface/tasks version)
60+
61+
# If the versions are different, error
62+
if [ "$LOCAL_TASKS_VERSION" != "$REMOTE_TASKS_VERSION" ]; then
63+
echo "Error: The local @huggingface/tasks package version ($LOCAL_TASKS_VERSION) differs from the remote version ($REMOTE_TASKS_VERSION). Release halted."
64+
exit 1
65+
fi
66+
67+
npm pack @huggingface/tasks
68+
mv huggingface-tasks-$LOCAL_TASKS_VERSION.tgz tasks-local.tgz
69+
70+
npm pack @huggingface/tasks@$REMOTE_TASKS_VERSION
71+
mv huggingface-tasks-$REMOTE_TASKS_VERSION.tgz tasks-remote.tgz
72+
73+
# Compute checksum of local tar. We need to extract both tar since the remote compression might be different
74+
tar -xf tasks-local.tgz
75+
LOCAL_CHECKSUM=$(cd package && tar --mtime='1970-01-01' --mode=755 -cf - . | sha256sum | cut -d' ' -f1)
76+
echo "Local package checksum: $LOCAL_CHECKSUM"
77+
78+
rm -Rf package
79+
80+
tar -xf tasks-remote.tgz
81+
REMOTE_CHECKSUM=$(cd package && tar --mtime='1970-01-01' --mode=755 -cf - . | sha256sum | cut -d' ' -f1)
82+
echo "Remote package checksum: $REMOTE_CHECKSUM"
83+
84+
rm -Rf package
85+
86+
if [ "$LOCAL_CHECKSUM" != "$REMOTE_CHECKSUM" ]; then
87+
echo "Checksum Verification Failed: The local @huggingface/tasks package differs from the remote version. Release halted. Local Checksum: $LOCAL_CHECKSUM, Remote Checksum: $REMOTE_CHECKSUM"
88+
exit 1
89+
fi
90+
echo "Checksum Verification Successful: The local and remote @huggingface/tasks packages are consistent. Proceeding with the @huggingface/widgets package release. Local Checksum: $LOCAL_CHECKSUM, Remote Checksum: $REMOTE_CHECKSUM."
91+
working-directory: packages/tasks
92+
5593
- run: pnpm publish --no-git-checks .
5694
env:
5795
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
run: |
6060
sleep 3
6161
pnpm i --filter inference... --filter hub... --frozen-lockfile
62-
pnpm --filter inference --filter hub publish --force --no-git-checks --registry http://localhost:4874/
62+
pnpm --filter inference --filter hub --filter tasks publish --force --no-git-checks --registry http://localhost:4874/
6363
6464
- name: E2E test - test yarn install
6565
working-directory: e2e/ts

packages/inference/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"type": "module",
4141
"scripts": {
4242
"build": "tsup src/index.ts --format cjs,esm --clean && pnpm run dts",
43-
"dts": "tsx scripts/generate-dts.ts",
43+
"dts": "tsx scripts/generate-dts.ts && tsc --noEmit dist/index.d.ts",
4444
"lint": "eslint --quiet --fix --ext .cjs,.ts .",
4545
"lint:check": "eslint --ext .cjs,.ts .",
4646
"format": "prettier --write .",
@@ -51,8 +51,10 @@
5151
"test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.mts",
5252
"check": "tsc"
5353
},
54+
"dependencies": {
55+
"@huggingface/tasks": "workspace:^"
56+
},
5457
"devDependencies": {
55-
"@huggingface/tasks": "workspace:^",
5658
"@types/node": "18.13.0"
5759
},
5860
"resolutions": {}

packages/inference/pnpm-lock.yaml

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

packages/inference/scripts/generate-dts.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { readFileSync, writeFileSync, appendFileSync, readdirSync } from "node:fs";
44
import { TASKS_DATA } from "@huggingface/tasks";
55

6+
const taskImports = new Set<string>();
7+
68
const tasks = Object.keys(TASKS_DATA)
79
.sort()
810
.filter((task) => task !== "other");
@@ -36,6 +38,16 @@ for (const dir of dirs) {
3638

3739
const fileContent = readFileSync(`./src/tasks/${dir}/${file}`, "utf-8");
3840

41+
// detect imports from @huggingface/tasks
42+
for (const imports of fileContent.matchAll(/import type {(.*)} from "@huggingface\/tasks";/g)) {
43+
// Convert A, B, C to ["A", "B", "C"]
44+
const imported = imports[1].split(",").map((x) => x.trim());
45+
46+
for (const imp of imported) {
47+
taskImports.add(imp);
48+
}
49+
}
50+
3951
for (const type of extractTypesAndInterfaces(fileContent)) {
4052
appendFileSync("./dist/index.d.ts", type + "\n");
4153
}
@@ -87,6 +99,13 @@ appendFileSync(
8799
"\n}\n"
88100
);
89101

102+
// Prepend import from @huggingface/tasks
103+
writeFileSync(
104+
"./dist/index.d.ts",
105+
`import type { ${[...taskImports].join(", ")} } from "@huggingface/tasks";\n` +
106+
readFileSync("./dist/index.d.ts", "utf-8")
107+
);
108+
90109
function* extractTypesAndInterfaces(fileContent: string): Iterable<string> {
91110
let index = 0;
92111

packages/inference/src/tasks/nlp/textGeneration.ts

Lines changed: 2 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,209 +1,9 @@
1+
import type { TextGenerationInput, TextGenerationOutput } from "@huggingface/tasks";
12
import { InferenceOutputError } from "../../lib/InferenceOutputError";
23
import type { BaseArgs, Options } from "../../types";
34
import { request } from "../custom/request";
45

5-
/**
6-
* Inputs for Text Generation inference
7-
*/
8-
export interface TextGenerationInput {
9-
/**
10-
* The text to initialize generation with
11-
*/
12-
inputs: string;
13-
/**
14-
* Additional inference parameters
15-
*/
16-
parameters?: TextGenerationParameters;
17-
/**
18-
* Whether to stream output tokens
19-
*/
20-
stream?: boolean;
21-
[property: string]: unknown;
22-
}
23-
24-
/**
25-
* Additional inference parameters
26-
*
27-
* Additional inference parameters for Text Generation
28-
*/
29-
export interface TextGenerationParameters {
30-
/**
31-
* The number of sampling queries to run. Only the best one (in terms of total logprob) will
32-
* be returned.
33-
*/
34-
best_of?: number;
35-
/**
36-
* Whether or not to output decoder input details
37-
*/
38-
decoder_input_details?: boolean;
39-
/**
40-
* Whether or not to output details
41-
*/
42-
details?: boolean;
43-
/**
44-
* Whether to use logits sampling instead of greedy decoding when generating new tokens.
45-
*/
46-
do_sample?: boolean;
47-
/**
48-
* The maximum number of tokens to generate.
49-
*/
50-
max_new_tokens?: number;
51-
/**
52-
* The parameter for repetition penalty. A value of 1.0 means no penalty. See [this
53-
* paper](https://hf.co/papers/1909.05858) for more details.
54-
*/
55-
repetition_penalty?: number;
56-
/**
57-
* Whether to prepend the prompt to the generated text.
58-
*/
59-
return_full_text?: boolean;
60-
/**
61-
* The random sampling seed.
62-
*/
63-
seed?: number;
64-
/**
65-
* Stop generating tokens if a member of `stop_sequences` is generated.
66-
*/
67-
stop_sequences?: string[];
68-
/**
69-
* The value used to modulate the logits distribution.
70-
*/
71-
temperature?: number;
72-
/**
73-
* The number of highest probability vocabulary tokens to keep for top-k-filtering.
74-
*/
75-
top_k?: number;
76-
/**
77-
* If set to < 1, only the smallest set of most probable tokens with probabilities that add
78-
* up to `top_p` or higher are kept for generation.
79-
*/
80-
top_p?: number;
81-
/**
82-
* Truncate input tokens to the given size.
83-
*/
84-
truncate?: number;
85-
/**
86-
* Typical Decoding mass. See [Typical Decoding for Natural Language
87-
* Generation](https://hf.co/papers/2202.00666) for more information
88-
*/
89-
typical_p?: number;
90-
/**
91-
* Watermarking with [A Watermark for Large Language Models](https://hf.co/papers/2301.10226)
92-
*/
93-
watermark?: boolean;
94-
[property: string]: unknown;
95-
}
96-
97-
/**
98-
* Outputs for Text Generation inference
99-
*/
100-
export interface TextGenerationOutput {
101-
/**
102-
* When enabled, details about the generation
103-
*/
104-
details?: TextGenerationOutputDetails;
105-
/**
106-
* The generated text
107-
*/
108-
generated_text: string;
109-
[property: string]: unknown;
110-
}
111-
112-
/**
113-
* When enabled, details about the generation
114-
*/
115-
export interface TextGenerationOutputDetails {
116-
/**
117-
* Details about additional sequences when best_of is provided
118-
*/
119-
best_of_sequences?: TextGenerationOutputSequenceDetails[];
120-
/**
121-
* The reason why the generation was stopped.
122-
*/
123-
finish_reason: TextGenerationFinishReason;
124-
/**
125-
* The number of generated tokens
126-
*/
127-
generated_tokens: number;
128-
prefill: TextGenerationPrefillToken[];
129-
/**
130-
* The random seed used for generation
131-
*/
132-
seed?: number;
133-
/**
134-
* The generated tokens and associated details
135-
*/
136-
tokens: TextGenerationOutputToken[];
137-
/**
138-
* Most likely tokens
139-
*/
140-
top_tokens?: Array<TextGenerationOutputToken[]>;
141-
[property: string]: unknown;
142-
}
143-
144-
export interface TextGenerationOutputSequenceDetails {
145-
finish_reason: TextGenerationFinishReason;
146-
/**
147-
* The generated text
148-
*/
149-
generated_text: string;
150-
/**
151-
* The number of generated tokens
152-
*/
153-
generated_tokens: number;
154-
prefill: TextGenerationPrefillToken[];
155-
/**
156-
* The random seed used for generation
157-
*/
158-
seed?: number;
159-
/**
160-
* The generated tokens and associated details
161-
*/
162-
tokens: TextGenerationOutputToken[];
163-
/**
164-
* Most likely tokens
165-
*/
166-
top_tokens?: Array<TextGenerationOutputToken[]>;
167-
[property: string]: unknown;
168-
}
169-
170-
export interface TextGenerationPrefillToken {
171-
id: number;
172-
logprob: number;
173-
/**
174-
* The text associated with that token
175-
*/
176-
text: string;
177-
[property: string]: unknown;
178-
}
179-
180-
/**
181-
* Generated token.
182-
*/
183-
export interface TextGenerationOutputToken {
184-
id: number;
185-
logprob?: number;
186-
/**
187-
* Whether or not that token is a special one
188-
*/
189-
special: boolean;
190-
/**
191-
* The text associated with that token
192-
*/
193-
text: string;
194-
[property: string]: unknown;
195-
}
196-
197-
/**
198-
* The reason why the generation was stopped.
199-
*
200-
* length: The generated sequence reached the maximum allowed length
201-
*
202-
* eos_token: The model generated an end-of-sentence (EOS) token
203-
*
204-
* stop_sequence: One of the sequence in stop_sequences was generated
205-
*/
206-
export type TextGenerationFinishReason = "length" | "eos_token" | "stop_sequence";
6+
export type { TextGenerationInput, TextGenerationOutput };
2077

2088
/**
2099
* Use to continue text from a prompt. This is a very generic task. Recommended model: gpt2 (it’s a simple model, but fun to play with).

packages/inference/src/tasks/nlp/textGenerationStream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import type { TextGenerationInput } from "@huggingface/tasks";
12
import type { BaseArgs, Options } from "../../types";
23
import { streamingRequest } from "../custom/streamingRequest";
3-
import type { TextGenerationInput } from "./textGeneration";
44

55
export interface TextGenerationStreamToken {
66
/** Token ID from the model tokenizer */

0 commit comments

Comments
 (0)