Skip to content
Merged
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
24 changes: 24 additions & 0 deletions app/src/main/java/helium314/keyboard/latin/ai/AiResultChecks.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin.ai

/** Checks on what an AI model returned, before it replaces the user's text */
object AiResultChecks {
/** a corrected text keeps about its length */
const val MIN_PROOFREAD_SHARE = 0.7
/** a translation can be much shorter than its source, e.g. into Chinese */
const val MIN_TRANSLATION_SHARE = 0.3

/** whether [result] is so much shorter than [input] that parts of it are most likely missing */
fun isCutOff(result: String, input: String, minShare: Double): Boolean =
input.length > 20 && result.length < input.length * minShare

// only at the start of the answer, and only with a colon: other lines belong to the translation
private val TRANSLATION_PREAMBLE = Regex(
"^(?:sure[,!.]\\s*(?=here))?" +
"(?:(?:here(?:'s|\\s+is)\\s+(?:the\\s+)?)?(?:translated\\s+text|translation)|text\\s+to\\s+translate|output|result|response)" +
"(?:\\s+(?:in|into|to)\\s+\\p{L}+)?\\s*:\\s*",
RegexOption.IGNORE_CASE
)

fun stripTranslationPreamble(text: String): String = text.replace(TRANSLATION_PREAMBLE, "").trim()
}
66 changes: 36 additions & 30 deletions app/src/main/java/helium314/keyboard/latin/ai/OfflineAiLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.net.Uri
import dalvik.system.DexClassLoader
import helium314.keyboard.compat.contentLengthLongCompat
import helium314.keyboard.latin.utils.replaceFile
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.prefs
import java.io.File
Expand Down Expand Up @@ -206,27 +207,29 @@ object OfflineAiLoader {
} catch (_: Exception) {}

val targetFile = File(context.filesDir, PLUGIN_FILENAME)
if (targetFile.exists()) targetFile.delete()
val success = replaceFile(targetFile, write = {
context.contentResolver.openInputStream(sourceUri)?.use { input ->
java.io.FileOutputStream(it).use { output ->
input.copyTo(output)
}
} ?: error("the file can't be opened")
}) {
targetFile.setReadOnly()
activeProvider?.cleanup()
activeProvider = null

context.contentResolver.openInputStream(sourceUri)?.use { input ->
java.io.FileOutputStream(targetFile).use { output ->
input.copyTo(output)
val provider = loadProviderInternal(context, targetFile)
val accepted = provider != null && provider.isAvailable()
if (accepted) {
activeProvider = provider
}
} ?: return false

targetFile.setReadOnly()
activeProvider?.cleanup()
activeProvider = null

val provider = loadProviderInternal(context, targetFile)
val success = provider != null && provider.isAvailable()
accepted
}
if (success) {
activeProvider = provider
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, true).apply()
Log.i(TAG, "Plugin imported and registered successfully")
} else {
targetFile.delete()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, false).apply()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, targetFile.exists()).apply()
Log.w(TAG, "Plugin import verification failed")
}
success
Expand All @@ -243,24 +246,27 @@ object OfflineAiLoader {
} catch (_: Exception) {}

val targetFile = File(context.filesDir, PLUGIN_FILENAME)
if (targetFile.exists()) targetFile.delete()
val success = replaceFile(targetFile, write = {
tempFile.copyTo(it, overwrite = true)
tempFile.delete()
}) {
targetFile.setReadOnly()

tempFile.copyTo(targetFile, overwrite = true)
tempFile.delete()
targetFile.setReadOnly()
activeProvider?.cleanup()
activeProvider = null

activeProvider?.cleanup()
activeProvider = null

val provider = loadProviderInternal(context, targetFile)
val success = provider != null && provider.isAvailable()
val provider = loadProviderInternal(context, targetFile)
val accepted = provider != null && provider.isAvailable()
if (accepted) {
activeProvider = provider
}
accepted
}
if (success) {
activeProvider = provider
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, true).apply()
Log.i(TAG, "Plugin imported from temp file successfully")
} else {
targetFile.delete()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, false).apply()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, targetFile.exists()).apply()
Log.w(TAG, "Plugin temp file verification failed")
}
success
Expand Down Expand Up @@ -289,11 +295,11 @@ object OfflineAiLoader {
}
}

