从Hugging Face下载数据测试whisper、fast_whisper耗时

文章介绍了如何在Python中使用fast_whisper库进行音频转录,包括解决因找不到libcudnn_ops_infer.so.8文件而产生的错误,以及设置HTTP和HTTPS代理。作者还提供了测试代码和处理过程中的观察结果,指出fast_whisper的速度优于其他方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时长比较短的音频:https://huggingface.co/datasets/PolyAI/minds14/viewer/en-US

时长比较长的音频:https://huggingface.co/datasets/librispeech_asr?row=8

此次测试过程暂时只使用比较短的音频

使用fast_whisper测试

下载安装,参考官方网站即可

 报错提示:

Could not load library libcudnn_ops_infer.so.8. Error: libcudnn_ops_infer.so.8: cannot open shared object file: No such file or directory
Please make sure libcudnn_ops_infer.so.8 is in your library path!

解决办法:

找到有libcudnn_ops_infer.so.8 的路径,在我的电脑中,改文件所在的路径为

在终端导入  export LD_LIBRARY_PATH=/opt/audio/venv/lib/python3.10/site-packages/nvidia/cudnn/lib:$LD_LIBRARY_PATH

test_fast_whisper.py


import subprocess
import os
import time
import unittest
import openpyxl
from pydub import AudioSegment
from datasets import load_dataset

from faster_whisper import WhisperModel

class TestFastWhisper(unittest.TestCase):

    def setUp(self):
        pass
    def test_fastwhisper(self):
        # 替换为您的脚本路径
        
        # 设置HTTP代理
        os.environ["http_proxy"] = "http://10.10.10.178:7890"
        os.environ["HTTP_PROXY"] = "http://10.10.10.178:7890"
        # 不知道此处为什么不能生效,必须要在终端中手动导入
        os.environ["LD_LIBRARY_PATH"] = "/opt/audio/venv/lib/python3.10/site-packages/nvidia/cudnn/lib:$LD_LIBRARY_PATH"
        
        # 设置HTTPS代理
        os.environ["https_proxy"] = "http://10.10.10.178:7890"
        os.environ["HTTPS_PROXY"] = "http://10.10.10.178:7890"
        print("load whisper")
        # 使用fast_whisper 
        model_size = "large-v2"

        # Run on GPU with FP16
        fast_whisper_model = WhisperModel(model_size, device="cuda", compute_type="float16")
        minds_14 = load_dataset("PolyAI/minds14", "en-US", split="train")  # for en-US
        
        workbook = openpyxl.Workbook()
            # 创建一个工作表
        worksheet = workbook.active
        # 设置表头
        worksheet["A1"] = "Audio Path"
        worksheet["B1"] = "Audio Duration (seconds)"
        worksheet["C1"] = "Audio Size (MB)"
        worksheet["D1"] = "Correct Text"
        worksheet["E1"] = "Transcribed Text"
        worksheet["F1"] = "Cost Time (seconds)"
        for index, each in enumerate(minds_14, start=2):
            audioPath = each["path"]
            print(audioPath)
            # audioArray = each["audio"]
            audioDuration = len(AudioSegment.from_file(audioPath))/1000
            audioSize = os.path.getsize(audioPath)/ (1024 * 1024)
            CorrectText = each["transcription"]
            tran_start_time = time.time()
            segments, info = fast_whisper_model.transcribe(audioPath, beam_size=5)
            segments = list(segments)  # The transcription will actually run here.
            print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
            text = ""
            for segment in segments:
                text += segment.text
            cost_time =  time.time() - tran_start_time
            print("Audio Path:", audioPath)
            print("Audio Duration (seconds):", audioDuration)
            print("Audio Size (MB):", audioSize)
            print("Correct Text:", CorrectText)
            print("Transcription Time (seconds):", cost_time)
            print("Transcribed Text:", text)

            worksheet[f"A{index}"] = audioPath
            worksheet[f"B{index}"] = audioDuration
            worksheet[f"C{index}"] = audioSize
            worksheet[f"D{index}"] = CorrectText
            worksheet[f"E{index}"] = text
            worksheet[f"F{index}"] = cost_time
            # break
        workbook.save("fast_whisper_output_data.xlsx")
        print("数据已保存到 fast_whisper_output_data.xlsx 文件")
          
        
if __name__ == '__main__':
    unittest.main()

使用whisper测试

下载安装,参考官方网站即可,代码与上面代码类似

测试结果可视化

不太熟悉用numbers,凑合着看一下就行

很明显,fast_whisper速度要更快一些

### 使用 `huggingface_hub` 下载数据集 为了从 Hugging Face 下载数据集,可以利用 `datasets` 库,这是由 Hugging Face 提供的一个专门用于加载和处理各种数据集的工具[^2]。然而,在某些情况下也可以直接使用 `huggingface_hub` 来获取特定资源。 #### 安装必要的包 如果尚未安装 `huggingface_hub` 和 `datasets`,可以通过以下命令来完成安装: ```bash pip install huggingface_hub datasets ``` 对于 Conda 用户,则可执行如下指令: ```bash conda install -c conda-forge huggingface_hub datasets ``` #### 导入所需模块并设置认证令牌 在下载之前,可能需要先登录或者至少提供访问令牌以便于验证身份。这一步骤不是强制性的,但对于私有仓库来说是必需的。 ```python from huggingface_hub import login login(token="your_hugging_face_token_here") # 替换为自己的HF Token ``` #### 加载数据集 接下来展示如何使用 `datasets` 库中的 `load_dataset()` 函数来加载公共或私人拥有的数据集。这里给出一个简单的例子说明如何操作: ```python from datasets import load_dataset dataset_name = "glue" # 更改为想要的数据集名称 subset_name = "mrpc" # 如果适用的话,指定子集名;否则省略此参数 # 对公开可用的数据集调用无需额外参数即可工作 data_files = {"train": "./my_local_train_file.csv"} # 可选:指向本地文件路径字典 ds = load_dataset(dataset_name, subset_name, data_files=data_files) print(ds['train'][0]) # 打印训练集中第一个样本作为演示 ``` 当涉及到受保护的内容时(比如一些付费或许可制的数据),则需传递相应的凭证信息给 API 请求头中去。此时应该确保已经成功完成了前面提到的身份验证过程。 #### 验证安装版本 确认当前使用的库是否是最新的稳定版也很重要,可通过下面的方式查询已安装软件的具体版本号: ```python import huggingface_hub import datasets print(f"huggingface_hub version: {huggingface_hub.__version__}") print(f"datasets version: {datasets.__version__}") ``` 以上就是关于怎样借助 Python 的 `huggingface_hub` 和 `datasets` 工具从 Hugging Face 平台上抓取目标数据集的方法介绍[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值