faster_whisper调用whisper-large-v3-turbo模型的坑
Please provide either the path to a local folder or the repo_id of a model on the Hub.
·
从Hugging Face下载whisper-large-v3-turbo模型。没有科学上网的可以去魔塔社区。
下载的模型目录:
我是全下载了,可能用不到这么多。
下载的模型文件是pytorch格式,如果你想用faster_whisper库调用模型,需要转换成ctranslate2格式。
没有ctranslate2库自行pip下载。
转换代码不要用这个:
model = WhisperForConditionalGeneration.from_pretrained(model_path)
processor = WhisperProcessor.from_pretrained(model_path)
# 配置转换参数
output_dir = r"./wisper/converted_whisper_large_v3_turbo"
# 创建转换器
converter = ctranslate2.converters.TransformersConverter(model, processor.tokenizer)
# 执行转换操作,将 quantization 设置为 None 以使用全精度
converter.convert(output_dir, quantization=None)
直接加载原始的 Whisper 模型和处理器,会报错:Please provide either the path to a local folder or the repo_id of a model on the Hub.
用这个:
from ctranslate2.converters import TransformersConverter
# 定义模型参数
model_name = "目录\whisper-large-v3" # Hugging Face 模型名称
output_dir = "目录\converted_whisper_large_v3" # 转换后的模型保存路径
quantization = "float32" # 量化选项:float32/float16/int8/int8_float16
# 执行转换
converter = TransformersConverter(model_name)
converter.convert(
output_dir=output_dir,
quantization=quantization,
force=True # 覆盖已有目录
)
print(f"模型已成功转换为 CTranslate2 格式,保存在 {output_dir}")
转换完成后,目录内应该是三个文件
config.json
model.bin
vocabulary.json
这时候把pytorch格式的模型目录内的:
tokenizer.json
preprocessor_config.json
粘贴到转换后的模型文件内
没有这两个文件,调用模型后,导入音频文件会报错:
ValueError: Invalid input features shape: expected an input with shape (1, 128, 3000), but got an input with shape (1, 80, 3000) instead
输入维度有问题。
测试代码
import os
from faster_whisper import WhisperModel
def test_whisper_large_v3_turbo():
try:
# 初始化模型,使用 GPU 进行计算,如果没有 GPU 可以将 device 改为 "cpu"
# model = WhisperModel("whisper-large-v3-turbo", device="cuda", compute_type="float16",local_files_only = True)
# model = WhisperModel(r"Whisper-large-v3-turbo", device="cuda", compute_type="float16")
model = WhisperModel(r"目录\converted_whisper_large_v3_turbo"
, device="cuda", compute_type="float32")
# 可以使用一个简短的示例音频文件进行测试,这里假设存在一个名为 test.wav 的音频文件
audio_path = "test.wav"
if not os.path.exists(audio_path):
print(f"示例音频文件 {audio_path} 不存在,请提供有效的音频文件路径。")
return
# 进行转录
segments, info = model.transcribe(audio_path, beam_size=5)
print("模型成功运行!")
print(f"检测到的语言: {info.language},概率: {info.language_probability}")
for segment in segments:
print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}")
except Exception as e:
print(f"模型运行出错: {e}")
if __name__ == "__main__":
test_whisper_large_v3_turbo()
更多推荐
所有评论(0)