Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.firebase.ai.ondevice

import android.graphics.Bitmap
import com.google.firebase.ai.ondevice.interop.Candidate
import com.google.firebase.ai.ondevice.interop.CountTokensResponse
import com.google.firebase.ai.ondevice.interop.FinishReason
Expand All @@ -25,15 +24,14 @@ import com.google.firebase.ai.ondevice.interop.GenerateContentResponse
import com.google.mlkit.genai.prompt.GenerateContentRequest
import com.google.mlkit.genai.prompt.ImagePart
import com.google.mlkit.genai.prompt.TextPart
import kotlin.math.min

// ====================================
// `Part` converter extension functions
// ====================================
internal fun com.google.firebase.ai.ondevice.interop.TextPart.toMlKit(): TextPart = TextPart(text)

internal fun com.google.firebase.ai.ondevice.interop.ImagePart.toMlKit(): ImagePart =
ImagePart(downsizeBitmapIfNeeded(bitmap))
ImagePart(bitmap)

// ============================================
// `CountTokens*` converter extension functions
Expand Down Expand Up @@ -89,24 +87,3 @@ private fun generateContentRequest(
builder.init()
return builder.build()
}

private fun downsizeBitmapIfNeeded(bitmap: Bitmap): Bitmap {
val IMAGE_SHORTER_DIMENSION_MAX_VALUE: Int = 768
val width = bitmap.width
val height = bitmap.height
val shorterDimension: Int = min(width, height)
if (shorterDimension <= IMAGE_SHORTER_DIMENSION_MAX_VALUE) {
return bitmap
}

val scaleFactor = (IMAGE_SHORTER_DIMENSION_MAX_VALUE.toDouble()) / shorterDimension

val newWidth = (width * scaleFactor).toInt()
val newHeight = (height * scaleFactor).toInt()

val resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, /* filter= */ false)
if (resizedBitmap != bitmap) {
bitmap.recycle()
}
return resizedBitmap
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ plugins {
alias(libs.plugins.protobuf) apply false
alias(libs.plugins.errorprone)
alias(libs.plugins.crashlytics) apply false
id("org.jetbrains.kotlin.android") version "2.1.21" apply false
id("PublishingPlugin")
id("test-report")
id("firebase-ci")
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ firebaseAnnotations = "17.0.0"
firebaseCommon = "22.0.1"
firebaseComponents = "19.0.0"
firebaseCrashlyticsGradle = "3.0.4"
genaiPrompt = "1.0.0-alpha1"
genaiPrompt = "1.0.0-beta1"
glide = "5.0.5"
googleApiClient = "2.8.1"
googleServices = "4.3.15"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/**
Expand Down Expand Up @@ -58,6 +61,14 @@ class FirebaseAndroidLibraryPlugin : BaseFirebaseLibraryPlugin() {
project.tasks.withType<KotlinCompile> {
kotlinOptions.freeCompilerArgs = listOf("-module-name", kotlinModuleName(project))
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.languageVersion = KotlinVersion.KOTLIN_2_0.version
}

project.afterEvaluate {
val kotlinExtension = project.extensions.findByType(KotlinProjectExtension::class.java)
if (kotlinExtension != null) {
kotlinExtension.coreLibrariesVersion = "2.0.21"
}
}

project.apply<DackkaPlugin>()
Expand Down
Loading