# Copyright (c) 2025 PaddlePaddle Authors. 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 os
from typing import Any, Dict, List

from paddlex_hps_server import BaseTritonPythonModel, protocol, schemas, utils


class TritonPythonModel(BaseTritonPythonModel):
    def get_input_model_type(self):
        return schemas.multilingual_speech_recognition.InferRequest

    def get_result_model_type(self):
        return schemas.multilingual_speech_recognition.InferResult

    def run(self, input, log_id):
        file_bytes = utils.get_raw_bytes(input.audio)
        ext = utils.infer_file_ext(input.audio)
        if ext is None:
            return protocol.create_aistudio_output_without_result(
                422,
                "File extension cannot be inferred",
                log_id=log_id,
            )
        audio_path = utils.write_to_temp_file(
            file_bytes,
            suffix=ext,
        )

        try:
            result = list(self.pipeline(audio_path))[0]
        finally:
            os.unlink(audio_path)

        segments: List[Dict[str, Any]] = []
        for item in result["result"]["segments"]:
            segment = dict(
                id=item["id"],
                seek=item["seek"],
                start=item["start"],
                end=item["end"],
                text=item["text"],
                tokens=item["tokens"],
                temperature=item["temperature"],
                avgLogProb=item["avg_logprob"],
                compressionRatio=item["compression_ratio"],
                noSpeechProb=item["no_speech_prob"],
            )
            segments.append(segment)

        return schemas.multilingual_speech_recognition.InferResult(
            text=result["result"]["text"],
            segments=segments,
            language=result["result"]["language"],
        )
