How to generate images with OpenAI's DALL-E models using R - reticulate approach

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll learn how to use openai's Dolly model to generate images using R if you find this video helpful please make sure you subscribe to my channel so that I can keep making videos like this as always the code from this tutorial is available on my GitHub which I'll link to in the description let's take a quick look at the openai documentation for this API so this is the openai page for using their images API which allows you to interact with their Dolly models if I use this drop down here we can see that there's examples for Python nodejs and curl there's not direct support for R but through the reticulate package we can write python code in R and access this API here's an example of what the python code looks like for generating an image and here we can see that we're going to need the image function from the openai python package the first thing that we're going to do is load the reticulate package and then we're going to import the openai python package into our R environment we'll do this by using the import function from the reticulate package and we're going to assign this to an object called open AI if I run this line of code eventually the open AI object will populate in my environment Pane and then we can access whatever python functions we want from that package if I try to access the functions that are available through the package that we just loaded image isn't actually one of the ones that shows up for me and if that happens to you it's because the version of python that you have installed does make a difference so the version of python that I currently have installed by default here on my computer doesn't actually have access to the images API so what I'm going to do from here is I'm going to create a virtual environment where I install a more up-to-date version of python and then I'm going to access the openai images API through that virtual environment so to do that I'm going to use the virtual n underscore create function from the reticulate package and then I'm going to create a name for that environment and I'm going to call it open AI because that's the reason that I'm creating this virtual environment and then I need to specify the version of python that I'd like to install into this virtual environment and I'm going to use version equals 3.10 Point latest and if I run that sorry that should say a colon there instead of a DOT if I try re-running that that's going to start installing that version of python into that virtual environment note that in order to do this you do need to already have this version of python installed on your machine and if you get an error when you try to run line two here what you can do first is run install Python and then put the version that you would like to install so I don't need to run this line of code because I've already run that before on my machine but if you don't already have that version of python installed on your computer then you will need to run that before you can create that virtual environment using that version of python and once that virtual environment is done being created we're also going to need to install the opening AI python package into that virtual environment we'll do that by using the virtual end underscore install function which is again from the reticulate package and we start by specifying the name of the virtual environment that we'd like to install the package into which is going to be this open AI environment that we just created and next we're going to tell the function which package we'd like to install into that environment and that's going to be the openai package which is a python package so I'm going to run line four here to install that python package into that virtual environment that we just created and I'm just going to restart our studio and that's because I already had the reticulate package loaded but this time before I load the reticulate package I want to start by specifying that I want the reticulate package to be loaded using that virtual environment that we created so I'm going to use the use Virtual EnV function and that's coming from the reticulate package but I'm going to specify that before I actually load the reticulate package because if I go ahead and I just load that package what it's going to do is it's going to try to find the default version of python that's installed but that's not the version of python that we want to use we actually want to load the reticulate package using that virtual environment that we created so that we have access to that images API inside of that function I'm just going to type the name of the virtual environment that we want to load reticulate with and then if I run that line of code now reticulate knows that that's the python installation that I want to use so I can go ahead and load reticulate now and this time if I import the openai python package into R it's now going to be looking for the open AI package inside of that virtual environment that we created and that version of the package should be compatible with the openai images API so now this time if I try to access the image function on that package it is available let's go back to the documentation so we need to access the create method from image and then there's a few arguments here that we'll need to enter so what I'm going to do is I'm actually just going to copy and paste this into our studio just to make my life a little bit easier I'm going to comment it out and then I'm just going to convert all of this python code into our code so here we're creating an object called response we're going to need image and then create from the openai python package and then inside of that function the first argument is the prompt so here is where we're going to type in the description of the image that we want the dolly model to create for us so let's type a white siamese cat here it says that the N parameter controls how many images you'd like to generate so for now let's just start by generating one and then the size argument is going to determine how big of an outputted image we want to create and the size that we choose here is going to impact how expensive it is to generate this image so let's start with 1024 by 1024 and if we take a look at the pricing for openai we can see that the images get more expensive as they get higher in resolution so generating this image is going to cost me 2 cents but you can reduce that amount a little bit if you're willing to have some lower resolution images if I were to just try to run this it wouldn't work right off the bat and that's because of the fact that it does cost money so in order to run this you'll first need to make sure that you have an openai account and that you have set up some sort of payment method or if you're using your free credits this should hopefully work as well if you haven't set up an account before it's fairly easy if you just Google for openai API you can either sign up or you can log into an existing account and then just select API and you'll need an API key in order to interact with this API so if you click your name and then go to view API Keys you can create a new secret key and give it a name so all the I'll call this my dolly key and then if I click create secret key it's going to generate that key for me make sure that you copy and paste it onto your clipboard you won't be able to see this code again so if you lose it you will have to generate a new one make sure that you never post your API key anywhere online it's a violation of the terms of service and people can charge money to your account you'll also need to make sure that you go to your billing settings and that you've added some sort of payment method if you don't still have any free credits remaining once you have that key you're going to need to add it as an environment variable so if you're on Windows you can just search for environment and then there is an option to edit the system environment variables and then under Advanced go to environment variables and then you'll want to click new and you want to type openai underscore API underscore key in all caps and then under variable value you're going to copy and paste that API key that you just got from the openai website once you've entered it you can click OK and then you can click OK again and click OK one more time and if all of that worked properly you should now be able to run this line of code and generate the image you might need to close and restart our studio again in order for that to work I'm just going to rerun these lines and then when I tried to run that I got an error and the reason for this error is just because there's differences between R and python when it comes to data types so the argument n here is expecting an integer in Python but we passed a numeric value rather than an integer value so to force this to become an integer we can just add the letter L after the value and this will now be passed to reticulate as an integer rather than as a numeric value so we should be able to run that just takes a couple seconds to generate the image and then if we want to be able to actually access that image there's just one final step here and this is what that last step looks like in Python but if we want to convert that to our code we're going to access the data element of the response list and then we want to access the first element of that so I'll use double brackets to access the first element of that list and then from there we need to access the URL element if I run that I get this super long URL outputted into the console and then I can copy and paste that URL into a web browser and there's the image that was generated from here I can download and save that image which you'll need to make sure that you do because the URLs aren't permanent they actually expire after one hour and that's really all there is to it there's some other options available for using this API that are described in the documentation so for example you can edit an existing image or you can also create variations of images that have already been created thank you so much for watching today's video I really hope you enjoyed it if you found it helpful please make sure to like this video And subscribe to my channel so that I can keep making more of these
Info
Channel: Melissa Van Bussel (ggnot2)
Views: 1,760
Rating: undefined out of 5
Keywords: dalle, dall-e, dall-e 2, dall-e-2, openai, OpenAI, dall-e R, WALL-E, WALL-E-2, ChatGPT, ChatGPT in R, GPT3.5, GPT-4, GPT-3, generative AI, Open AI, OpenAI Images API, reticulate, dall-e python, ggnot, ggnot2, R Studio, RStudio, posit, install_python, virtualenv_create, gptchatteR, GPTStudio, gptstudio, openai$Image$create, openai.image.create, use_virtualenv, melissa van bussel, RLadies, R-Ladies, RPyLadies, generate images in R, image generation R, DALL-E 2, DALL, DALL·E 2, DALL·E, synthetic data generation
Id: C7fUUdmu_XA
Channel Id: undefined
Length: 11min 50sec (710 seconds)
Published: Mon Jun 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.