#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/moss_transcribe_diarize/modular_moss_transcribe_diarize.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_moss_transcribe_diarize.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re

import numpy as np
import torch

from ...audio_utils import AudioInput, make_audio_chat_template_content, make_list_of_audio_chat_template
from ...feature_extraction_utils import BatchFeature
from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack, prepare_keyword_inputs, prepare_prompt_input
from ...tokenization_utils_base import TextInput
from ...utils import auto_docstring, logging
from ...utils.deprecation import deprecate_attr
from ...utils.import_utils import requires


logger = logging.get_logger(__name__)


@requires(backends=("torch",))
@auto_docstring
class MossTranscribeDiarizeProcessor(ProcessorMixin):
    r"""
    Constructs a MOSS-Transcribe-Diarize processor which wraps [`MossTranscribeDiarizeFeatureExtractor`] and
    [`Qwen2TokenizerFast`] into a single processor that inherits both the audio feature extraction and
    tokenizer functionalities.

    See the [`~MossTranscribeDiarizeProcessor.__call__`] for more information.

    Args:
        feature_extractor (`MossTranscribeDiarizeFeatureExtractor`):
            The feature extractor for audio processing.
        tokenizer (`Qwen2TokenizerFast`):
            The tokenizer for text processing.
        chat_template (`str`, *optional*):
            A Jinja template which will be used to convert lists of messages in a chat into a tokenizable string.
    """

    def __init__(
        self,
        feature_extractor=None,
        tokenizer=None,
        chat_template: str | None = None,
        audio_token: str = "<|audio_pad|>",
        audio_bos_token: str = "<|audio_start|>",
        audio_eos_token: str = "<|audio_end|>",
        audio_tokens_per_second: float = 12.5,
        audio_merge_size: int = 4,
        time_marker_every_seconds: int = 2,
    ):
        r"""
        audio_token (`str`, *optional*, defaults to `"<|audio_pad|>"`):
            Special token used to represent audio inputs in the chat template.
        audio_bos_token (`str`, *optional*, defaults to `"<|audio_start|>"`):
            Special token marking the start of the audio span in the chat template.
        audio_eos_token (`str`, *optional*, defaults to `"<|audio_end|>"`):
            Special token marking the end of the audio span in the chat template.
        audio_tokens_per_second (`float`, *optional*, defaults to 12.5):
            Expected number of audio placeholder tokens per second of input audio.
        audio_merge_size (`int`, *optional*, defaults to 4):
            Whisper frame merge factor used when counting audio tokens.
        time_marker_every_seconds (`int`, *optional*, defaults to 2):
            Insert numeric time-marker tokens into the audio span every N seconds. Set to `0` to disable
            time markers and emit a plain audio placeholder span instead.
        """
        self.audio_token = audio_token
        self.audio_token_id = tokenizer.convert_tokens_to_ids(audio_token)
        self.audio_bos_token = audio_bos_token
        self.audio_bos_token_id = tokenizer.convert_tokens_to_ids(audio_bos_token)
        self.audio_eos_token = audio_eos_token
        self.audio_eos_token_id = tokenizer.convert_tokens_to_ids(audio_eos_token)
        super().__init__(feature_extractor, tokenizer, chat_template=chat_template)
        self.audio_tokens_per_second = audio_tokens_per_second
        self.audio_merge_size = audio_merge_size
        self.time_marker_every_seconds = time_marker_every_seconds
        # Diarized segments look like `[start][S01]text[end]`, e.g. `[0.00][S01]Hello there.[7.56]`.
        self._segment_pattern = re.compile(
            r"\[(?P<start>[^\[\]]+)\]\[S(?P<speaker>\d+)\](?P<content>.*?)\[(?P<end>[^\[\]]+)\]", re.DOTALL
        )

    @auto_docstring
    def __call__(
        self,
        text: TextInput | list[TextInput],
        audio: AudioInput | None = None,
        output_labels: bool | None = False,
        **kwargs: Unpack[ProcessingKwargs],
    ) -> BatchFeature:
        r"""
        output_labels (bool, *optional*, default=False):
            Whether to return labels for training.

        Returns:
            [`BatchFeature`]: A dictionary with tokenized text (`input_ids`, `attention_mask`) and
            audio features (`input_values`, `padding_mask`).
        """
        output_kwargs = self._merge_kwargs(tokenizer_init_kwargs=self.tokenizer.init_kwargs, **kwargs)
        return_tensors = output_kwargs["text_kwargs"].get("return_tensors", None)

        if return_tensors != "pt":
            raise ValueError(f"{self.__class__.__name__} only supports `return_tensors='pt'`.")

        if audio is not None:
            _, text, _, audio = self.prepare_inputs_layout(text=text, audio=audio, **kwargs)

        model_inputs = super().__call__(text=text, audio=audio, **kwargs)

        if output_labels:
            labels = model_inputs["input_ids"].clone()
            labels[labels == self.audio_token_id] = -100
            labels[labels == self.audio_bos_token_id] = -100
            labels[labels == self.audio_eos_token_id] = -100
            labels[labels == self.tokenizer.pad_token_id] = -100
            model_inputs["labels"] = labels

        return BatchFeature(data=model_inputs, tensor_type="pt", skip_tensor_conversion=self.skip_tensor_conversion)

    def prepare_inputs_layout(self, text=None, audio=None, **kwargs):
        _, text, _, audio = super().prepare_inputs_layout(text=text, audio=audio, **kwargs)
        if isinstance(text, str):
            text = [text]
        return None, text, None, audio

    def validate_inputs(
        self,
        text: TextInput | list[TextInput] | None = None,
        audio: AudioInput | None = None,
        **kwargs: Unpack[ProcessingKwargs],
    ):
        super().validate_inputs(text=text, audio=audio, **kwargs)

        if not isinstance(text, (list, tuple)):
            raise ValueError("text input must be a string or list of strings")

        if audio is not None:
            for example in audio:
                if example.ndim != 1:
                    raise ValueError(f"Audio should be mono, got shape: {example.shape}")

    def _process_audio(self, audio: AudioInput, **kwargs) -> tuple[dict[str, torch.Tensor], list[str]]:
        audio_inputs = self.feature_extractor(audio, **kwargs)

        # Derive how many Whisper-window chunks each sample was split into from its raw length in
        # `padding_mask`, the same way the model derives `audio_chunk_mapping` via `config.audio_chunk_size`.
        window_size = self.feature_extractor.n_samples
        per_sample_lengths = audio_inputs["padding_mask"].sum(-1)
        per_sample_windows = ((per_sample_lengths + window_size - 1) // window_size).clamp(min=1).to(torch.long)
        audio_chunk_mapping = torch.repeat_interleave(torch.arange(len(audio), dtype=torch.long), per_sample_windows)

        # Based on `WhisperEncoder._get_feat_extract_output_lengths` (conv stride 2 only), so the placeholder
        # token count matches `get_audio_features` from the same mask.
        audio_lengths = audio_inputs["input_features_mask"].sum(-1)
        audio_inputs["num_audio_tokens"] = self._get_audio_token_length(audio_lengths, audio_chunk_mapping)
        audio_replacements = [self.replace_audio_token(audio_inputs, audio_idx=idx) for idx in range(len(audio))]
        return audio_inputs, audio_replacements

    def replace_audio_token(self, audio_inputs: dict, audio_idx: int, **kwargs) -> str:
        num_tokens = int(audio_inputs["num_audio_tokens"][audio_idx])

        tokens_per_marker = int(self.audio_tokens_per_second * self.time_marker_every_seconds)
        if tokens_per_marker <= 0:
            return self.audio_token * num_tokens

        # This model is trained with literal second-count tokens (ex 2, 4, ...) interleaved
        # into the audio span every `time_marker_every_seconds`, so the model can ground text to timestamps.
        duration = num_tokens / float(self.audio_tokens_per_second)
        parts, consumed = [], 0
        for sec in range(self.time_marker_every_seconds, int(duration) + 1, self.time_marker_every_seconds):
            pos = (sec // self.time_marker_every_seconds) * tokens_per_marker
            segment_len = pos - consumed
            if segment_len > 0:
                parts.append(self.audio_token * segment_len)
                consumed += segment_len
            parts.append(str(sec))

        # Since `duration` rarely lands on an exact multiple of `time_marker_every_seconds`, the loop above
        # stops at the last full interval and any audio tokens beyond that point get no marker.
        remainder = num_tokens - consumed
        if remainder > 0:
            parts.append(self.audio_token * remainder)
        return "".join(parts)

    @property
    def unused_input_names(self) -> list[str]:
        "Input names returned always by subprocessors but not used in model's `forward`"
        return ["num_audio_tokens"]

    def apply_transcription_request(
        self,
        audio: str | list[str] | AudioInput,
        prompt: str | list[str] | None = None,
        keywords: str | list[str] | list[list[str]] | None = None,
        tokenize: bool = True,
        **kwargs: Unpack[ProcessingKwargs],
    ) -> BatchFeature | list[str]:
        """
        Prepare inputs for transcription / diarization without manually writing the chat template.

        Args:
            audio (`str`, `list[str]`, `np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`):
                Audio to transcribe. Strings are interpreted as local paths or URLs and will be loaded automatically by
                the chat template loader; NumPy arrays and PyTorch tensors are forwarded directly.
            prompt (`str` or `list[str]`, *optional*):
                Custom prompt(s) to include in the user turn. A list must be the same length as the batch. When
                `None`, the chat template supplies the default timestamped diarization prompt.
            keywords (`str`, `list[str]`, or `list[list[str]]`, *optional*):
                Hotwords/domain terms (e.g. proper nouns) to bias transcription toward the correct spelling. A
                string or flat list of strings is shared across the batch; a nested list supplies separate
                keywords per audio sample.
            tokenize (`bool`, *optional*, defaults to `True`):
                Whether to tokenize the prompts and process the audio. If `False`, the audio is not loaded and
                the prompts are returned as strings with unexpanded audio placeholders. Passing them with the
                audio to the processor, `processor(text=prompts, audio=audio)`, gives the same outputs as
                `tokenize=True`.
            **kwargs:
                Additional keyword arguments forwarded to [`~MossTranscribeDiarizeProcessor.apply_chat_template`].
        """
        audio_items: list[str | np.ndarray] = list(make_list_of_audio_chat_template(audio))
        audio_items = [el.detach().cpu().numpy() if isinstance(el, torch.Tensor) else el for el in audio_items]

        batch_size = len(audio_items)
        if batch_size == 0:
            raise ValueError("`audio` must contain at least one sample.")

        prompts = prepare_prompt_input(prompt, batch_size, input_name="prompt")
        keyword_batches = prepare_keyword_inputs(keywords, batch_size)

        conversations = []
        for audio_item, prompt_text, keyword_list in zip(audio_items, prompts, keyword_batches):
            content = [make_audio_chat_template_content(audio_item)]
            if prompt_text is not None:
                content.append({"type": "text", "text": prompt_text})
            if keyword_list:
                content.append({"type": "keywords", "keywords": keyword_list})
            conversations.append([{"role": "user", "content": content}])

        return self.apply_chat_template(
            conversations,
            tokenize=tokenize,
            add_generation_prompt=True,
            return_dict=True,
            **kwargs,
        )

    def decode(self, *args, return_format="raw", **kwargs):
        """
        Forward arguments to [`~PreTrainedTokenizer.decode`] and optionally parse the dict-like output.

        VibeVoice ASR outputs transcriptions in a dictionary-like format, e.g.:
        ```
        [
            'assistant\n[{"Start":0.0,"End":7.56,"Speaker":0,"Content":"text"}]\n',
            'assistant\n[{"Start":0,"End":5.20,"Speaker":0,"Content":"text"}]\n'
        ]
        ```

        Args:
            return_format (`str`, *optional*, defaults to `"raw"`):
                Options are:
                - `"raw"`: Return a list of raw decoded strings from the tokenizer, without any parsing.
                - `"parsed"`: Return a list of list of parsed dictionary objects for each speaker utterance with timestamps.
                - `"transcription_only"`: Return a list of extracted transcription strings.

                `skip_special_tokens` is automatically enforced (hard-set) to `True` for `"parsed"` and `"transcription_only"`.
        """
        return_types = ["raw", "parsed", "transcription_only"]
        if return_format not in return_types:
            raise ValueError(f"return_format must be one of {return_types}.")
        if return_format != "raw":
            kwargs["skip_special_tokens"] = True  # for other formats this does not make sense, we can silently ignore

        decoded = self.tokenizer.decode(*args, **kwargs)

        if return_format == "parsed":
            decoded = self.extract_speaker_dict(decoded)
        elif return_format == "transcription_only":
            decoded = self.extract_transcription(decoded)
        return decoded

    def extract_speaker_dict(self, text: str | list[str]) -> list[dict] | str | list[list[dict] | str]:
        """
        Extract speaker/timestamp segments from raw output, returning original text on failure.

        Args:
            text (`str` or `list[str]`):
                Single text or batch of texts to parse from the output of `decode` with `return_format="raw"`.

        Returns:
            Parsed output(s). For single input, returns `list[dict]` or `str`.
            For batch input, returns `list[list[dict] | str]`.
        """
        is_single = isinstance(text, str)
        if is_single:
            text = [text]

        speaker_dict = []
        for t in text:
            t = t.strip()
            if t.startswith("assistant"):
                t = t[len("assistant") :].strip()

            segments = [
                {
                    "Start": self.parse_timestamp(match["start"]),
                    "End": self.parse_timestamp(match["end"]),
                    "Speaker": int(match["speaker"]),
                    "Content": match["content"].strip(),
                }
                for match in self._segment_pattern.finditer(t)
            ]

            if not segments:
                logger.warning("Output doesn't contain any `[start][S0N]text[end]` segments.")
                speaker_dict.append(t)
            else:
                speaker_dict.append(segments)

        return speaker_dict[0] if is_single else speaker_dict

    def extract_transcription(self, text: str | list[str]) -> str | list[str]:
        """
        Extract and concatenate 'Content' fields from the raw output, returning original text on failure.

        Args:
            text (`str` or `list[str]`):
                Single text or batch of texts to parse from the output of `decode` with `return_format="raw"`.

        Returns:
            Extracted transcription(s). For single input, returns `str`.
            For batch input, returns `list[str]`.
        """
        is_single = isinstance(text, str)
        if is_single:
            text = [text]

        transcriptions = []
        for t in text:
            dict_output = self.extract_speaker_dict(t)

            # If parsing failed, dict_output is the original string
            if isinstance(dict_output, str):
                transcriptions.append(dict_output)
            else:
                contents = [seg.get("Content", "") for seg in dict_output if isinstance(seg, dict)]
                transcriptions.append(" ".join(contents).strip())

        return transcriptions[0] if is_single else transcriptions

    def _get_audio_token_length(self, audio_lengths, audio_chunk_mapping):
        num_samples = int(audio_chunk_mapping[-1]) + 1
        conv_lengths = (audio_lengths - 1) // 2 + 1
        # Each chunk rounds up to a whole number of merge groups independently (into its own
        # zero-padded tail) before summing. If done after concatenating chunks, a chunk's
        # trailing frames would be lost (up to `self.audio_merge_size - 1`) to floor-division.
        chunk_tokens = (conv_lengths + self.audio_merge_size - 1) // self.audio_merge_size
        per_sample_tokens = torch.zeros(num_samples, dtype=torch.long)
        per_sample_tokens.scatter_add_(0, audio_chunk_mapping, chunk_tokens)
        return per_sample_tokens

    @property
    def model_input_names(self) -> list[str]:
        return super().model_input_names + ["input_features_mask", "padding_mask"]

    @staticmethod
    def parse_timestamp(value: str) -> float:
        """
        Parse a timestamp string into seconds. Accepts a plain float (`"7.56"`) or a colon-separated
        `MM:SS` / `HH:MM:SS` string (`"1:23"`, `"01:02:03"`), for MOSS-Transcribe-Diarize's diarization output.
        """
        return sum(float(part) * 60**i for i, part in enumerate(reversed(value.strip().split(":"))))


__getattr__ = deprecate_attr(__name__, rename={"MossTranscribeDiarizeProcessorKwargs": (ProcessingKwargs, "5.29.0")})

__all__ = ["MossTranscribeDiarizeProcessor"]
