@@ -82,6 +82,12 @@ class MainActivity : ComponentActivity() {
8282 private lateinit var viewModel: ModelSettingsViewModel
8383 private lateinit var downloadViewModel: ModelDownloadViewModel
8484
85+ // Cached ParakeetModule: loaded once and reused across transcription calls.
86+ private var parakeetModule: ParakeetModule ? = null
87+ private var loadedModelPath: String = " "
88+ private var loadedTokenizerPath: String = " "
89+ private var loadedDataPath: String? = null
90+
8591 enum class Screen {
8692 DOWNLOAD ,
8793 MAIN ,
@@ -211,6 +217,35 @@ class MainActivity : ComponentActivity() {
211217 }.start()
212218 }
213219
220+ /* *
221+ * Returns the cached ParakeetModule, creating or recreating it only if
222+ * the model settings have changed since the last load.
223+ */
224+ private fun getOrCreateModule (settings : ModelSettings ): ParakeetModule {
225+ val dataPath = settings.dataPath.ifBlank { null }
226+ if (parakeetModule != null &&
227+ loadedModelPath == settings.modelPath &&
228+ loadedTokenizerPath == settings.tokenizerPath &&
229+ loadedDataPath == dataPath
230+ ) {
231+ return parakeetModule!!
232+ }
233+
234+ // Settings changed or first load — close the old module and create a new one.
235+ parakeetModule?.close()
236+ Log .v(TAG , " Loading model: ${settings.modelPath} " )
237+ val module = ParakeetModule (
238+ modelPath = settings.modelPath,
239+ tokenizerPath = settings.tokenizerPath,
240+ dataPath = dataPath
241+ )
242+ parakeetModule = module
243+ loadedModelPath = settings.modelPath
244+ loadedTokenizerPath = settings.tokenizerPath
245+ loadedDataPath = dataPath
246+ return module
247+ }
248+
214249 /* *
215250 * Common method to run Parakeet on a WAV file path.
216251 */
@@ -232,24 +267,18 @@ class MainActivity : ComponentActivity() {
232267 buttonEnabled = false
233268 }
234269
235- val parakeetModule = ParakeetModule (
236- modelPath = settings.modelPath,
237- tokenizerPath = settings.tokenizerPath,
238- dataPath = settings.dataPath.ifBlank { null }
239- )
270+ val module = getOrCreateModule(settings)
240271
241272 Log .v(TAG , " Starting transcribe for: $wavFilePath " )
242273 runOnUiThread {
243274 statusText = " Transcribing..."
244275 }
245276 val startTime = System .currentTimeMillis()
246- val result = parakeetModule .transcribe(wavFilePath)
277+ val result = module .transcribe(wavFilePath)
247278 val elapsedTime = System .currentTimeMillis() - startTime
248279 val elapsedSeconds = elapsedTime / 1000.0
249280 Log .v(TAG , " Finished transcribe in ${elapsedSeconds} s" )
250281
251- parakeetModule.close()
252-
253282 runOnUiThread {
254283 transcriptionOutput = result
255284 statusText = " Transcription complete (%.2fs)" .format(elapsedSeconds)
@@ -468,6 +497,8 @@ class MainActivity : ComponentActivity() {
468497 if (isRecording) {
469498 stopRecording()
470499 }
500+ parakeetModule?.close()
501+ parakeetModule = null
471502 }
472503
473504 private val requestPermissionLauncher = registerForActivityResult(
0 commit comments