Last active
November 8, 2022 15:22
-
-
Save franjohn21/67db4d43a345355114bf1bf1d3ac1999 to your computer and use it in GitHub Desktop.
TS code to interact with Astria API
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
import { FormData } from 'formdata-node'; | |
import fetch from 'node-fetch-commonjs'; | |
// set to `false` to use production mode | |
const USE_FREE_TIER = true; | |
const ASTRIA_API_KEY = ''; | |
const train = () => { | |
const formData = new FormData(); | |
formData.append('tune[title]', '<title here>'); | |
formData.append('tune[name]', '<name here>'); | |
if (USE_FREE_TIER) { | |
formData.append('tune[branch]', 'fast'); | |
} | |
formData.append( | |
'tune[callback]', | |
`https://api.yourdomain.com/train/complete/for/${id}` | |
); | |
const images = [ | |
'https://thispersondoesnotexist.com/image', | |
'https://thispersondoesnotexist.com/image' | |
// add images here ... | |
] | |
for (let i = 0; i < images.length; i++) { | |
const image = images[i]; | |
const response = await fetch(image); | |
if (!response.ok) { | |
throw new Error(`Unable to download image: ${image}`); | |
} | |
const blob = await response.blob(); | |
formData.append('tune[images][]', blob); | |
} | |
const response = await fetch('https://www.astria.ai/tunes', { | |
method: 'POST', | |
body: formData, | |
headers: { | |
Authorization: 'Bearer ' + ASTRIA_API_KEY, | |
contentType: 'multipart/form-data', | |
}, | |
}); | |
if (response.status >= 400 && response.status < 600) { | |
throw new Error( | |
'Failed to create tune. Received status code: ' + response.status | |
); | |
} | |
const tune = (await response.json()) as { id: string; status: number }; | |
// do something with the tune | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment