|
10 | 10 | </p> |
11 | 11 |
|
12 | 12 | ```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 | + |
13 | 32 | await inference.translation({ |
14 | 33 | model: 't5-base', |
15 | 34 | inputs: 'My name is Wolfgang and I live in Berlin' |
16 | 35 | }) |
17 | 36 |
|
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 | | - |
27 | 37 | await inference.textToImage({ |
28 | 38 | model: 'stabilityai/stable-diffusion-2', |
29 | 39 | inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]', |
30 | 40 | parameters: { |
31 | 41 | negative_prompt: 'blurry', |
32 | 42 | } |
33 | 43 | }) |
| 44 | + |
| 45 | +// and much more… |
34 | 46 | ``` |
35 | 47 |
|
36 | 48 | # Hugging Face JS libraries |
37 | 49 |
|
38 | 50 | This is a collection of JS libraries to interact with the Hugging Face API, with TS types included. |
39 | 51 |
|
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 |
41 | 53 | - [@huggingface/hub](packages/hub/README.md): Interact with huggingface.co to create or delete repos and commit / download files |
42 | 54 | - [@huggingface/agents](packages/agents/README.md): Interact with HF models through a natural language interface |
43 | 55 |
|
@@ -130,30 +142,6 @@ await inference.imageToText({ |
130 | 142 | const gpt2 = inference.endpoint('https://xyz.eu-west-1.aws.endpoints.huggingface.cloud/gpt2'); |
131 | 143 | const { generated_text } = await gpt2.textGeneration({inputs: 'The answer to the universe is'}); |
132 | 144 | ``` |
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 | | -``` |
157 | 145 |
|
158 | 146 | ### @huggingface/hub examples |
159 | 147 |
|
@@ -184,6 +172,31 @@ await deleteFiles({ |
184 | 172 | }); |
185 | 173 | ``` |
186 | 174 |
|
| 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 | + |
187 | 200 |
|
188 | 201 | There are more features of course, check each library's README! |
189 | 202 |
|
|
0 commit comments