Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

3.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { sttEngineFor } from "@/features/chat/adapters/studio-model-dictation-adapter";
import { useVoiceSettingsStore, withSttVariant } from "@/features/settings";
import { sttContextBody } from "@/features/settings/stores/stt-context";
import { useCallback, useEffect, useRef, useState } from "react";
import { transcribeAudioInput } from "../api";
import { type AudioSourceSelection, sourceRefOf } from "../audio-run-request";
import { sttEngineForRepoId, sttSidecarKeyFor } from "../catalog";

/** fills the prompt without a transcript entry or language hint for cross-lingual references. */
export function useReferenceTranscribe({
  sttRepo,
  onText,
  purpose = "reference",
}: {
  sttRepo: string | null;
  onText: (text: string, reference: AudioSourceSelection) => void;
  /** "convert" transcribes up to Convert's cap instead of the 30 s clone reference. */
  purpose?: "reference" | "convert";
}) {
  const [transcribing, setTranscribing] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const abort = useRef<AbortController | null>(null);
  const onTextRef = useRef(onText);
  onTextRef.current = onText;

  const transcribe = useCallback(
    async (reference: AudioSourceSelection | null) => {
      if (!reference) return;
      // a newer clip wins so its transcription is never skipped.
      abort.current?.abort();
      const controller = new AbortController();
      abort.current = controller;
      setTranscribing(true);
      setError(null);
      try {
        const voice = useVoiceSettingsStore.getState();
        const model = sttRepo ? sttSidecarKeyFor(sttRepo) : voice.sttModel;
        const engine = sttRepo ? sttEngineForRepoId(sttRepo) : sttEngineFor(model);
        const result = await transcribeAudioInput(
          sourceRefOf(reference),
          {
            // the Settings model uses its saved quant; a Transcribe selection is already resident.
            model: sttRepo
              ? model
              : withSttVariant(model, voice.sttGgufVariant),
            engine,
            device: voice.sttDevice,
            purpose,
            ...sttContextBody(model, engine, voice.sttContextLength),
          },
          controller.signal,
        );
        if (controller.signal.aborted) return;
        const text = result.text.trim();
        if (text) {
          onTextRef.current(text, reference);
        } else {
          setError(
            "No speech was heard in the clip. Type what's said instead.",
          );
        }
      } catch (reason) {
        if (controller.signal.aborted) return;
        setError(
          reason instanceof Error && reason.message
            ? reason.message
            : "Could not transcribe the clip.",
        );
      } finally {
        if (abort.current === controller) {
          abort.current = null;
          setTranscribing(false);
        }
      }
    },
    [sttRepo, purpose],
  );

  const cancel = useCallback(() => {
    abort.current?.abort();
    abort.current = null;
    setTranscribing(false);
    setError(null);
  }, []);

  useEffect(() => () => abort.current?.abort(), []);

  return { transcribe, cancel, transcribing, error };
}