Skip to content

Commit d57fc81

Browse files
authored
Simpler support for new model libraries (#482)
close this [internal issue](huggingface-internal/moon-landing#8791) (ignore the unrelated README changes)
1 parent ea2d471 commit d57fc81

File tree

7 files changed

+420
-377
lines changed

7 files changed

+420
-377
lines changed

README.md

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,46 @@
1010
</p>
1111

1212
```ts
13+
// Programatically interact with the Hub
14+
15+
await createRepo({
16+
repo: {type: "model", name: "my-user/nlp-model"},
17+
credentials: {accessToken: HF_TOKEN}
18+
});
19+
20+
await uploadFile({
21+
repo: "my-user/nlp-model",
22+
credentials: {accessToken: HF_TOKEN},
23+
// Can work with native File in browsers
24+
file: {
25+
path: "pytorch_model.bin",
26+
content: new Blob(...)
27+
}
28+
});
29+
30+
// Use hosted inference
31+
1332
await inference.translation({
1433
model: 't5-base',
1534
inputs: 'My name is Wolfgang and I live in Berlin'
1635
})
1736

18-
await hf.translation({
19-
model: "facebook/nllb-200-distilled-600M",
20-
inputs: "how is the weather like in Gaborone",
21-
parameters : {
22-
src_lang: "eng_Latn",
23-
tgt_lang: "sot_Latn"
24-
}
25-
})
26-
2737
await inference.textToImage({
2838
model: 'stabilityai/stable-diffusion-2',
2939
inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]',
3040
parameters: {
3141
negative_prompt: 'blurry',
3242
}
3343
})
44+
45+
// and much more…
3446
```
3547

3648
# Hugging Face JS libraries
3749

3850
This is a collection of JS libraries to interact with the Hugging Face API, with TS types included.
3951

40-
- [@huggingface/inference](packages/inference/README.md): Use Inference Endpoints (serverless) to make calls to 100,000+ Machine Learning models
52+
- [@huggingface/inference](packages/inference/README.md): Use Inference Endpoints (serverless or dedicated) to make calls to 100,000+ Machine Learning models
4153
- [@huggingface/hub](packages/hub/README.md): Interact with huggingface.co to create or delete repos and commit / download files
4254
- [@huggingface/agents](packages/agents/README.md): Interact with HF models through a natural language interface
4355

@@ -130,30 +142,6 @@ await inference.imageToText({
130142
const gpt2 = inference.endpoint('https://xyz.eu-west-1.aws.endpoints.huggingface.cloud/gpt2');
131143
const { generated_text } = await gpt2.textGeneration({inputs: 'The answer to the universe is'});
132144
```
133-
### @huggingface/agents example
134-
135-
```ts
136-
import {HfAgent, LLMFromHub, defaultTools} from '@huggingface/agents';
137-
138-
const HF_TOKEN = "hf_...";
139-
140-
const agent = new HfAgent(
141-
HF_TOKEN,
142-
LLMFromHub(HF_TOKEN),
143-
[...defaultTools]
144-
);
145-
146-
147-
// you can generate the code, inspect it and then run it
148-
const code = await agent.generateCode("Draw a picture of a cat wearing a top hat. Then caption the picture and read it out loud.");
149-
console.log(code);
150-
const messages = await agent.evaluateCode(code)
151-
console.log(messages); // contains the data
152-
153-
// or you can run the code directly, however you can't check that the code is safe to execute this way, use at your own risk.
154-
const messages = await agent.run("Draw a picture of a cat wearing a top hat. Then caption the picture and read it out loud.")
155-
console.log(messages);
156-
```
157145

158146
### @huggingface/hub examples
159147

@@ -184,6 +172,31 @@ await deleteFiles({
184172
});
185173
```
186174

175+
### @huggingface/agents example
176+
177+
```ts
178+
import {HfAgent, LLMFromHub, defaultTools} from '@huggingface/agents';
179+
180+
const HF_TOKEN = "hf_...";
181+
182+
const agent = new HfAgent(
183+
HF_TOKEN,
184+
LLMFromHub(HF_TOKEN),
185+
[...defaultTools]
186+
);
187+
188+
189+
// you can generate the code, inspect it and then run it
190+
const code = await agent.generateCode("Draw a picture of a cat wearing a top hat. Then caption the picture and read it out loud.");
191+
console.log(code);
192+
const messages = await agent.evaluateCode(code)
193+
console.log(messages); // contains the data
194+
195+
// or you can run the code directly, however you can't check that the code is safe to execute this way, use at your own risk.
196+
const messages = await agent.run("Draw a picture of a cat wearing a top hat. Then caption the picture and read it out loud.")
197+
console.log(messages);
198+
```
199+
187200

188201
There are more features of course, check each library's README!
189202

packages/hub/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 🤗 Hugging Face Hub API
22

3-
Official utilities to use the Hugging Face hub API, still very experimental.
3+
Official utilities to use the Hugging Face Hub API.
44

55
## Install
66

packages/tasks/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This package contains the definition files (written in Typescript) for the huggi
2525

2626
- **pipeline types** a.k.a. **task types** (used to determine which widget to display on the model page, and which inference API to run)
2727
- **default widget inputs** (when they aren't provided in the model card)
28-
- definitions and UI elements for **third party libraries**.
28+
- definitions and UI elements for **model libraries** (and soon for **dataset libraries**).
2929

3030
Please add to any of those definitions by opening a PR. Thanks 🔥
3131

packages/tasks/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export { LIBRARY_TASK_MAPPING_EXCLUDING_TRANSFORMERS } from "./library-to-tasks";
2-
export { MODEL_LIBRARIES_UI_ELEMENTS } from "./library-ui-elements";
32
export { MAPPING_DEFAULT_WIDGET } from "./default-widget-inputs";
43
export type { TaskData, TaskDemo, TaskDemoEntry, ExampleRepo } from "./tasks";
54
export * from "./tasks";
@@ -14,8 +13,8 @@ export {
1413
SUBTASK_TYPES,
1514
PIPELINE_TYPES_SET,
1615
} from "./pipelines";
17-
export { ModelLibrary, ALL_DISPLAY_MODEL_LIBRARY_KEYS } from "./model-libraries";
18-
export type { ModelLibraryKey } from "./model-libraries";
16+
export { ALL_DISPLAY_MODEL_LIBRARY_KEYS, ALL_MODEL_LIBRARY_KEYS, MODEL_LIBRARIES_UI_ELEMENTS } from "./model-libraries";
17+
export type { LibraryUiElement, ModelLibraryKey } from "./model-libraries";
1918
export type { ModelData, TransformersInfo } from "./model-data";
2019
export type {
2120
WidgetExample,
@@ -41,5 +40,3 @@ export { InferenceDisplayability } from "./model-data";
4140

4241
import * as snippets from "./snippets";
4342
export { snippets };
44-
45-
export type { LibraryUiElement } from "./library-ui-elements";

packages/tasks/src/library-to-tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { PipelineType } from "./pipelines";
33

44
/**
55
* Mapping from library name (excluding Transformers) to its supported tasks.
6-
* Inference Endpoints (serverless) should be disabled for all other (library, task) pairs beyond this mapping.
6+
* Inference API (serverless) should be disabled for all other (library, task) pairs beyond this mapping.
77
* As an exception, we assume Transformers supports all inference tasks.
88
* This mapping is generated automatically by "python-api-export-tasks" action in huggingface/api-inference-community repo upon merge.
99
* Ref: https://github.com/huggingface/api-inference-community/pull/158

0 commit comments

Comments
 (0)