- Add GeminiCloudModel: proxies inference to Gemini 2.0 Flash via HTTPS using HttpURLConnection (no new deps). Supports both blocking and SSE streaming. Works from any context — background, emulator, CI. - Fix background inference: OnDeviceModel.create() now accepts an apiKey; when set, GeminiCloudModel is selected immediately, bypassing Gemini Nano's foreground-only restriction. ApiServerService reads the key from SharedPreferences at startup. - Add API key UI: password field in MainActivity saved to SharedPreferences before the service starts; disabled while server is running. - Add GitHub Actions CI (.github/workflows/ci.yml): build job produces a debug APK artifact; test job spins up a KVM-accelerated Android 31 emulator, installs the APK, writes the GEMINI_API_KEY secret into SharedPreferences via adb run-as, starts the service, and runs curl assertions against /health, /v1/models, /v1/chat/completions, and the SSE streaming endpoint. - Add release workflow (.github/workflows/release.yml): triggered on v* tags, builds the APK and creates a GitHub Release with it attached. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
102 lines
4.0 KiB
Kotlin
102 lines
4.0 KiB
Kotlin
package com.pixel10.ai.inference
|
|
|
|
import android.content.Context
|
|
import android.util.Log
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.withContext
|
|
|
|
/**
|
|
* Unified interface for on-device AI inference on the Pixel 10.
|
|
*
|
|
* Supports three backends, tried in order:
|
|
* 1. **Gemini Nano** via ML Kit Prompt API — uses the system-managed model
|
|
* through AICore, accelerated by the Tensor G5 TPU. Zero setup needed
|
|
* on supported Pixel devices. Requires foreground context.
|
|
* 2. **Gemini Cloud** — proxies to Gemini 2.0 Flash via HTTPS. Works from
|
|
* any context (background, emulator). Requires an API key.
|
|
* 3. **MediaPipe LLM** — for custom open-weight models (Gemma 2B/3n, etc.)
|
|
* that you supply yourself. Place the .bin/.task file in the app's
|
|
* files directory.
|
|
*
|
|
* Pass [apiKey] to enable the cloud backend. If an API key is provided,
|
|
* cloud is preferred over Nano to guarantee background operation.
|
|
*/
|
|
interface OnDeviceModel {
|
|
val backendName: String
|
|
val isReady: Boolean
|
|
|
|
suspend fun generate(
|
|
prompt: String,
|
|
maxTokens: Int = 1024,
|
|
temperature: Float = 0.7f
|
|
): String
|
|
|
|
suspend fun generateStreaming(
|
|
prompt: String,
|
|
onToken: (String) -> Unit
|
|
): String
|
|
|
|
fun close()
|
|
|
|
companion object {
|
|
private const val TAG = "OnDeviceModel"
|
|
|
|
/**
|
|
* Create the best available model.
|
|
*
|
|
* Fallback chain:
|
|
* 1. GeminiNano — on-device, best quality, foreground only
|
|
* 2. GeminiCloud — network, always works (background + CI); needs [apiKey]
|
|
* 3. MediaPipe — local model file, fully offline
|
|
*
|
|
* If [apiKey] is non-empty, cloud is tried *before* Nano so the server
|
|
* stays responsive after the user navigates away from the app.
|
|
*/
|
|
suspend fun create(context: Context, apiKey: String = ""): OnDeviceModel =
|
|
withContext(Dispatchers.IO) {
|
|
val hasKey = apiKey.isNotBlank()
|
|
|
|
// Prefer cloud when an API key is available — guarantees background operation
|
|
if (hasKey) {
|
|
Log.i(TAG, "API key set — using Gemini Cloud for background-safe inference")
|
|
return@withContext GeminiCloudModel(apiKey)
|
|
}
|
|
|
|
// Try Gemini Nano via ML Kit Prompt API
|
|
try {
|
|
Log.i(TAG, "Attempting Gemini Nano via ML Kit Prompt API...")
|
|
val nano = GeminiNanoModel.create(context)
|
|
Log.i(TAG, "Gemini Nano ready!")
|
|
return@withContext nano
|
|
} catch (e: Exception) {
|
|
Log.w(TAG, "Gemini Nano not available: ${e.message}")
|
|
}
|
|
|
|
// Fall back to MediaPipe with a local model file
|
|
try {
|
|
Log.i(TAG, "Attempting MediaPipe LLM with local model...")
|
|
val mediapipe = MediaPipeModel.create(context)
|
|
Log.i(TAG, "MediaPipe model ready!")
|
|
return@withContext mediapipe
|
|
} catch (e: Exception) {
|
|
Log.w(TAG, "MediaPipe model not available: ${e.message}")
|
|
}
|
|
|
|
throw InferenceException(
|
|
"No AI model available.\n\n" +
|
|
"Option 1: Enter a Gemini API key in the app (works everywhere)\n\n" +
|
|
"Option 2: Use a Pixel device with Gemini Nano support " +
|
|
"(Pixel 10/9/8 series)\n\n" +
|
|
"Option 3: Place a MediaPipe-compatible model (.bin or .task) in:\n" +
|
|
" ${context.filesDir.absolutePath}/\n" +
|
|
" Supported: gemma-3n-E2B.task, gemma-2b-it-gpu-int4.bin, etc.\n\n" +
|
|
"Download models from:\n" +
|
|
" https://ai.google.dev/edge/mediapipe/solutions/genai/llm_inference/android"
|
|
)
|
|
}
|
|
}
|
|
|
|
class InferenceException(message: String, cause: Throwable? = null) :
|
|
Exception(message, cause)
|
|
}
|