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
47 changes: 47 additions & 0 deletions Android/src/app/eval/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.ai.edge.gallery.eval">

<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="34" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

<application
android:label="Gallery Eval App"
android:allowBackup="false">

<service
android:name=".EvalService"
android:exported="true"
android:foregroundServiceType="specialUse">
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Evaluation server for model execution" />
</service>

<receiver
android:name=".EvalReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.ai.edge.gallery.eval.START_SERVER" />
<action android:name="com.google.ai.edge.gallery.eval.STOP_SERVER" />
</intent-filter>
</receiver>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.ai.edge.gallery.eval

import android.util.Log
import com.google.ai.edge.gallery.data.Model
import com.google.ai.edge.gallery.runtime.LlmModelHelper
import java.util.concurrent.ConcurrentHashMap

class ConversationManager {

private val modelHistory = ConcurrentHashMap<String, List<HistoryMessage>>()

fun checkHistoryAndReset(
modelName: String,
incomingHistory: List<HistoryMessage>,
model: Model,
helper: LlmModelHelper,
) {
val cachedHistory = modelHistory[modelName]
if (cachedHistory == null || cachedHistory != incomingHistory) {
Log.i(TAG, "History mismatch or new session. Resetting conversation for $modelName.")
val lmHistory = PromptParser.convertToLmMessages(incomingHistory)
helper.resetConversation(
model = model,
supportImage = model.llmSupportImage,
supportAudio = model.llmSupportAudio,
initialMessages = lmHistory,
)
modelHistory[modelName] = incomingHistory
} else {
Log.i(TAG, "History match. Appending to existing conversation for $modelName.")
}
}

fun appendTurn(modelName: String, promptContentStr: String, assistantResult: String) {
val currentHistory = modelHistory[modelName] ?: emptyList()
modelHistory[modelName] =
currentHistory +
HistoryMessage("user", promptContentStr) +
HistoryMessage("assistant", assistantResult)
}

// Exposed for testing
fun getHistory(modelName: String): List<HistoryMessage>? {
return modelHistory[modelName]
}

// Exposed for testing
fun updateHistory(modelName: String, history: List<HistoryMessage>) {
modelHistory[modelName] = history
}

companion object {
private const val TAG = "ConversationManager"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.ai.edge.gallery.eval

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

class EvalReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
Log.i(TAG, "Received action: $action")
val serviceIntent =
Intent(context, EvalService::class.java).apply {
this.action = action
if (intent.hasExtra("port")) {
putExtra("port", intent.getIntExtra("port", 8080))
}
}

if (action == EvalService.ACTION_STOP_SERVER) {
context.stopService(Intent(context, EvalService::class.java))
} else {
try {
context.startForegroundService(serviceIntent)
} catch (e: Exception) {
Log.e(TAG, "Failed to start service", e)
}
}
}

companion object {
private const val TAG = "EvalReceiver"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.ai.edge.gallery.eval

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat

class EvalService : Service() {

override fun onCreate() {
super.onCreate()
createNotificationChannel()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val action = intent?.action
Log.i(TAG, "onStartCommand action: $action")

if (action == ACTION_STOP_SERVER) {
stopSelf()
return START_NOT_STICKY
}

// Extract optional model configuration from the intent.
// In CL 3, these will be used to pre-initialize the model.
val port = intent?.getIntExtra("port", 8080) ?: 8080

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(
NOTIFICATION_ID,
createNotification(port),
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE,
)
} else {
startForeground(NOTIFICATION_ID, createNotification(port))
}

return START_STICKY
}

override fun onDestroy() {
super.onDestroy()
}

override fun onBind(intent: Intent?): IBinder? = null

private fun createNotificationChannel() {
val channel =
NotificationChannel(CHANNEL_ID, "Eval Service Channel", NotificationManager.IMPORTANCE_LOW)
val manager = getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(channel)
}

private fun createNotification(port: Int): Notification {
return NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Gallery Eval Server")
.setContentText("Running on port $port")
.setSmallIcon(android.R.drawable.ic_media_play)
.build()
}

companion object {
private const val TAG = "EvalService"
private const val CHANNEL_ID = "EvalServiceChannel"
private const val NOTIFICATION_ID = 1

const val ACTION_START_SERVER = "com.google.ai.edge.gallery.eval.START_SERVER"
const val ACTION_STOP_SERVER = "com.google.ai.edge.gallery.eval.STOP_SERVER"
}
}
Loading
Loading