Skip to content

com_cloudurable_jai_model_image

Rick Hightower edited this page Jul 16, 2023 · 1 revision

com.cloudurable.jai.model.image

class diagram

ImageResponse

The ImageResponse class is an implementation of the Response interface. It represents an image response and provides information about the created date and the URLs of multiple images. The image response is returned as a JSON object with the following structure:

{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}

The created field represents the timestamp of when the image response was created. The data field is an array of objects, with each object containing the URL of an image.

ImageResponseDeserializer

The ImageResponseDeserializer is a class responsible for parsing and deserializing JSON data into an ImageResponse object. It is used in the software to handle the conversion of JSON data into a Java object representation.

public static ImageResponse deserialize(final String json)

public static ImageResponse deserialize(final String json) {
    final ImageResponse.Builder builder = ImageResponse.builder();
    final JsonParser jsonParser = JsonParserBuilder.builder().build();
    final ObjectNode objectNode = jsonParser.parse(json).getObjectNode();
    builder.created(Instant.ofEpochSecond(objectNode.getInt("created")));
    final List<ImageResponseData> imageResponseData = objectNode.getArrayNode("data").mapObjectNode(data -> new ImageResponseData(URI.create(data.getString("url"))));
    builder.data(imageResponseData);
    return builder.build();
}

The method deserialize in class com.cloudurable.jai.model.image.ImageResponseDeserializer is used to deserialize a JSON string into an ImageResponse object.

Here is a step-by-step description of what the method is doing:

  1. It takes a JSON string as input.

  2. It creates an instance of ImageResponse.Builder using the builder method.

  3. It creates an instance of JsonParser using the JsonParserBuilder with default configurations.

  4. It parses the JSON string using the jsonParser.parse(json) method and gets the root ObjectNode from the parsed JSON.

  5. It retrieves the value of the "created" field from the root ObjectNode using getInt("created") and converts it to an Instant using Instant.ofEpochSecond.

  6. It creates an empty list of ImageResponseData objects.

  7. It retrieves the value of the "data" field from the root ObjectNode as an array using getArrayNode("data").

  8. It maps each element of the array to an ObjectNode and creates a new ImageResponseData object using the value of the "url" field in the ObjectNode as a URI. The resulting ImageResponseData objects are added to the list created in step 6.

  9. It sets the list of ImageResponseData objects in the ImageResponse.Builder using the data method.

  10. It calls the build method on the ImageResponse.Builder to obtain the final ImageResponse object.

  11. It returns the deserialized ImageResponse object.

Overall, this method parses a JSON string, extracts the necessary fields from the JSON, creates and populates the corresponding objects, and returns the deserialized ImageResponse object. sequence diagram

ImageResponseData

The ImageResponseData class represents the data of an image response.

ImageRequest

The ImageRequest class is an abstract class that represents an image request. It implements the Request interface.

CreateImageVariationRequest

The CreateImageVariationRequest is a subclass of the ImageRequest class. It represents a request for creating a variation of an image.

CreateImageRequest

The CreateImageRequest class is a subclass of ImageRequest and represents a request for creating an image. It is used to encapsulate the necessary information and parameters needed to create an image.

EditImageRequest

The EditImageRequest class extends the ImageRequest class and represents a request for editing an image.

ImageRequestSerializer

The ImageRequestSerializer class is a public class that serves as an encoder for constructing a multipart form specifically designed for an image creation request. This class is responsible for serializing the relevant information and formatting it correctly for the request.

public static MultipartEntityBuilder buildEditForm(EditImageRequest imageRequest)

public static MultipartEntityBuilder buildEditForm(EditImageRequest imageRequest) {
    MultipartEntityBuilder form = buildForm(imageRequest);
    form.addTextBody("prompt", imageRequest.getPrompt());
    form.addBinaryBody("image", imageRequest.getImageBody(), "application/binary", imageRequest.getImageFileName());
    if (imageRequest.getMaskBody() != null) {
        form.addBinaryBody("mask", imageRequest.getMaskBody(), "application/binary", imageRequest.getMaskImageFileName());
    }
    return form;
}

The buildEditForm method, defined in the com.cloudurable.jai.model.image.ImageRequestSerializer class, is responsible for generating a multipart form entity builder based on the given EditImageRequest object.

Here is the step-by-step description of what the buildEditForm method is doing based on its body:

  1. Begin by creating a new MultipartEntityBuilder object named form using the buildForm method, passing in the imageRequest object.
  2. Add a text body field to the form with the key "prompt" and the value obtained from imageRequest.getPrompt() method.
  3. Add a binary body field to the form with the key "image", the byte array obtained from imageRequest.getImageBody() method, the content type "application/binary", and the file name obtained from imageRequest.getImageFileName() method.
  4. Check if imageRequest.getMaskBody() is not null.
    • If it is not null, add a binary body field to the form with the key "mask", the byte array obtained from imageRequest.getMaskBody() method, the content type "application/binary", and the file name obtained from imageRequest.getMaskImageFileName() method.
  5. Return the form object.

The buildEditForm method essentially constructs a multipart form entity builder by adding various fields to it, such as text and binary bodies, based on the attributes of the EditImageRequest object. sequence diagram

