Update chat events/transcription flow and container startup fixes
This commit is contained in:
29
Frontend/server/utils/langfuse.ts
Normal file
29
Frontend/server/utils/langfuse.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Langfuse } from "langfuse";
|
||||
|
||||
let client: Langfuse | null = null;
|
||||
|
||||
function isTruthy(value: string | undefined) {
|
||||
const v = (value ?? "").trim().toLowerCase();
|
||||
return v === "1" || v === "true" || v === "yes" || v === "on";
|
||||
}
|
||||
|
||||
export function isLangfuseEnabled() {
|
||||
const enabledRaw = process.env.LANGFUSE_ENABLED;
|
||||
if (enabledRaw && !isTruthy(enabledRaw)) return false;
|
||||
return Boolean((process.env.LANGFUSE_PUBLIC_KEY ?? "").trim() && (process.env.LANGFUSE_SECRET_KEY ?? "").trim());
|
||||
}
|
||||
|
||||
export function getLangfuseClient() {
|
||||
if (!isLangfuseEnabled()) return null;
|
||||
if (client) return client;
|
||||
|
||||
client = new Langfuse({
|
||||
publicKey: (process.env.LANGFUSE_PUBLIC_KEY ?? "").trim(),
|
||||
secretKey: (process.env.LANGFUSE_SECRET_KEY ?? "").trim(),
|
||||
baseUrl: (process.env.LANGFUSE_BASE_URL ?? "http://langfuse-web:3000").trim(),
|
||||
enabled: true,
|
||||
});
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
53
Frontend/server/utils/whisper.ts
Normal file
53
Frontend/server/utils/whisper.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
type WhisperTranscribeInput = {
|
||||
samples: Float32Array;
|
||||
sampleRate: number;
|
||||
language?: string;
|
||||
};
|
||||
let whisperPipelinePromise: Promise<any> | null = null;
|
||||
let transformersPromise: Promise<any> | null = null;
|
||||
|
||||
function getWhisperModelId() {
|
||||
return (process.env.CF_WHISPER_MODEL ?? "Xenova/whisper-small").trim() || "Xenova/whisper-small";
|
||||
}
|
||||
|
||||
function getWhisperLanguage() {
|
||||
const value = (process.env.CF_WHISPER_LANGUAGE ?? "ru").trim();
|
||||
return value || "ru";
|
||||
}
|
||||
|
||||
async function getWhisperPipeline() {
|
||||
if (!transformersPromise) {
|
||||
transformersPromise = import("@xenova/transformers");
|
||||
}
|
||||
|
||||
const { env, pipeline } = await transformersPromise;
|
||||
|
||||
if (!whisperPipelinePromise) {
|
||||
env.allowRemoteModels = true;
|
||||
env.allowLocalModels = true;
|
||||
env.cacheDir = "/app/.data/transformers";
|
||||
|
||||
const modelId = getWhisperModelId();
|
||||
whisperPipelinePromise = pipeline("automatic-speech-recognition", modelId);
|
||||
}
|
||||
|
||||
return whisperPipelinePromise;
|
||||
}
|
||||
|
||||
export async function transcribeWithWhisper(input: WhisperTranscribeInput) {
|
||||
const transcriber = (await getWhisperPipeline()) as any;
|
||||
const result = await transcriber(
|
||||
input.samples,
|
||||
{
|
||||
sampling_rate: input.sampleRate,
|
||||
language: (input.language ?? getWhisperLanguage()) || "ru",
|
||||
task: "transcribe",
|
||||
chunk_length_s: 20,
|
||||
stride_length_s: 5,
|
||||
return_timestamps: false,
|
||||
},
|
||||
);
|
||||
|
||||
const text = String((result as any)?.text ?? "").trim();
|
||||
return text;
|
||||
}
|
||||
Reference in New Issue
Block a user