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
48 changes: 48 additions & 0 deletions Android/src/app/eval/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
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,63 @@
/*
* 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.litertlm.Message as LmMessage
import java.util.concurrent.ConcurrentHashMap

class ConversationManager {

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

fun checkHistoryAndReset(
sessionKey: String,
incomingHistory: List<HistoryMessage>,
resetAction: (List<LmMessage>) -> Unit,
) {
val cachedHistory = sessionHistory[sessionKey]
if (cachedHistory == null || cachedHistory != incomingHistory) {
Log.i(TAG, "History mismatch or new session. Resetting conversation for $sessionKey.")
val lmHistory = PromptParser.convertToLmMessages(incomingHistory)
resetAction(lmHistory)
sessionHistory[sessionKey] = incomingHistory
} else {
Log.i(TAG, "History match. Appending to existing conversation for $sessionKey.")
}
}

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

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

// Exposed for testing
fun updateHistory(sessionKey: String, history: List<HistoryMessage>) {
sessionHistory[sessionKey] = 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"
}
}
Loading
Loading