public static MultipartEntityBuilder buildForm(ImageRequest imageRequest)

public static MultipartEntityBuilder buildForm(ImageRequest imageRequest) {
    MultipartEntityBuilder form = MultipartEntityBuilder.create();
    if (imageRequest.getResponseFormat() != null) {
        form.addTextBody("response_format", imageRequest.getResponseFormat().toString().toLowerCase());
    }
    if (imageRequest.getSize() != null) {
        switch(imageRequest.getSize()) {
            case SIZE_1024_1024:
                form.addTextBody("size", "1024x1024");
                break;
            case SIZE_256_BY_256:
                form.addTextBody("size", "256x256");
                break;
            case SIZE_512_BY_512:
                form.addTextBody("size", "512x512");
                break;
        }
    }
    if (imageRequest.getN() != 0 && imageRequest.getN() != 1) {
        form.addTextBody("n", String.valueOf(imageRequest.getN()));
    }
    if (imageRequest.getUser() != null) {
        form.addTextBody("user", imageRequest.getUser());
    }
    return form;
}

The method buildForm in the class com.cloudurable.jai.model.image.ImageRequestSerializer takes an ImageRequest object as a parameter. It creates a MultipartEntityBuilder object called form using MultipartEntityBuilder.create().

The method then checks if the responseFormat property of the imageRequest is not null. If it is not null, it adds a text body to the form with the key "response_format" and the lowercase string representation of the responseFormat value.

Next, the method checks if the size property of the imageRequest is not null. If it is not null, it performs a switch case on the size value. If the size value matches SIZE_1024_1024, it adds a text body to the form with the key "size" and the value "1024x1024". If the size value matches SIZE_256_BY_256, it adds a text body to the form with the key "size" and the value "256x256". If the size value matches SIZE_512_BY_512, it adds a text body to the form with the key "size" and the value "512x512".

The method then checks if the n property of the imageRequest is not 0 and not 1. If it satisfies this condition, it adds a text body to the form with the key "n" and the string representation of the n value.

Finally, the method checks if the user property of the imageRequest is not null. If it is not null, it adds a text body to the form with the key "user" and the value of the user property.

The method returns the form object. sequence diagram

public static String buildJson(CreateImageRequest imageRequest)

public static String buildJson(CreateImageRequest imageRequest) {
    JsonSerializer serializer = new JsonSerializer();
    serializer.startObject();
    serializer.addAttribute("prompt", imageRequest.getPrompt());
    // Add response format if available
    if (imageRequest.getResponseFormat() != null) {
        serializer.addAttribute("response_format", imageRequest.getResponseFormat().toString().toLowerCase());
    }
    // Add size if available
    if (imageRequest.getSize() != null) {
        switch(imageRequest.getSize()) {
            case SIZE_1024_1024:
                serializer.addAttribute("size", "1024x1024");
                break;
            case SIZE_256_BY_256:
                serializer.addAttribute("size", "256x256");
                break;
            case SIZE_512_BY_512:
                serializer.addAttribute("size", "512x512");
                break;
        }
    }
    if (imageRequest.getN() != 0 && imageRequest.getN() != 1) {
        serializer.addAttribute("n", imageRequest.getN());
    }
    if (imageRequest.getUser() != null) {
        serializer.addAttribute("user", imageRequest.getUser());
    }
    serializer.endObject();
    return serializer.toString();
}

The buildJson method in the ImageRequestSerializer class is used to build a JSON string based on the provided CreateImageRequest object.

Here is a step-by-step breakdown of what the method does:

  1. Create a new JsonSerializer object to start building the JSON string.
  2. Start the JSON object by calling the startObject method of the JsonSerializer.
  3. Add the "prompt" attribute to the JSON object by calling the addAttribute method of the JsonSerializer and passing the value of imageRequest.getPrompt().
  4. Check if the imageRequest.getResponseFormat() is not null.
    • If it's not null, add the "response_format" attribute to the JSON object by calling the addAttribute method of the JsonSerializer and passing the lowercase string representation of imageRequest.getResponseFormat().
  5. Check if the imageRequest.getSize() is not null.
    • If it's not null, switch on the value of imageRequest.getSize().
      • If it matches SIZE_1024_1024, add the "size" attribute with the value "1024x1024" to the JSON object.
      • If it matches SIZE_256_BY_256, add the "size" attribute with the value "256x256" to the JSON object.
      • If it matches SIZE_512_BY_512, add the "size" attribute with the value "512x512" to the JSON object.
  6. Check if the imageRequest.getN() is not 0 and not 1.
    • If it meets the condition, add the "n" attribute with the value of imageRequest.getN() to the JSON object.
  7. Check if the imageRequest.getUser() is not null.
    • If it's not null, add the "user" attribute with the value of imageRequest.getUser() to the JSON object.
  8. End the JSON object by calling the endObject method of the JsonSerializer.
  9. Convert the JSON object to a string by calling the toString method of the JsonSerializer.
  10. Return the resulting JSON string.

This method takes a CreateImageRequest object as input and generates a corresponding JSON string based on the attributes of the object. sequence diagram

Clone this wiki locally