
import os
import uuid
from funasr import AutoModel

from app.utils.pinyin_utils import replace_word

target_word = "抱坡"
# 模型1
model = AutoModel(model="E:\\yuyin_model\\Voice_translation", model_revision="v2.0.4",
                  vad_model="E:\\yuyin_model\\Endpoint_detection", vad_model_revision="v2.0.4",
                  punc_model="E:\\yuyin_model\\Ct_punc", punc_model_revision="v2.0.4",
                  use_cuda=True, use_fast=True,
                  )


def parse_file(file):
    # 文件保存路径
    UPLOAD_FOLDER = 'data/audio'
    os.makedirs(UPLOAD_FOLDER, exist_ok=True)

    # 生成UUID文件名
    file_ext = os.path.splitext(file.filename)[1]
    filename = f"{uuid.uuid4()}{file_ext}"

    # 保存文件
    file_path = os.path.join(UPLOAD_FOLDER, filename)
    file.save(file_path)

    # 语音转文字模型1
    res = model.generate(file_path,
                         batch_size_s=30, hotword='test')
    texts = [item['text'] for item in res]
    msg = ' '.join(texts)
    # print(msg)
    # 语音转文字模型2
    # res = inference_pipeline(file_path)
    # # print(res)
    # texts = [item['text'] for item in res]
    # # print(texts)
    # msg = ' '.join(texts)

    # msg = vocal_text(file_path)
    os.remove(file_path)
    msg = replace_word(msg, target_word)
    words_to_replace = ["爆破", "爆坡", "高坡"]
    for word in words_to_replace:
        msg = msg.replace(word, "抱坡")
    print(msg)
    return {"msg": "上传成功",
            "code": 200,
            "filename": filename,
            "voiceMsg": msg
            }
