Files
galaxy/app/src/voice/transcriber.rs
T

49 lines
1.6 KiB
Rust

use std::sync::Arc;
use async_trait::async_trait;
use galaxyui::{Entity, SingletonEntity};
use crate::server::server_api::TranscribeError;
/// Interface for transcribing voice input.
#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait Transcriber: Send + Sync {
/// Transcribe the given base64 encoded wav file into text.
/// This is expected to be async and called off the main thread.
async fn transcribe(&self, wav_base64: String) -> Result<String, TranscribeError>;
}
/// A voice transcriber that is enabled or disabled.
///
/// This is a singleton model that the app can decide to enable or disable.
/// The editor does expect that it will exist as a singleton fetchable from app context
/// either way though, and depending on whether the optional transcriber is set,
/// the editor considers transcriber to be enabled or disabled.
///
/// We set it up this way to avoid the editor having a direct dependency on any server api.
pub struct VoiceTranscriber {
/// The transcriber to use. If `None`, the transcriber is disabled.
#[cfg_attr(not(feature = "voice_input"), allow(dead_code))]
transcriber: Option<Arc<dyn Transcriber>>,
}
impl VoiceTranscriber {
pub fn new(transcriber: Arc<dyn Transcriber>) -> Self {
Self {
transcriber: Some(transcriber),
}
}
/// Returns the transcriber if one is set.
pub fn transcriber(&self) -> Option<&Arc<dyn Transcriber>> {
self.transcriber.as_ref()
}
}
impl Entity for VoiceTranscriber {
type Event = ();
}
impl SingletonEntity for VoiceTranscriber {}