Skip to content

Instantly share code, notes, and snippets.

@jacobmischka
Last active January 20, 2023 19:35
Show Gist options
  • Save jacobmischka/6d2173cad16ebb5ce036a9228dce15f2 to your computer and use it in GitHub Desktop.
Save jacobmischka/6d2173cad16ebb5ce036a9228dce15f2 to your computer and use it in GitHub Desktop.
import Interval, { io, ctx } from "@interval/sdk";
import 'dotenv/config';
const interval = new Interval({
apiKey: process.env.INTERVAL_KEY,
endpoint: process.env.INTERVAL_ENDPOINT,
routes: {
image_generation: async () => {
const { prompt, n, size } = await io.group({
prompt: io.input.text('Prompt', {
multiline: true
}),
n: io.input.number('Number of images', {
defaultValue: 1,
min: 1,
max: 10,
}),
size: io.select.single('Image size', {
options: ['256x256', '512x512', '1024x1024'],
defaultValue: '512x512'
})
});
await ctx.loading.start('Generating images')
const r = await fetch('https://api.openai.com/v1/images/generations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAPI_API_KEY}`,
},
body: JSON.stringify({
prompt,
n,
size,
})
});
const data = await r.json();
await io.group(
data.data.map(({ url }, i) =>
io.display.image(`Image ${i + 1}`, { url }))
);
},
image_variations: async () => {
const { image, n, size } = await io.group({
image: io.experimental.input.file('Input file', {
allowedExtensions: ['.png'],
helpText: 'Must be a square image under 4MB',
}),
n: io.input.number('Number of images', {
defaultValue: 1,
min: 1,
max: 10,
}),
size: io.select.single('Image size', {
options: ['256x256', '512x512', '1024x1024'],
defaultValue: '512x512'
})
});
await ctx.loading.start('Generating images')
const formData = new FormData();
formData.append('image', new Blob([await image.buffer()]));
formData.append('n', n.toString())
formData.append('size', size)
const r = await fetch('https://api.openai.com/v1/images/variations', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.OPENAPI_API_KEY}`,
},
body: formData,
});
const data = await r.json();
await io.group(
data.data.map(({ url }, i) =>
io.display.image(`Image ${i + 1}`, { url }))
);
}
}
});
interval.listen();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment