Use Gemini API inside android for a Question Answering task.

George Soloupis
3 min readMar 2, 2024

--

Written by George Soloupis ML and Android GDE.

In this blog post, we will showcase the practical implementation of the Gemini API within an Android application for a Question and Answer (QnA) task. Our demonstration will utilize an existing application designed to support offline functionality, specifically tailored to accommodate the BERT model with 512 token inputs in the form of a TensorFlow Lite file. This application is finely tuned to seamlessly integrate both offline and online capabilities, enabling a comprehensive comparison between the offline BERT model and the powerful Gemini AI model.

We will not analyse how the BERT model is working inside the android app since this has been explained at an older post here but rather we will focus on the ease of use of the Gemini SDK for Android. The initial step involves acquiring an API key, which will be included with every API call (API is not working without it and you will get an exception/crash of the application). You can use this link to get one. Before proceeding read the documentation about which regions are supported at the moment and if your country is not in the list then try to use a VPN.

The necessary dependency is required inside the build.gradle.kts file:

dependencies {
// ... other androidx dependencies

// add the dependency for the Google AI client SDK for Android
implementation("com.google.ai.client.generativeai:generativeai:0.2.0")
}

Then inside the Activity, Fragment or ViewModel you can create the model as:

val generativeModel = GenerativeModel(
// For text-only input, use the gemini-pro model
modelName = "gemini-pro",
// Access your API key
apiKey = API_KEY
)

With the above lines of code you are ready to implement common use cases as:

We will focus on QnA task and we will send to the API the context and the question we would like to ask, for example:

Context: “Nikola Tesla (Serbian Cyrillic: 10 July 1856–7 January 1943) was a Serbian American inventor, electrical engineer, mechanical engineer, physicist, and futurist best known for his contributions to the design of the modern alternating current (AC) electricity supply system.”

Question: “When was Nikola Tesla born?”

The call will be like:

val returnedText = generativeModel.generateContent("Nikola Tesla (Serbian Cyrillic: 10 July 1856–7 January 1943) was a Serbian American inventor, electrical engineer, mechanical engineer, physicist, and futurist best known for his contributions to the design of the modern alternating current (AC) electricity supply system. When was Nikola Tesla born?")

And from the response we can get the text and show it on the text field:

Responses from both implementations.

Above you can see the the response of the Bert model highlighted with yellow color inside the context and the response of the Gemini Pro API at the bottom text field.

Another example:

With yellow the Bert result and at the bottom the response from the API.

You can build and deploy the application from this GitHub repository (specific branch).

Conclusion
Integrating the Gemini Pro API into an Android application is remarkably straightforward. Begin by obtaining an API key, which must be included in each API call. Once obtained, you can seamlessly query the API with contextual information to pose questions. Furthermore, the demonstration application features an offline implementation of the BERT model capable of processing 512 token inputs.

--

--

George Soloupis
George Soloupis

Written by George Soloupis

I am a pharmacist turned android developer and machine learning engineer. Right now I work on accessibility on Zolup browser and I am a ML & Android GDE.

No responses yet