This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
user
committed
Mar 26, 2023
1 parent
3e43110
commit 8b3eac1
Showing
1 changed file
with
35 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,39 @@ | ||
import fetch from 'chiaki' | ||
|
||
|
||
export default class Replicate { | ||
|
||
async run(model, inputs) { | ||
let prediction = await this.create(model, inputs) | ||
|
||
while (! [ | ||
'canceled', | ||
'succeeded', | ||
'failed' | ||
].includes(prediction.status)) { | ||
await new Promise(_ => setTimeout(_, 250)) | ||
prediction = await this.get(prediction) | ||
} | ||
|
||
return prediction | ||
} | ||
|
||
async get(prediction) { | ||
return await fetch(`https://replicate.com/api/models${prediction.version.model.absolute_url}/versions/${prediction.version_id}/predictions/${prediction.uuid}`) | ||
.then(response => JSON.parse(response.body).prediction) | ||
} | ||
|
||
async create(model, inputs) { | ||
const [path, version] = model.split(':') | ||
|
||
return await fetch({ | ||
hostname: 'replicate.com', | ||
path: `/api/models/${path}/versions/${version}/predictions`, | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ inputs }), | ||
}) | ||
.then(response => JSON.parse(response.body)) | ||
} | ||
export default { | ||
run: async function(model, inputs) { | ||
let prediction = await this.create(model, inputs) | ||
|
||
while (! [ | ||
'canceled', | ||
'succeeded', | ||
'failed' | ||
].includes(prediction.status)) { | ||
await new Promise(_ => setTimeout(_, 250)) | ||
prediction = await this.get(prediction) | ||
} | ||
|
||
return prediction.output | ||
}, | ||
|
||
get(prediction) { | ||
return fetch(`https://replicate.com/api/models${prediction.version.model.absolute_url}/versions/${prediction.version_id}/predictions/${prediction.uuid}`) | ||
.then(response => JSON.parse(response.body).prediction) | ||
}, | ||
|
||
create(model, inputs) { | ||
const [path, version] = model.split(':') | ||
|
||
return fetch({ | ||
hostname: 'replicate.com', | ||
path: `/api/models/${path}/versions/${version}/predictions`, | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ inputs }), | ||
}) | ||
.then(response => JSON.parse(response.body)) | ||
} | ||
} |