feat: feature-gate local inference dependencies (#7976)
Signed-off-by: DaeHee Lee <lee111dae11@proton.me> Signed-off-by: jh-block <jhugo@block.xyz> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: jh-block <jhugo@block.xyz>
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
pub mod providers;
|
||||
#[cfg(feature = "local-inference")]
|
||||
pub mod whisper;
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
use crate::config::Config;
|
||||
#[cfg(feature = "local-inference")]
|
||||
use crate::dictation::whisper::LOCAL_WHISPER_MODEL_CONFIG_KEY;
|
||||
use crate::providers::api_client::{ApiClient, AuthMethod};
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "local-inference")]
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
|
||||
#[cfg(feature = "local-inference")]
|
||||
static LOCAL_TRANSCRIBER: once_cell::sync::Lazy<
|
||||
Mutex<Option<(String, super::whisper::WhisperTranscriber)>>,
|
||||
> = once_cell::sync::Lazy::new(|| Mutex::new(None));
|
||||
|
||||
#[cfg(feature = "local-inference")]
|
||||
const WHISPER_TOKENIZER_JSON: &str = include_str!("whisper_data/tokens.json");
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize, ToSchema)]
|
||||
@@ -21,6 +25,7 @@ pub enum DictationProvider {
|
||||
OpenAI,
|
||||
ElevenLabs,
|
||||
Groq,
|
||||
#[cfg(feature = "local-inference")]
|
||||
Local,
|
||||
}
|
||||
|
||||
@@ -66,19 +71,39 @@ pub const PROVIDERS: &[DictationProviderDef] = &[
|
||||
uses_provider_config: false,
|
||||
settings_path: None,
|
||||
},
|
||||
DictationProviderDef {
|
||||
provider: DictationProvider::Local,
|
||||
config_key: LOCAL_WHISPER_MODEL_CONFIG_KEY,
|
||||
default_base_url: "",
|
||||
endpoint_path: "",
|
||||
host_key: None,
|
||||
description: "Uses local Whisper model for transcription. No API key needed.",
|
||||
uses_provider_config: false,
|
||||
settings_path: None,
|
||||
},
|
||||
];
|
||||
|
||||
#[cfg(feature = "local-inference")]
|
||||
pub const LOCAL_PROVIDER_DEF: DictationProviderDef = DictationProviderDef {
|
||||
provider: DictationProvider::Local,
|
||||
config_key: LOCAL_WHISPER_MODEL_CONFIG_KEY,
|
||||
default_base_url: "",
|
||||
endpoint_path: "",
|
||||
host_key: None,
|
||||
description: "Uses local Whisper model for transcription. No API key needed.",
|
||||
uses_provider_config: false,
|
||||
settings_path: None,
|
||||
};
|
||||
|
||||
/// Returns all provider definitions, including Local when the `local-inference` feature is enabled.
|
||||
pub fn all_providers() -> Vec<&'static DictationProviderDef> {
|
||||
#[cfg(not(feature = "local-inference"))]
|
||||
{
|
||||
PROVIDERS.iter().collect()
|
||||
}
|
||||
#[cfg(feature = "local-inference")]
|
||||
{
|
||||
let mut all: Vec<&DictationProviderDef> = PROVIDERS.iter().collect();
|
||||
all.push(&LOCAL_PROVIDER_DEF);
|
||||
all
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_provider_def(provider: DictationProvider) -> &'static DictationProviderDef {
|
||||
#[cfg(feature = "local-inference")]
|
||||
if provider == DictationProvider::Local {
|
||||
return &LOCAL_PROVIDER_DEF;
|
||||
}
|
||||
PROVIDERS
|
||||
.iter()
|
||||
.find(|def| def.provider == provider)
|
||||
@@ -89,6 +114,7 @@ pub fn is_configured(provider: DictationProvider) -> bool {
|
||||
let config = Config::global();
|
||||
|
||||
match provider {
|
||||
#[cfg(feature = "local-inference")]
|
||||
DictationProvider::Local => config
|
||||
.get(LOCAL_WHISPER_MODEL_CONFIG_KEY, false)
|
||||
.ok()
|
||||
@@ -102,6 +128,7 @@ pub fn is_configured(provider: DictationProvider) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "local-inference")]
|
||||
pub async fn transcribe_local(audio_bytes: Vec<u8>) -> Result<String> {
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let config = Config::global();
|
||||
@@ -178,6 +205,7 @@ fn build_api_client(provider: DictationProvider) -> Result<ApiClient> {
|
||||
header_name: "xi-api-key".to_string(),
|
||||
key: api_key,
|
||||
},
|
||||
#[cfg(feature = "local-inference")]
|
||||
DictationProvider::Local => anyhow::bail!("Local provider should not use API client"),
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[cfg(feature = "local-inference")]
|
||||
use super::local_inference::LocalInferenceProvider;
|
||||
use super::{
|
||||
anthropic::AnthropicProvider,
|
||||
avian::AvianProvider,
|
||||
@@ -19,7 +21,6 @@ use super::{
|
||||
githubcopilot::GithubCopilotProvider,
|
||||
google::GoogleProvider,
|
||||
litellm::LiteLLMProvider,
|
||||
local_inference::LocalInferenceProvider,
|
||||
nanogpt::NanoGptProvider,
|
||||
ollama::OllamaProvider,
|
||||
openai::OpenAiProvider,
|
||||
@@ -49,6 +50,7 @@ async fn init_registry() -> RwLock<ProviderRegistry> {
|
||||
registry.register::<AvianProvider>(false);
|
||||
registry.register::<AzureProvider>(false);
|
||||
registry.register::<BedrockProvider>(false);
|
||||
#[cfg(feature = "local-inference")]
|
||||
registry.register::<LocalInferenceProvider>(false);
|
||||
registry.register::<ChatGptCodexProvider>(true);
|
||||
registry.register::<ClaudeAcpProvider>(false);
|
||||
|
||||
@@ -27,6 +27,7 @@ pub mod githubcopilot;
|
||||
pub mod google;
|
||||
mod init;
|
||||
pub mod litellm;
|
||||
#[cfg(feature = "local-inference")]
|
||||
pub mod local_inference;
|
||||
pub mod nanogpt;
|
||||
pub mod oauth;
|
||||
|
||||
Reference in New Issue
Block a user