Home > Back-end >  Unity connecting to Dall-E API when using image param?
Unity connecting to Dall-E API when using image param?

Time:11-07

How would one use the Dall-E text-to-image API's image and mask parameters in Unity C#?

For background, something like the following works for the other parameters like prompt (full code on GitHub):

string apiMode = "generations";
string apiUrl = "https://api.openai.com/v1/images/"   apiMode;

UnityWebRequest www = UnityWebRequest.Post(apiUrl, "");
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader("Authorization", "Bearer "   key);

string jsonString = JsonConvert.SerializeObject(aiParams, Formatting.None, serializerSettings);

www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonString));
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();

When using apiMode "edits" or "variations" though, as per Dall-E's documentation, it will return an API error suggesting to switch to content-type "multipart/form-data". How would one use this and add the binary data for image or mask, given some byte[] for the png? Thanks!

CodePudding user response:

UnityWebRequest.Post -> overload that takes List<IMultipartFormSection> will automatically use this content type header and there you can provide MultipartFormFileSection and MultipartFormDataSection sections.

The example is unfortunately useless but it would probably look somewhat like e.g.

 var jsonString = JsonConvert.SerializeObject(aiParams, Formatting.None, serializerSettings);
 var jsonBytes = Encoding.UTF8.GetBytes(jsonString);
 var formParts = new List<IMultipartFormSection>();
 formParts.Add(new MultipartFormDataSection("NameOfJSONSection", jsonBytes, "application/json"));
 formParts.Add(new MultipartFormFileSection("NameOfFileSection", yourFileContent, "YourFileName.png", "image/png"));

 using (UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", formParts))
 {
     www.SetRequestHeader("Authorization", "Bearer "   key);
     yield return www.SendWebRequest();

     if (www.result != UnityWebRequest.Result.Success)
     {
         Debug.Log(www.error);
     }
     else
     {
         Debug.Log("Form upload complete!");
     }
 }
  • Related