Replicate-Kotlin is a wrapper around Replicate’s API, enabling you to interact with cloud-based AI models using pure Kotlin code. This library is designed to easily integrate generative AI into Android and other kotlin supported environments.
- Overview
- Features
- Installation
- Usage
- Example
- Configuration
- API Reference
- Contributing
- License
- Contact
- Contributors
Replicate lets you run machine learning models with a cloud API without needing to understand the intricacies of machine learning. This client library provides a client class to interact with the Replicate API, offering methods for creating, retrieving, and canceling predictions.
- Initialize Replicate client with an auth token
- Create, retrieve, and list predictions
- Cancel predictions
- Await prediction completion
Add the following dependency to your build.gradle file:
implementation 'io.github.enyason:replicate-kotlin:0.1.0'You can initialize the Replicate client using an API token or a configuration object.
val client = Replicate.client("your-api-token")
// or
val config = ReplicateConfig("your-api-token")
val client = Replicate.client(config)Warning
Don't store your API token in code. Instead, you can explore secure storage solutions provided by your target platform (e.g. Android Secrets Gradle Plugin for Android)
To create a prediction, you need to provide a Predictable instance.
suspend fun createPredictionExample() {
val predictable = PhotoBackgroundRemover(input = mapOf("image" to dataUri))
val predictionTask = client.createPrediction<String>(predictable)
println(predictionTask.result)
}Retrieve a prediction by its ID.
suspend fun getPredictionExample(predictionId: String) {
val predictionTask = client.getPrediction<String>(predictionId)
println(predictionTask.result)
}Cancel an ongoing prediction using its ID.
suspend fun cancelPredictionExample(predictionId: String) {
val result = client.cancelPrediction(predictionId)
if (result.isSuccess) {
println("Prediction canceled successfully")
} else {
println("Failed to cancel prediction: ${result.exceptionOrNull()}")
}
}To be implemented.
fun listPredictionsExample() {
val predictions = client.getPredictions()
println(predictions)
}Awaits for the completion of a Prediction Task.
fun awaitPredictionTaskExample() {
val predictionTask = client.createPrediction<Any>(predictable)
val prediction = predictionTask.await()
println(prediction.output)
}This example removes background from a photo. For the input image, you can use a remote url or a local file. However, the file must be a bas64 encoded string as shown in the example.
suspend fun main() {
val imageFile = File(classLoader.getResource("image.jpg")!!.file)
val imageBytes = Files.readAllBytes(imageFile.toPath())
val encodedImage = Base64.encode(imageBytes)
val mimeType = "image/jpeg"
val dataUri = "data:$mimeType;base64,$encodedImage"
val client = Replicate.client("token")
val predictable = BgRemoval(input = mapOf("image" to dataUri))
val task = client.createPrediction<String>(predictable)
val result = task.await()
println(result?.output)
}You can configure the SDK behavior using the ReplicateConfig class.
data class ReplicateConfig(
val token: String,
val baseUrl: String = "https://api.replicate.com/v1"
)Reference: https://replicate.com/docs/reference/http
Contributions are welcome! Please read our contributing guidelines for more details.
This project is licensed under the MIT License - see the LICENSE file for details.
For support or questions, please contact us at enyason95@gmail.com.