Skip to content

Commit 6c3f88e

Browse files
authored
Document and improve file APIs (#68)
* Automatically convert file inputs to their corresponding get URL * Add example usage for uploading file inputs
1 parent c203281 commit 6c3f88e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ prediction, _ := r8.CreatePrediction(ctx, version, input, &webhook, false)
7676
_ = r8.Wait(ctx, prediction) // Wait for the prediction to finish
7777
```
7878

79+
Some models take file inputs.
80+
Use the `CreateFileFromPath`, `CreateFileFromBytes`, or `CreateFileFromBuffer` method
81+
to upload a file and pass it as a prediction input.
82+
83+
```go
84+
// https://replicate.com/vaibhavs10/incredibly-fast-whisper
85+
version := "3ab86df6c8f54c11309d4d1f930ac292bad43ace52d10c80d87eb258b3c9f79c"
86+
87+
file, _ := r8.CreateFileFromPath(ctx, "path/to/audio.mp3", nil)
88+
89+
input := replicate.PredictionInput{
90+
"audio": file,
91+
}
92+
prediction, _ := r8.CreatePrediction(ctx, version, input, nil, false)
93+
```
94+
7995
### Webhooks
8096

8197
To prevent unauthorized requests, Replicate signs every webhook and its metadata with a unique key for each user or organization. You can use this signature to verify the webhook indeed comes from Replicate before you process it.

prediction.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ func (p Prediction) Progress() *PredictionProgress {
104104

105105
// CreatePrediction sends a request to the Replicate API to create a prediction.
106106
func (r *Client) CreatePrediction(ctx context.Context, version string, input PredictionInput, webhook *Webhook, stream bool) (*Prediction, error) {
107+
// Convert File objects in input to their "get" URL value
108+
for key, value := range input {
109+
if file, ok := value.(*File); ok {
110+
input[key] = file.URLs["get"]
111+
}
112+
}
113+
107114
data := map[string]interface{}{
108115
"version": version,
109116
"input": input,

0 commit comments

Comments
 (0)