@Volatile
var isBusy: Boolean = false
/** the number of requests that use the model */
val busy = java.util.concurrent.atomic.AtomicInteger()

fun onTrimMemory() {
if (isBusy) {
if (busy.get() > 0) {
Log.i(TAG, "Skipping memory trim: offline AI is currently busy")
return
}
Expand Down
69 changes: 37 additions & 32 deletions app/src/main/java/helium314/keyboard/latin/cjk/CjkPluginLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.net.Uri
import android.os.Build
import dalvik.system.DexClassLoader
import helium314.keyboard.latin.utils.replaceFile
import helium314.keyboard.latin.BuildConfig
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.prefs
Expand Down Expand Up @@ -316,28 +317,30 @@ object CjkPluginLoader {
} catch (_: Exception) {}

val targetFile = File(context.filesDir, PLUGIN_FILENAME)
if (targetFile.exists()) targetFile.delete()

context.contentResolver.openInputStream(sourceUri)?.use { input ->
java.io.FileOutputStream(targetFile).use { output ->
input.copyTo(output)
val success = replaceFile(targetFile, write = {
context.contentResolver.openInputStream(sourceUri)?.use { input ->
java.io.FileOutputStream(it).use { output ->
input.copyTo(output)
}
} ?: error("the file can't be opened")
}) {
targetFile.setReadOnly()
activeEngine?.cleanup()
activeEngine = null

val engine = loadEngineInternal(context, targetFile)
val accepted = engine != null && isUsable(engine)
if (accepted) {
activeEngine = engine
lastApkModified = targetFile.lastModified()
}
} ?: return false

targetFile.setReadOnly()
activeEngine?.cleanup()
activeEngine = null

val engine = loadEngineInternal(context, targetFile)
val success = engine != null && isUsable(engine)
accepted
}
if (success) {
activeEngine = engine
lastApkModified = targetFile.lastModified()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, true).apply()
Log.i(TAG, "CJK plugin imported and verified successfully")
} else {
targetFile.delete()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, false).apply()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, targetFile.exists()).apply()
Log.w(TAG, "CJK plugin verification failed")
}
success
Expand All @@ -355,25 +358,27 @@ object CjkPluginLoader {
} catch (_: Exception) {}

val targetFile = File(context.filesDir, PLUGIN_FILENAME)
if (targetFile.exists()) targetFile.delete()

tempFile.copyTo(targetFile, overwrite = true)
tempFile.delete()

targetFile.setReadOnly()
activeEngine?.cleanup()
activeEngine = null

val engine = loadEngineInternal(context, targetFile)
val success = engine != null && isUsable(engine)
val success = replaceFile(targetFile, write = {
tempFile.copyTo(it, overwrite = true)
tempFile.delete()
}) {
targetFile.setReadOnly()
activeEngine?.cleanup()
activeEngine = null

val engine = loadEngineInternal(context, targetFile)
val accepted = engine != null && isUsable(engine)
if (accepted) {
activeEngine = engine
lastApkModified = targetFile.lastModified()
}
accepted
}
if (success) {
activeEngine = engine
lastApkModified = targetFile.lastModified()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, true).apply()
Log.i(TAG, "CJK plugin imported from temp file and verified successfully")
} else {
targetFile.delete()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, false).apply()
context.prefs().edit().putBoolean(PREF_HAS_PLUGIN, targetFile.exists()).apply()
Log.w(TAG, "CJK plugin verification failed")
}
success
Expand Down
Loading
Loading