Mastering ComfyUI: Getting started with API - TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome back to Dreaming AI my name is n and today we are going to learn how to use the Ki apis uh first off I want to mention that to follow this tutorial you'll need some programming Basics as we'll be using both comfy UI and python for interfacing so let's start with an example that's already been set up provided by the author of comfy UI actually he provides two examples but I've chosen the one that in my opinion is more complete you can find this example in the uh script examples folder and here it is uh let's copy it somewhere else so that we can work on the code comfortably and create our tiny application so firstly if you don't have python installed you can easily install it by downloading the installer from the website if you're using Linux you might even have it pre-installed additionally if you're using the portable version of comfy UI like mine you can also use the python that comes within this environment since it's a full-fledged installation the only difference is that the libraries are not installed somewhere on your computer but within the virtual environment in this case the only dependency package you need should be the web socket which can be installed with the following line of code for the portable environment or with another line if you're in a normal python environment for this example I'll use the portable one let's open the script to see how the API interfacing works and how to avoid making some mistakes firstly we can see that the workflow code is already included however we want to load the workflows directly from the Json files so we'll immediately delete this part of the code and replace it with another part that will directly load the files subsequently since the workflows are in Json format this library is used to correctly interpret their content by python uh now Point number one you should not and cannot use regular workflows in fact to create a workflow compatible with the apis you need to save it from comfy UI in API format to do this just click on this button instead of the normal saves if you don't have it just go to settings and enable Dev mode options and then proceed with saving the workflow we're going to use contains 2K Samplers in which I've shared both the prom and the seed I did this Solly for the purpose of this example since it wouldn't normally make much sense great now let's save it in API format and open it again in comfyi uh initially you'll notice that the seed node has been automatically detached and placed directly into the main blocks while the prompt connections remain intact this aspect is crucial to understand the comfy UI API format doesn't support the conversion of a regular field into an input field when you save the workflow in API format the field reverts to its original state potentially disrupting any connections previously established through it however this is isn't a significant issue for us since while working with programming language we can directly use real variables to replicate the original behavior of our workflow another thing you need to pay attention to is the titles of the nodes if you have two or more nodes where you know you need to bury some parameters during programming I strongly suggest changing the titles so that they are recognizable later on if this aspect is not clear to you don't worry because we'll see it in just a moment in my case I changed the title of the text clip in code so that I can understand later which one is positive and which one is negative okay uh let's save our workflow again in API format and open it okay now for convenience I'll open it here on the side so that you can see both the script and the workflow let's move on to modifying the fields of a work work for directly from code as you can easily see the nodes are divided by numerical Index this is why we can refer to them by that number the next value is the implo subgroup which you'll practically enter every time and then the third value represents the name of the field we want to modify which you can find within the input group that's it theal need laborious thing is to find out which index corresponds to the node you want to modify the input values for this is why I mentioned earlier to change the title of these nodes because if you have two with the same title it would be unnecessarily difficult to figure out what's why now following these precautions we can proceed to modify the prompts and the seed by sharing the seed for both K samplers great now the image saving function has already been done by the author of this example so we won't recreate it uh by looking at how it works in detail we can understand that it uses get requests to The Prompt endpoint to understand when the workflow has finished its execution and then starts requesting the output of the jobs reported in history from view however to see the result of our workflow we need to uncomment this part of the code AS written in the example well let's try running this workflow as I mentioned I'll be using the portable environment to run it so I'll use the full path of the python.exe contained in Python embedded as The Interpreter excellent now that we know the basics let's move on to something that's not explained let's say we want to do an image to image or a video to video nowhere is it explained how to upload images or videos and use them within the apis actually no nodes like load image work by using the file name inside the input folder or in a subfolder within it so we just need to find a way to upload and receive the name comfy UI Gibbs to that file to then use it as a value for the field I did some reverse engineering of comfy UI and in the end I managed to create this function starting from the way the upload endpoint was built with this function we can upload any image even in a subfolder and avoid com youi creating duplicates in case we're always using the same image by setting the overwrite to true so let's do a test uh for this explanation it seemed perfect to use the example workflow posted on the comfy UI repository but save it in API format and set the title of the two clips first to make it easier to locate in the workf code for the example image we'll use this one that I'll put here in the same folder as the script I will now change some parameters of the workflow like the model and the prompts so that we can get a better result well as you can see the image has been copied correctly into the input folder and the script has produced a correct Outlet instead of displaying the output this way you can also Implement a function that saves the images to disk like this and that's it personally I find the possibility of reusing my workflows to create real projects wonderful I already have some ideas in mind that I would like to implement as soon as I can uh in the meantime you can find the script I used in the description please consider liking and subscribing if you found this tutorial useful also if you have any questions please let me know in the comments below I'll be happy to help you out as much as I can and until next time keep [Music] dreaming
Info
Channel: DreamingAI
Views: 3,934
Rating: undefined out of 5
Keywords: comfyui, api, python, Img2Img, upload image, generate images, basic, texttoimage, AI, stable diffusion, artificial intellingence, dreamingai, ai news, best free ai, best ai model, dreamingai tutorials
Id: va8Jkc7o9d4
Channel Id: undefined
Length: 11min 13sec (673 seconds)
Published: Sun Feb 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.