跳過到頁腳內容
PYTHON 幫助

在 Python 中使用 WhisperX 进行转录

Python已經鞏固了其作為世界上最具通用性和強大編程語言之一的地位,這在很大程度上歸功於其龐大的庫和框架生態系統。 在機器學習和自然語言處理(NLP)領域引起轟動的一個此類庫為 WhisperX。 在本文中,我們將探討 WhisperX 是什麼,其主要功能以及如何在各種應用中使用它。 此外,我們還將介紹另一個功能強大的 Python 庫 IronPDF,並通過一個實用的代碼示例展示如何將其與 WhisperX 配合使用。

What is WhisperX?

WhisperX 是一個專為語音識別和 NLP 任務設計的高級 Python 庫。 它利用最先進的機器學習模型將口語轉換為書面文本,具有高精度語言檢測和時間精確的語音轉錄功能。 WhisperX 在需要實時翻譯的應用中尤其有用,例如虛擬助手、自動化客戶服務系統和轉錄服務。

WhisperX 的主要功能

  1. 高準確性:WhisperX 使用尖端算法和大型數據集訓練其模型,確保語音識別的高準確性。
  2. 實時處理:該庫經過優化,可進行實時處理,非常適合需要立即轉錄和響應的應用。
  3. 語言支持:WhisperX 支持多種語言,滿足全球觀眾和各種用途需求。
  4. 易於集成:通過其完善的 API 文檔,WhisperX 可以輕鬆集成到現有的 Python 應用中。
  5. 可定制性:用戶可以微調模型,以更好地適應特定的口音、方言和術語。

開始使用 WhisperX

要開始使用 WhisperX,您需要安裝該庫。 這可以通過 Python 包安裝器 pip 完成。 假設您已安裝 Python 和 pip,可以使用以下命令安裝 WhisperX:

pip install whisperx
pip install whisperx
SHELL

WhisperX 的基本用法 - 快速自動語音識別

以下是一個基本示例,演示如何使用 WhisperX 轉錄音頻文件:

import whisperx

# Initialize the WhisperX recognizer
recognizer = whisperx.Recognizer()

# Load your audio
audio_file = "path_to_your_audio_file.wav"

# Perform transcription
transcription = recognizer.transcribe(audio_file)

# Print the transcription
print("Transcription:", transcription)
import whisperx

# Initialize the WhisperX recognizer
recognizer = whisperx.Recognizer()

# Load your audio
audio_file = "path_to_your_audio_file.wav"

# Perform transcription
transcription = recognizer.transcribe(audio_file)

# Print the transcription
print("Transcription:", transcription)
PYTHON

這個簡單的示例展示了如何初始化 WhisperX 識別器、加載音頻並進行轉錄,以高精度將口語轉換為文本。

WhisperX Python (開發者如何使用):圖 1 - 檢測到的語言輸出

WhisperX 的高級功能

WhisperX 還提供了高級功能,例如說話者識別,這在多說話者環境中可能非常重要。 以下是一個如何使用此功能的示例:

import whisperx

# Initialize the WhisperX recognizer with speaker identification enabled
recognizer = whisperx.Recognizer(speaker_identification=True)

# Load your audio file
audio_file = "path_to_your_audio_file.wav"

# Perform transcription with speaker identification
transcription, speakers = recognizer.transcribe(audio_file)

# Print the transcription with speaker labels
for i, segment in enumerate(transcription):
    print(f"Speaker {speakers[i]}: {segment}")
import whisperx

# Initialize the WhisperX recognizer with speaker identification enabled
recognizer = whisperx.Recognizer(speaker_identification=True)

# Load your audio file
audio_file = "path_to_your_audio_file.wav"

# Perform transcription with speaker identification
transcription, speakers = recognizer.transcribe(audio_file)

# Print the transcription with speaker labels
for i, segment in enumerate(transcription):
    print(f"Speaker {speakers[i]}: {segment}")
PYTHON

