ACF Library

sendRequestImageEdits

Back to List

Description: Send a request to the image/edits openAI endpoint. Doing image edits.

FileMaker Prototype:

Set Variable [$res; ACF_Run("sendRequestImageEdits"; string_imagePath;  string_prompt;  string_size)]

Category: OPENAI

Dependencies

  • GetExtentionFromPath: Retrieve the file type (extention) from a filename or a full path. NOTE: put USE bootstrap; below the package header instead of copying the code for GetExtentionFromPath
  • FormAttachement: Create File Part for multipart/formdata API request
  • FormField: Create field part in Multipart/Formdata request
  • GetNameFromPath: Retrieve filename part from a full path

NOTE: This function serves as a reference template. To use it effectively, you may need to adjust certain parts of the code, such as field names, database structures, and specific data lists to fit the requirements of your system. This customization allows the function to work seamlessly within your environment.

Function source:

function sendRequestImageEdits ( string imagePath, string prompt, string size )
   
   // Produce alpha mask
   string mask;
   if ( ImageEdits::MaskPath != "") then
      mask = ImageEdits::MaskPath;
   else
      mask = produceAlphaMask (imagePath); 
   end if

   string url = "https://api.openai.com/v1/images/edits";
   // format the post data as multipart form data 
   string vb1 = "--------------------WebKitFormBoundaryvaBIACFqQXnhryXmJxO";
   string vb2 = "--" + vb1 + "\r\n";
   string vb3 = "--" + vb1 + "--\r\n";
   int x; 
   
   
   string APIkey = ExecuteSQL ("SELECT AI_APIKey FROM Preferences"); 
   
   string hdrs = format ( "Content-Type: multipart/form-data; boundary=%s",vb1 )+
   "\nAuthorization: Bearer "+APIkey;
   
   string postData = FormAttachement (imagePath, "image", vb2, false ) + 
   FormAttachement (mask, "mask", vb2, false ) + 
   FormField ( vb2, "model", "dall-e-2") +
   FormField ( vb2, "prompt", prompt) +
   FormField ( vb2, "size", size) +
   // FormField ( vb2, "image", Content) +
   FormField ( vb2, "n", "1") +
   FormField ( vb2, "response_format", "b64_json") + vb3;
   
   // print postData + "\n"; 
   string respons = HTTP_POST ( url, postdata, hdrs);
   string err, res,restxt, b64data, filecontent, filepath, imgtype, origext; 
   
   // print respons; 
   // return JSON ("xx","xx");
   if (left(respons,1) != "{") then
      return JSON ("response",respons); 
   end if
   JSON resp; 
   resp = respons; 
   if ( list_keys (resp) == "error") then
      err = resp["error.message"];
      print "\nError responce: " + err; 
      alert (err);
      return JSON ( "Success", 0, "message", err); 
   else
      print "="*60+"\n"+respons+"\n"+"="*60;
      // Handle the result
      restxt = "No restext for this api"; // resp["choices[1].message.content"];
      b64data = resp["data[1].b64_json"];
      imgtype = "png";
      origext = GetExtentionFromPath ( imagePath); 
      filecontent = BASE64_DECODE ( b64data); 
      filepath = documents_directory() + "images/" ;
      res = create_directory ( filepath); 
      filepath += substitute(GetNameFromPath(imagePath), "."+origext,"")+"_processed."+imgtype;
      x = open (filepath, "wb"); 
      write ( x, filecontent); 
      close (x); 
      return JSON ( "Success", 1, "content", restxt, "imagePath",filepath ); 
   end if
end

The sendRequestImageEdits Function sends a request to the image/edits endpoint DALL-E-2.

  • ImagePath is the path to a Square image, 256x256, 512x512 or 1024x1024 sized image. It needs to be in PNG format. To convert any image to this spec, use the resizeAndConvert2PNG function in this library.
  • The prompt is the instructions to the API about what needs to be done.
  • Size is the target size of the image, one of the sizes mentioned above.
  • ImageEdits::MaskPath is a path to the Alpha mask to use. If it's empty, it calls produceAlphaMask to make the whole image editable. This could be added as a parameter to this function instead of using relationship graph dependency.

Example

Set Variable [$res; ACF_Run("sendRequestImageEdits"; string_imagePath;  string_prompt;  string_size)]

It is an experimental function, and I have not yet come up with any examples for its use.

It remains to get some experience about what this API can do.

Back to List