Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed Mar 26, 2023
1 parent 3e43110 commit 8b3eac1
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions index.js
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))
}
}

0 comments on commit 8b3eac1

Please sign in to comment.