在此示例中,WhisperX 不僅轉錄音頻,還識別了不同的說話者,並相應地標記每個片段。

IronPDF for Python

雖然 WhisperX 處理音頻到文本的轉錄,但通常需要以結構化和專業的格式呈現這些數據。 這就是 IronPDF for Python 發揮作用的地方。 IronPDF 是一個強大的庫,用於以編程方式生成、編輯和操作 PDF 文檔。 它使開發人員能夠從頭開始生成 PDF,將 HTML 轉換為 PDF 等。

安裝 IronPDF

可以使用 pip 安裝 IronPDF:

pip install ironpdf
pip install ironpdf
SHELL

WhisperX Python (開發者如何使用):圖 2 - IronPDF

結合使用 WhisperX 和 IronPDF

現在讓我們創建一個實用示例,演示如何使用 WhisperX 轉錄音頻文件,然後使用 IronPDF 生成包含轉錄內容的 PDF 文檔。

import whisperx
from ironpdf import IronPdf

# Initialize the WhisperX recognizer
recognizer = whisperx.Recognizer()

# Load your audio file
audio_file = "path_to_your_audio_file.wav"

# Perform transcription
transcription = recognizer.transcribe(audio_file)

# Create a PDF document using IronPDF
renderer = IronPdf.ChromePdfRenderer()
pdf_from_html = renderer.RenderHtmlAsPdf(f"<h1>Transcription</h1><p>{transcription}</p>")

# Save the PDF to a file
output_file = "transcription_output.pdf"
pdf_from_html.save(output_file)
print(f"Transcription saved to {output_file}")
import whisperx
from ironpdf import IronPdf

# Initialize the WhisperX recognizer
recognizer = whisperx.Recognizer()

# Load your audio file
audio_file = "path_to_your_audio_file.wav"

# Perform transcription
transcription = recognizer.transcribe(audio_file)

# Create a PDF document using IronPDF
renderer = IronPdf.ChromePdfRenderer()
pdf_from_html = renderer.RenderHtmlAsPdf(f"<h1>Transcription</h1><p>{transcription}</p>")

# Save the PDF to a file
output_file = "transcription_output.pdf"
pdf_from_html.save(output_file)
print(f"Transcription saved to {output_file}")
PYTHON

結合代碼示例的解釋

  1. 使用 WhisperX 進行轉錄

    • 初始化 WhisperX 識別器並加載音頻文件。
    • transcribe 方法處理音頻並返回轉錄內容。
  2. 使用 IronPDF 創建 PDF

    • 創建 IronPdf.ChromePdfRenderer 的實例。
    • 使用 RenderHtmlAsPdf 方法,將包含轉錄文本的 HTML 格式字符串添加到 PDF 中。
    • save 方法將 PDF 寫入文件。

WhisperX Python (開發者如何使用):圖 3 - PDF 輸出

此綜合示例展示了如何利用 WhisperX 和 IronPDF 的優勢來創建一個完整的解決方案,該解決方案可轉錄音頻並生成包含轉錄內容的 PDF 文檔。

結論

WhisperX 是任何希望在其應用中實現語音識別、說話者對話和轉錄功能的人的強大工具。 其高度準確性、實時處理能力以及多語言支持使其成為 NLP 領域的重要資產。另一方面,IronPDF 提供了一種無縫的方式來以編程方式創建和操作 PDF 文檔。 通過結合使用 WhisperX 和 IronPDF,開發人員可以創建綜合解決方案,不僅轉錄音頻,還能以精緻、專業的格式呈現轉錄內容。

無論您是在構建虛擬助手、客戶服務聊天機器人,還是轉錄服務,WhisperX 和 IronPDF 提供了增強您應用能力所需的工具,並為用戶提供高質量的結果。

要獲得有關 IronPDF 授權的更多詳細信息,請訪問 IronPDF 授權頁面。 此外,我們的 HTML 到 PDF 轉換詳細教程可供進一步探索。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。