Skip to content

Conversation

@gjohnsx
Copy link
Contributor

@gjohnsx gjohnsx commented Oct 27, 2023

I added a text-to-image job using the Stability AI REST API.

I wasn't sure what to do with the image in the example job so I'm just logging the huge base64 url

image

If you have any other ideas for what to do with the artifacts once returned I'm all ears!

Copy link
Contributor

@nicktrn nicktrn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gjohnsx, thanks for your contribution!

Think you forgot to link the issue. If you edit your post to add closes #47 or fixes #47 the issue can be automatically closed on merge. Makes things easier on our end! 🙏

Left some comments.

src/stability.ts Outdated
const images = await io.runTask(
"Create image from text",
async () => {
const response = await fetch(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use io.backgroundFetch() for the reasons explained here. This can be used outside of io.runTask() so we don't create an unnecessary subtask.

src/stability.ts Outdated
images.artifacts.forEach((image, index) => {
const imageUrl = `data:image/png;base64,${image.base64}`;
// Log the URL to the console
io.logger.info(`Image ${index + 1}/${payload.samples ?? 1}:`, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great example of how to correctly use cacheKeys within loops! Will be useful for people to adapt to their needs.

The only issue here is the use of forEach(). This will not work as expected, especially with real subtasks. We should probably use async iteration with for (const artifact of images.artifacts) { await ... } or alternatively await Promise.all(images.artifacts.map(..)).

@D-K-P D-K-P linked an issue Oct 27, 2023 that may be closed by this pull request
@gjohnsx
Copy link
Contributor Author

gjohnsx commented Oct 27, 2023

@nicktrn thank you very much for the tips and for the comments!

I changed the first io.runTask to io.backgroundFetch.
Then I changed the way we iterate through the returned artifacts and we use a custom cache key inside the loop. We are just returning a URL of the image in this example.

Take a look and let me know if that looks good.
image

Test payload:

{
  "width": 1024,
  "height": 1024,
  "samples": 2,
  "text_prompts": [
    {
      "text": "A lighthouse on a cliff"
    }
  ]
}

@nicktrn
Copy link
Contributor

nicktrn commented Oct 27, 2023

I changed the first io.runTask to io.backgroundFetch

Looks good! You can make things even simpler by removing the typecast and passing it like this:

io.backgroundFetch<GenerationResponse>(...)

We are just returning a URL of the image in this example

Sure, that's fine. My main concern was people adapting the previous example without realising they're creating (async) tasks within a forEach() loop. Appreciate the comment linking to the cache key docs.

@D-K-P D-K-P merged commit ea5fa79 into triggerdotdev:main Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create an example job using the Stability AI API

3 participants