How to Create AI Images From Google Sheets

About this Tutorial

Use the Open AI API and Dall-e-3 to create image in Sheets

Video Transcript

00:00 Hey, so in this video we're gonna try to create the code in Apps Script to create images from DALI 3.
00:07 We have this code here from OpenAI, and we already got our API key. Uh, this is not using ChatGPT, it is using actually just the OpenAI API.
00:21 Uh, this is a curl command here, but we're going to change that and edit it a little bit. But we also have our original.
00:30 Original text creation API here, um, we're gonna copy this to the clipboard, and we're gonna try to use this and edit it down to just creating images.
00:42 Let's see if this works. So we're gonna do extensions Apps Script. By the way, bettersheets.co.slash snippets is where you can get this kind of code.
00:51 If you want to just get the code, we're going to paste it into our Apps Script here. So we have a sheet called API key.
01:00 Yes, I'm gonna actually call this API key lowercase. So it matches this. Our role data. So our model looks like we just need to edit the model to be Dolly three.
01:13 There we go. It looks like we need to also, let's go back here. We just need a prompt only. me.
01:31 Looks like. So this message is prompt. I think we can just rename this prompt and then put in text here prompt.
01:43 We don't need temperature. We don't need max tokens. We don't need top key. We don't need any of this other stuff.
01:50 So let's look at that again. We want n one n size. So n one and the size. Let's just make it square.
02:00 The same one. And they have here. Great. We need to do the post method. We're going to use headers bearer API key and that's the same.
02:13 Authorization bearer API key. Same thing. And our answer response is going to be some text because it looks like we're going to generate a URL.
02:25 Um. It says here. Somewhere, where you can request one image at a time with Dolly three. Ah, yeah. By default, images are generated at standard quality, but we could do HD if we wanted to.
02:45 Um. Yeah, let's see what happens here. So instead of putting a prompt here, I'm going to actually just for testing purposes put prompt here equals.
02:59 And say, uhh, uhh, an illustration of a Google Sheet on a screen on the beach. Let's see how that works.
03:13 So we're going to hit run and see what happens. If we get any errors, we need to authorize, of course.
03:21 Let's do that. And we should be getting, er, you're not allowed to sample from this model in valid request error.
03:37 Hmm. Let's try deli2. Maybe it is a problem with the model. Right. Not allowed to sample from this model. In valid request error primers.
03:52 We need to, let's see. I just realized our URL is what's wrong. Because this error is a . There is that you're not allowed to sample from this model, but our Fitch is chat completions.
04:06 So we just change it to api.openai.com.v1 images generations. Now it should work. Let's run it again. We should be getting a URL.
04:20 Yeah, now it's taking a little bit of time, so that's good. So we're gonna get the content of this JSON and see what happens.
04:28 Can I read properties of undefined? So let's see. . Let's just do this, JSON, return JSON, and see what we get.
04:40 In order to see it, we should have it logged. So we just change it to logger.log instead of return. And let's see what happens.
04:50 So this is the debugging process. We're gonna go through each and every step of this in this video. There we go.
04:56 We have databack. Uh, we have a revised prompt. And And we're looking for a URL. Here's the URL. Right there.
05:06 Until. Right here. I think. There it is. That's exactly what we asked for. Cool. So we just need to grab this URL from this JSON.
05:20 So we're gonna do JSON. I think it's, um. I think we're gonna use JSON.data.0 because it's gonna be the first option in the data package.
05:31 . . . and then .url. So let's run it and see what we get. And there we go. We get our URL. This is probably a new URL.
05:40 Yeah, but it's a similar generation. Similar image. Cool. So now we're creating images here. We want to return this. Or, uh, in fact, what we may want to do is create a little bit of a image generation.
05:54 Enter a prompt and get the image back. So let's go back to our sheet. Sheet one. We're gonna write. Prompt here and put in a little spot for our prompt.
06:07 It's going to be this text box. And then we'll get our responses. So we'll put in a page here prompt and response.
06:17 Or URL, actually. So B2 will be our prompt. So we're gonna get that value from here. Prompt is spreadsheet app.getactive.
06:31 of spreadsheet get sheet by name. Be sheet one for this purpose dot get range. I think it was what? B2.
06:47 Get value. There you go. Value. So that's our prompt. And what we want to do is variable image URL. or else.
07:01 equals this JSON dot data dot URL. We also may want to get the uuhm, uuhm, other thing here, the URL, there's also revised prompt.
07:16 So variable revised. What's going on is that any time we ask Deli, uh, for the prompt, it's gonna revise it, it's gonna rewrite it, it's gonna add some more detail if we can always undo that if we want, but for this.
07:31 Let's just get, that and see what it looks like, zero data dot revised prompt. And now we are gonna go back to our sheet.
07:44 Insert row before, and we're just gonna do it before for. And now we'll always have row four. So I'll say spreadsheetapp dot.
07:59 Actually we'll rename this, to variable sheet responses equals this. Sheet responses. Let's bring it all the way up.
08:16 And now we can do sheet responses dot. Uhm, not a pen row. Uh, get range. Row four, number one, is going to be our prompt.
08:37 . . . . . . Set Value, Prompt, now, let's do that all again except we want the URL in the second column. Let's Let's do a timestamp as well, new date in the third column, and in the fourth column, we'll put the revised prompt.
09:09 Make sure this is image URL, there we go. So all in the fourth row, we'll do timestamp and revised prompt.
09:20 Let's delete everything else. We can even view gridlines there. Create an image of a cat working on Google. . And let's take this and run it, and let's see what happens.
09:40 This is so exciting. So basically the idea is, take this prompt, run it through AI, when you get the response back, insert in a row, uh, here in the fourth row, and then put all of our information there.
09:55 Awesome. Is this the URL? Now that's cool. That's cool. Cat working on Google Sheets. Sweet. And we can see, this is much more detailed.
10:10 It's a fluffy orange, it picks a color, create an image of a cat working on Google Sheets. What we may want to do as well is delete B2 once we're done with it, because we're reputting it in prompt.
10:25 So let's take that. And once we put it in the prompt there we will, uh, . delete a B2. Clear content.
10:37 There we go. And so now we've created essentially a chat GPT here just using the API, creating images in Google Sheets.
10:44 Uh, this is really cool. Create a pug puppy working on Google Sheets. From the pound. If we want to, we could create a function on open.
11:12 And we can create, if we go to our snippets. Let's get this function here. here.
11:26 AI menu and we'll run AI. Let's save this. We're going to refresh our page once it's saved. Refresh our page.
11:40 And what's going to happen is we're going to get a little menu up here. AI menu. Oh, we need to rename that first item.
11:47 Let's go back to do that. Create an image. We'll rename it to instead of first item. We can also rename this from AI.
11:58 AI image. Let's save that. Close it. Refresh. Now we'll have an AI menu. Create an image. It'll go from B2.
12:14 Oh, I think we have to authorize it again. Now we have new stuff. Allow. This is really cool. We'll be able to do this in just a few minutes, right?
12:24 Oh, this is going to be awesome. Running and just to make sure it's running. I mean, Mmm. That was fast.
12:34 Maybe it was the first one running. Oh, that's a cute pug puppy. From the pound. It's a lot of puppies there.
12:45 Cool. So, thanks for watching. Uh, you can get this. Uh, snip it. I'll put this snip it, uh, over at bettersheets.co.slash.snippets if you want just the code itself.
12:57 Bye. Oh, and um, also if you remember down below, you get this, uh, sheet, . Exactly as it is, I'll delete the API key by the time you watch this.
13:06 Bye.