Skip to content

Commit 83a5025

Browse files
authored
Add example of how to pass a file input to a model (#63)
1 parent a7c7151 commit 83a5025

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ const replicate = new Replicate({
3434
Run a model and await the result:
3535

3636
```js
37-
const model =
38-
"stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478";
37+
const model = "stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478";
3938
const input = {
4039
prompt: "a 19th century portrait of a raccoon gentleman wearing a suit",
4140
};
@@ -68,6 +67,29 @@ console.log(prediction.output);
6867
// ['https://replicate.delivery/pbxt/RoaxeXqhL0xaYyLm6w3bpGwF5RaNBjADukfFnMbhOyeoWBdhA/out-0.png']
6968
```
7069

70+
To run a model that takes a file input,
71+
convert its data into a base64-encoded data URI:
72+
73+
```js
74+
import { promises as fs } from "fs";
75+
76+
// Read the file into a buffer
77+
const data = await fs.readFile("path/to/image.png", "utf-8");
78+
// Convert the buffer into a base64-encoded string
79+
const base64 = data.toString("base64");
80+
// Set MIME type for PNG image
81+
const mimeType = "image/png";
82+
// Create the data URI
83+
const dataURI = `data:${mimeType};base64,${base64}`;
84+
85+
const model = "nightmareai/real-esrgan:42fed1c4974146d4d2414e2be2c5277c7fcf05fcc3a73abf41610695738c1d7b";
86+
const input = {
87+
image: dataURI,
88+
};
89+
const output = await replicate.run(model, { input });
90+
// ['https://replicate.delivery/mgxm/e7b0e122-9daa-410e-8cde-006c7308ff4d/output.png']
91+
```
92+
7193
## API
7294

7395
### Constructor
@@ -175,11 +197,7 @@ const response = await replicate.models.versions.list(model_owner, model_name);
175197
### `replicate.models.versions.get`
176198

177199
```js
178-
const response = await replicate.models.versions.get(
179-
model_owner,
180-
model_name,
181-
version_id
182-
);
200+
const response = await replicate.models.versions.get(model_owner, model_name, version_id);
183201
```
184202

185203
| name | type | description |
@@ -218,7 +236,7 @@ const response = await replicate.predictions.create(options);
218236
| name | type | description |
219237
| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- |
220238
| `options.version` | string | **Required**. The model version |
221-
| `options.input` | object | **Required**. An object with the models inputs |
239+
| `options.input` | object | **Required**. An object with the model's inputs |
222240
| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output |
223241
| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) |
224242

@@ -317,7 +335,7 @@ const response = await replicate.trainings.create(options);
317335
| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- |
318336
| `options.version` | string | **Required**. The model version |
319337
| `options.destination` | string | **Required**. The destination for the trained version in the form `{username}/{model_name}` |
320-
| `options.input` | object | **Required**. An object with the models inputs |
338+
| `options.input` | object | **Required**. An object with the model's inputs |
321339
| `options.webhook` | string | An HTTPS URL for receiving a webhook when the training has new output |
322340
| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) |
323341

0 commit comments

Comments
 (0)