Skip to content

Commit bdaa678

Browse files
authored
add handle_file docs (#8522)
1 parent 595ecf3 commit bdaa678

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

.changeset/tired-dryers-retire.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/client": minor
3+
"gradio": minor
4+
---
5+
6+
feat:add handle_file docs

client/js/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,110 @@ const app = await Client.duplicate("user/space-name", {
339339
hardware: "a10g-small"
340340
});
341341
```
342+
343+
### `handle_file(file_or_url: File | string | Blob | Buffer)`
344+
345+
This utility function is used to simplify the process of handling file inputs for the client.
346+
347+
Gradio APIs expect a special file datastructure that references a location on the server. These files can be manually uploaded but figuring what to do with different file types can be difficult depending on your environment.
348+
349+
This function will handle files regardless of whether or not they are local files (node only), URLs, Blobs, or Buffers. It will take in a reference and handle it accordingly,uploading the file where appropriate and generating the correct data structure for the client.
350+
351+
The return value of this function can be used anywhere in the input data where a file is expected:
352+
353+
```ts
354+
import { handle_file } from "@gradio/client";
355+
356+
const app = await Client.connect("user/space-name");
357+
const result = await app.predict("/predict", {
358+
single: handle_file(file),
359+
flat: [handle_file(url), handle_file(buffer)],
360+
nested: {
361+
image: handle_file(url),
362+
layers: [handle_file(buffer)]
363+
},
364+
deeply_nested: {
365+
image: handle_file(url),
366+
layers: [{
367+
layer1: handle_file(buffer),
368+
layer2: handle_file(buffer)
369+
}]
370+
}
371+
});
372+
```
373+
374+
#### filepaths
375+
376+
`handle_file` can be passed a local filepath which it will upload to the client server and return a reference that the client can understand.
377+
378+
This only works in a node environment.
379+
380+
Filepaths are resolved relative to the current working directory, not the location of the file that calls `handle_file`.
381+
382+
```ts
383+
import { handle_file } from "@gradio/client";
384+
385+
// not uploaded yet
386+
const file_ref = handle_file("path/to/file");
387+
388+
const app = await Client.connect("user/space-name");
389+
390+
// upload happens here
391+
const result = await app.predict("/predict", {
392+
file: file_ref,
393+
});
394+
```
395+
396+
#### URLs
397+
398+
`handle_file` can be passed a URL which it will convert into a reference that the client can understand.
399+
400+
```ts
401+
import { handle_file } from "@gradio/client";
402+
403+
const url_ref = handle_file("https://example.com/file.png");
404+
405+
const app = await Client.connect("user/space-name");
406+
const result = await app.predict("/predict", {
407+
url: url_ref,
408+
});
409+
```
410+
411+
#### Blobs
412+
413+
`handle_file` can be passed a Blob which it will upload to the client server and return a reference that the client can understand.
414+
415+
The upload is not initiated until predict or submit are called.
416+
417+
```ts
418+
import { handle_file } from "@gradio/client";
419+
420+
// not uploaded yet
421+
const blob_ref = handle_file(new Blob(["Hello, world!"]));
422+
423+
const app = await Client.connect("user/space-name");
424+
425+
// upload happens here
426+
const result = await app.predict("/predict", {
427+
blob: blob_ref,
428+
});
429+
```
430+
431+
#### Buffers
432+
433+
`handle_file` can be passed a Buffer which it will upload to the client server and return a reference that the client can understand.
434+
435+
```ts
436+
import { handle_file } from "@gradio/client";
437+
import { readFileSync } from "fs";
438+
439+
// not uploaded yet
440+
const buffer_ref = handle_file(readFileSync("file.png"));
441+
442+
const app = await Client.connect("user/space-name");
443+
444+
// upload happens here
445+
const result = await app.predict("/predict", {
446+
buffer: buffer_ref,
447+
});
448+
```

0 commit comments

Comments
 (0)