file_service.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import uuid
  3. from funasr import AutoModel
  4. from app.utils.pinyin_utils import replace_word
  5. target_word = "抱坡"
  6. # 模型1
  7. model = AutoModel(model="E:\\yuyin_model\\Voice_translation", model_revision="v2.0.4",
  8. vad_model="E:\\yuyin_model\\Endpoint_detection", vad_model_revision="v2.0.4",
  9. punc_model="E:\\yuyin_model\\Ct_punc", punc_model_revision="v2.0.4",
  10. use_cuda=True, use_fast=True,
  11. )
  12. def parse_file(file):
  13. # 文件保存路径
  14. UPLOAD_FOLDER = 'data/audio'
  15. os.makedirs(UPLOAD_FOLDER, exist_ok=True)
  16. # 生成UUID文件名
  17. file_ext = os.path.splitext(file.filename)[1]
  18. filename = f"{uuid.uuid4()}{file_ext}"
  19. # 保存文件
  20. file_path = os.path.join(UPLOAD_FOLDER, filename)
  21. file.save(file_path)
  22. # 语音转文字模型1
  23. res = model.generate(file_path,
  24. batch_size_s=30, hotword='test')
  25. texts = [item['text'] for item in res]
  26. msg = ' '.join(texts)
  27. # print(msg)
  28. # 语音转文字模型2
  29. # res = inference_pipeline(file_path)
  30. # # print(res)
  31. # texts = [item['text'] for item in res]
  32. # # print(texts)
  33. # msg = ' '.join(texts)
  34. # msg = vocal_text(file_path)
  35. os.remove(file_path)
  36. msg = replace_word(msg, target_word)
  37. words_to_replace = ["爆破", "爆坡", "高坡"]
  38. for word in words_to_replace:
  39. msg = msg.replace(word, "抱坡")
  40. print(msg)
  41. return {"msg": "上传成功",
  42. "code": 200,
  43. "filename": filename,
  44. "voiceMsg": msg
  45. }