-
Notifications
You must be signed in to change notification settings - Fork 7.4k
feat(firebase-ai): add transcriptions to Live API sample and reduce abstraction #2836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,25 +1,47 @@ | ||||||
| package com.google.firebase.quickstart.ai.feature.live | ||||||
|
|
||||||
| import android.annotation.SuppressLint | ||||||
| import android.graphics.Bitmap | ||||||
| import androidx.lifecycle.ViewModel | ||||||
| import androidx.lifecycle.viewModelScope | ||||||
| import com.google.firebase.Firebase | ||||||
| import com.google.firebase.ai.ai | ||||||
| import com.google.firebase.ai.type.AudioTranscriptionConfig | ||||||
| import com.google.firebase.ai.type.GenerativeBackend | ||||||
| import com.google.firebase.ai.type.InlineData | ||||||
| import com.google.firebase.ai.type.LiveSession | ||||||
| import com.google.firebase.ai.type.PublicPreviewAPI | ||||||
| import com.google.firebase.ai.type.ResponseModality | ||||||
| import com.google.firebase.ai.type.SpeechConfig | ||||||
| import com.google.firebase.ai.type.Transcription | ||||||
| import com.google.firebase.ai.type.Voice | ||||||
| import com.google.firebase.ai.type.liveGenerationConfig | ||||||
| import kotlinx.coroutines.Dispatchers | ||||||
| import kotlinx.coroutines.flow.MutableStateFlow | ||||||
| import kotlinx.coroutines.flow.StateFlow | ||||||
| import kotlinx.coroutines.flow.asStateFlow | ||||||
| import kotlinx.coroutines.flow.update | ||||||
| import kotlinx.coroutines.launch | ||||||
| import kotlinx.coroutines.runBlocking | ||||||
| import kotlinx.serialization.Serializable | ||||||
| import java.io.ByteArrayOutputStream | ||||||
|
|
||||||
| @Serializable | ||||||
| object StreamRealtimeVideoRoute | ||||||
|
|
||||||
| @OptIn(PublicPreviewAPI::class) | ||||||
| class StreamVideoViewModel : BidiViewModel() { | ||||||
| class StreamVideoViewModel : ViewModel() { | ||||||
| private var liveSession: LiveSession | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Declaring
Suggested change
|
||||||
|
|
||||||
| private val _transcriptions = MutableStateFlow<List<TranscriptionItem>>(emptyList()) | ||||||
| val transcriptions: StateFlow<List<TranscriptionItem>> = _transcriptions.asStateFlow() | ||||||
|
|
||||||
| init { | ||||||
| val liveGenerationConfig = liveGenerationConfig { | ||||||
| speechConfig = SpeechConfig(voice = Voice("CHARON")) | ||||||
| responseModality = ResponseModality.AUDIO | ||||||
| inputAudioTranscription = AudioTranscriptionConfig() | ||||||
| outputAudioTranscription = AudioTranscriptionConfig() | ||||||
| } | ||||||
|
|
||||||
| // Note that each backend supports a different set of models. | ||||||
|
|
@@ -33,4 +55,58 @@ class StreamVideoViewModel : BidiViewModel() { | |||||
| ) | ||||||
| runBlocking { liveSession = liveModel.connect() } | ||||||
| } | ||||||
|
|
||||||
| private fun handleTranscription(input: Transcription?, output: Transcription?) { | ||||||
| input?.text?.let { text -> | ||||||
| if (text.isNotEmpty()) { | ||||||
| _transcriptions.update { current -> | ||||||
| val last = current.lastOrNull() | ||||||
| if (last != null && last.speaker == TranscriptionSpeaker.USER) { | ||||||
| current.dropLast(1) + last.copy(text = last.text + text) | ||||||
| } else { | ||||||
| current + TranscriptionItem(speaker = TranscriptionSpeaker.USER, text = text) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| output?.text?.let { text -> | ||||||
| if (text.isNotEmpty()) { | ||||||
| _transcriptions.update { current -> | ||||||
| val last = current.lastOrNull() | ||||||
| if (last != null && last.speaker == TranscriptionSpeaker.MODEL) { | ||||||
| current.dropLast(1) + last.copy(text = last.text + text) | ||||||
| } else { | ||||||
| current + TranscriptionItem(speaker = TranscriptionSpeaker.MODEL, text = text) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // The permission check is handled by the view that calls this function. | ||||||
| @SuppressLint("MissingPermission") | ||||||
| fun startConversation() { | ||||||
| viewModelScope.launch(Dispatchers.IO) { | ||||||
| liveSession.startAudioConversation(null, ::handleTranscription) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fun endConversation() { | ||||||
| liveSession.stopAudioConversation() | ||||||
| } | ||||||
|
|
||||||
| fun clearTranscriptions() { | ||||||
| _transcriptions.value = emptyList() | ||||||
| } | ||||||
|
|
||||||
| fun sendVideoFrame(frame: Bitmap) { | ||||||
| viewModelScope.launch { | ||||||
| // Directly compress the Bitmap to a ByteArray | ||||||
| val byteArrayOutputStream = ByteArrayOutputStream() | ||||||
| frame.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream) | ||||||
| val jpegBytes = byteArrayOutputStream.toByteArray() | ||||||
|
|
||||||
| liveSession.sendVideoRealtime(InlineData(jpegBytes, "image/jpeg")) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declaring
liveSessionas a non-nullable property initialized viarunBlockingin theinitblock blocks the main thread during ViewModel creation to establish a network connection. This can cause UI freezes or ANR (Application Not Responding) errors. Instead, declareliveSessionaslateinit var(or nullable) and connect to the live model asynchronously usingviewModelScope.launch.