-
Notifications
You must be signed in to change notification settings - Fork 10
/
WhisperRecognitionModel.kt
85 lines (72 loc) · 2.37 KB
/
WhisperRecognitionModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.whispercppdemo
import android.app.Application
import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.io.File
class WhisperRecognitionModel(
private val context: Context
) : ViewModel() {
var canTranscribe = false
var dataLog = ""
private val modelsPath = File(context.filesDir, "models")
private var whisperContext: WhisperContext? = null
init {
viewModelScope.launch {
loadData()
}
}
private suspend fun loadData() {
printMessage("Loading data...\n")
try {
copyAssets()
loadBaseModel()
canTranscribe = true
} catch (e: Exception) {
Log.w(LOG_TAG, e)
printMessage("${e.localizedMessage}\n")
}
}
private suspend fun printMessage(msg: String) = withContext(Dispatchers.Main) {
dataLog += msg
println(msg)
}
private suspend fun copyAssets() = withContext(Dispatchers.IO) {
modelsPath.mkdirs()
printMessage("All data copied to working directory.\n")
}
private suspend fun loadBaseModel() = withContext(Dispatchers.IO) {
printMessage("Loading model...\n")
val models = context.assets.list("models/")
if (models != null) {
whisperContext =
WhisperContext.createContextFromAsset(context.assets, "models/" + models[0])
printMessage("Loaded model ${models[0]}.\n")
}
}
fun transcribe(bytes: ByteArray, onResultListener: (String) -> Unit) = viewModelScope.launch {
if (!canTranscribe) {
onResultListener("[busy!]")
}
printMessage("Reading wave bytes...\n")
val data = decodeBytes(bytes)
printMessage("Transcribing data...\n")
val text = whisperContext?.transcribeData(data)
printMessage("Done: $text\n")
text?.let { onResultListener(it) } ?: onResultListener("[no test!]")
}
override fun onCleared() {
runBlocking {
whisperContext?.release()
whisperContext = null
}
}
companion object {
private const val LOG_TAG = "WhisperRecognitionModel"
}
}