Using OpenAI plugin in an Android Application(Dalle API)

Preethi Rao
3 min readMay 7, 2023

The OpenAI API harnesses a variety of models that possess distinct capabilities and are available at varying price points. Moreover, for your particular use case, you have the option to carry out restricted adjustments to their fundamental models by means of fine-tuning.

There are three methods available in the Dalle Images API for interacting with images:

  • Generating new images from text prompts,
  • Editing existing images based on new text prompts,
  • Creating variations of an existing image.

I did used 2 of the APIs in my sample, lets go through them one by one

Initial Setup

To access the OpenAI APIs in my android sample applicaton, I am utilizing the openai-kotlin unofficial library(its mentioned in the official site of openai).

To begin with you need to generate a api key from the openai website,

One thing to remember as you as you log into the openai official platform website, in your account api access feature will be enabled, and it will have a default expiration time(usually 1–2 months), make sure you will write your experiment project within that time, else you might have to buy a new plan.

To start with you need to add following dependencies in your app build.gradle file

dependencies {
// import Kotlin API client BOM
implementation platform('com.aallam.openai:openai-client-bom:3.2.3')

// define dependencies without versions
implementation 'com.aallam.openai:openai-client'
runtimeOnly 'io.ktor:ktor-client-okhttp'
}

Generating new images from text prompts

You can use the following API in-order to generate a image from the user text

 openAi = OpenAI("api-token")
val imageUrl = openAi?.imageURL( // or openAI.imageJSON
creation = ImageCreation(
prompt = userText,
n = NUMBER_OF_IMAGE,
size = ImageSize.is1024x1024
)
)

Creating variations of an existing image

To create variation of existing image you need keep 3 things in mind

  • Your image should a png image
  • Size image size should be lesser than 4MB
  • Image should be square image
  • you shouldnt cross the billing limit

I have not added this example in my sample demo, cause I still found lot of issues in the api, if anyone interested please comment below, I can add it there, also the gif may not be justifying the feature, because at the right time, I had exhausted my usage limit, and this part of openAI I didn't like, as they are forcing us to pay even when we are testing their apis.

In the below gif I used a sample Birthday wish card, and using the below API it generated around 5 different versions of that birthday card, which was pretty decent, I will be trying this feature definitely in future, after this becomes stable.

val inputStream = contentResolver.openInputStream(uri)
val source = inputStream!!.source()
val imageUrl = openAi?.imageURL( // or openAI.imageJSON
variation = ImageVariation(
image = FileSource(name = uri.toString(), source = source),
n = 5,
size = ImageSize.is1024x1024
)
)

I have written a sample android application you can refer to the android application in the GitHub, Additionally, Soon I plan to experiment with editing existing images, incase of the article useful please do give a clap as It definitely took quite sometime from me to research around this, and a clap will make my day, have a nice day everyone ❤️

--

--