如何使用OpenAIS耳語轉錄和診斷音頻文件
Whisper是OpenAI的最先進的語音識別系統,已接受了從網絡收集的680,000小時的多語言和多任務監督數據的培訓。這個大型多樣的數據集可改善對重音,背景噪音和技術語言的魯棒性。此外,它可以用多種語言進行轉錄,並從這些語言轉換為英語。 Openai發布了模型和代碼,以作為構建利用語音識別的有用應用的基礎。
不過,耳語的一個很大的缺點是,它無法告訴您誰在談話中說話。分析對話時,這是一個問題。這是診斷的地方。診斷是識別在對話中說話的過程。
在本教程中,您將學習如何識別說話者,然後將其與耳語的抄錄匹配。我們將使用pyannote-audio
來實現這一目標。讓我們開始吧!
首先,我們需要準備音頻文件。我們將使用Yann下載的Lex Fridmans播客的前20分鐘。要下載視頻並提取音頻,我們將使用yt-dlp
軟件包。
! pip install -U yt-dlp
我們還需要安裝FFMPEG
! wget -O - -q https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz | xz -qdc | tar -x
現在,我們可以通過命令行進行實際下載和音頻提取。
! yt-dlp -xv --ffmpeg-location ffmpeg-master-latest-linux64-gpl/bin --audio-format wav -o download.wav -- https://youtu.be/SGzMElJ11Cc
現在,我們的工作目錄中有download.wav
文件。讓我們剪切音頻的前20分鐘。我們只需幾行代碼即可使用PYDUB軟件包。
! pip install pydub
from pydub import AudioSegment
t1 = 0 * 1000 # works in milliseconds
t2 = 20 * 60 * 1000
newAudio = AudioSegment . from_wav ( "download.wav" )
a = newAudio [ t1 : t2 ]
a . export ( "audio.wav" , format = "wav" )
audio.wav
現在是音頻文件的前20分鐘。
pyannote.audio
是用Python編寫的用於說話者診斷的開源工具包。基於Pytorch機器學習框架,它提供了一組可訓練的端到端神經構建塊,可以將其組合併共同優化以構建揚聲器診斷管道。 pyannote.audio
還介紹了驗證的模型和管道,涵蓋了各種域,用於語音活動檢測,揚聲器細分,重疊的語音檢測,揚聲器嵌入了大多數人的最先進性能。
安裝pyannote並將其運行在視頻音頻上以生成腹瀉。
! pip install pyannote.audio
from pyannote . audio import Pipeline
pipeline = Pipeline . from_pretrained ( 'pyannote/speaker-diarization' )
DEMO_FILE = { 'uri' : 'blabal' , 'audio' : 'audio.wav' }
dz = pipeline ( DEMO_FILE )
with open ( "diarization.txt" , "w" ) as text_file :
text_file . write ( str ( dz ))
讓我們打印出來,看看它的外觀。
print(*list(dz.itertracks(yield_label = True))[:10], sep="n")
輸出:
(<Segment(2.03344, 36.8128)>, 0, 'SPEAKER_00')
(<Segment(38.1122, 51.3759)>, 0, 'SPEAKER_00')
(<Segment(51.8653, 90.2053)>, 1, 'SPEAKER_01')
(<Segment(91.2853, 92.9391)>, 1, 'SPEAKER_01')
(<Segment(94.8628, 116.497)>, 0, 'SPEAKER_00')
(<Segment(116.497, 124.124)>, 1, 'SPEAKER_01')
(<Segment(124.192, 151.597)>, 1, 'SPEAKER_01')
(<Segment(152.018, 179.12)>, 1, 'SPEAKER_01')
(<Segment(180.318, 194.037)>, 1, 'SPEAKER_01')
(<Segment(195.016, 207.385)>, 0, 'SPEAKER_00')
這看起來已經很好,但是讓我們清理一些數據:
def millisec ( timeStr ):
spl = timeStr . split ( ":" )
s = ( int )(( int ( spl [ 0 ]) * 60 * 60 + int ( spl [ 1 ]) * 60 + float ( spl [ 2 ]) ) * 1000 )
return s
import re
dz = open ( 'diarization.txt' ). read (). splitlines ()
dzList = []
for l in dz :
start , end = tuple ( re . findall ( '[0-9]+:[0-9]+:[0-9]+.[0-9]+' , string = l ))
start = millisec ( start ) - spacermilli
end = millisec ( end ) - spacermilli
lex = not re . findall ( 'SPEAKER_01' , string = l )
dzList . append ([ start , end , lex ])
print ( * dzList [: 10 ], sep = ' n ' )
[33, 34812, True]
[36112, 49375, True]
[49865, 88205, False]
[89285, 90939, False]
[92862, 114496, True]
[114496, 122124, False]
[122191, 149596, False]
[150018, 177119, False]
[178317, 192037, False]
[193015, 205385, True]
現在,我們在列表中有診斷數據。前兩個數字是毫秒毫秒的揚聲器段的開始和結束時間。第三個數字是一個布爾值,告訴我們說話者是否是Lex。
接下來,我們將根據診斷將音頻段連接起來,並將隔離劑作為定界符。
from pydub import AudioSegment
import re
sounds = spacer
segments = []
dz = open ( 'diarization.txt' ). read (). splitlines ()
for l in dz :
start , end = tuple ( re . findall ( '[0-9]+:[0-9]+:[0-9]+.[0-9]+' , string = l ))
start = int ( millisec ( start )) #milliseconds
end = int ( millisec ( end )) #milliseconds
segments . append ( len ( sounds ))
sounds = sounds . append ( audio [ start : end ], crossfade = 0 )
sounds = sounds . append ( spacer , crossfade = 0 )
sounds . export ( "dz.wav" , format = "wav" ) #Exports to a wav file in the current path.
print ( segments [: 8 ])
[2000, 38779, 54042, 94382, 98036, 121670, 131297, 160702]
接下來,我們將使用竊竊私語來抄錄音頻文件的不同段。重要的是:與pyannote.audio發生的版本衝突導致錯誤。我們的解決方法是首先運行Pyannote,然後小聲說。您可以安全地忽略錯誤。
安裝打開AI耳語。
! pip install git+https://github.com/openai/whisper.git
在準備好的音頻文件上打開AI竊竊私語。它將轉錄寫入文件中。您可以根據自己的需求調整模型大小。您可以在GitHub上的型號卡上找到所有型號。
! whisper dz.wav --language en --model base
[00:00.000 --> 00:04.720] The following is a conversation with Yann LeCun,
[00:04.720 --> 00:06.560] his second time on the podcast.
[00:06.560 --> 00:11.160] He is the chief AI scientist at Meta, formerly Facebook,
[00:11.160 --> 00:15.040] professor at NYU, touring award winner,
[00:15.040 --> 00:17.600] one of the seminal figures in the history
[00:17.600 --> 00:20.460] of machine learning and artificial intelligence,
...
為了使用.VTT文件,我們需要安裝WebVtt-PY庫。
! pip install -U webvtt-py
讓我們看一下數據:
import webvtt
captions = [[( int )( millisec ( caption . start )), ( int )( millisec ( caption . end )), caption . text ] for caption in webvtt . read ( 'dz.wav.vtt' )]
print ( * captions [: 8 ], sep = ' n ' )
[0, 4720, 'The following is a conversation with Yann LeCun,']
[4720, 6560, 'his second time on the podcast.']
[6560, 11160, 'He is the chief AI scientist at Meta, formerly Facebook,']
[11160, 15040, 'professor at NYU, touring award winner,']
[15040, 17600, 'one of the seminal figures in the history']
[17600, 20460, 'of machine learning and artificial intelligence,']
[20460, 23940, 'and someone who is brilliant and opinionated']
[23940, 25400, 'in the best kind of way,']
...
接下來,我們將每條轉錄行與某些診斷匹配,並通過生成HTML文件來顯示所有內容。為了獲得正確的時機,我們應該照顧原始音頻中沒有診斷段的零件。我們為音頻中的每個細分市場附加了一個新的DIV。
# we need this fore our HTML file (basicly just some styling)
preS = '<!DOCTYPE html>n<html lang="en">n <head>n <meta charset="UTF-8">n <meta name="viewport" content="width=device-width, initial-scale=1.0">n <meta http-equiv="X-UA-Compatible" content="ie=edge">n <title>Lexicap</title>n <style>n body {n font-family: sans-serif;n font-size: 18px;n color: #111;n padding: 0 0 1em 0;n }n .l {n color: #050;n }n .s {n display: inline-block;n }n .e {n display: inline-block;n }n .t {n display: inline-block;n }n #player {nttposition: sticky;ntttop: 20px;nttfloat: right;nt}n </style>n </head>n <body>n <h2>Yann LeCun: Dark Matter of Intelligence and Self-Supervised Learning | Lex Fridman Podcast #258</h2>n <div id="player"></div>n <script>n var tag = document.createElement('script');n tag.src = "https://www.youtube.com/iframe_api";n var firstScriptTag = document.getElementsByTagName('script')[0];n firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);n var player;n function onYouTubeIframeAPIReady() {n player = new YT.Player('player', {n height: '210',n width: '340',n videoId: 'SGzMElJ11Cc',n });n }n function setCurrentTime(timepoint) {n player.seekTo(timepoint);n player.playVideo();n }n </script><br>n'
postS = 't</body>n</html>'
from datetime import timedelta
html = list(preS)
for i in range(len(segments)):
idx = 0
for idx in range(len(captions)):
if captions[idx][0] >= (segments[i] - spacermilli):
break;
while (idx < (len(captions))) and ((i == len(segments) - 1) or (captions[idx][1] < segments[i+1])):
c = captions[idx]
start = dzList[i][0] + (c[0] -segments[i])
if start < 0:
start = 0
idx += 1
start = start / 1000.0
startStr = '{0:02d}:{1:02d}:{2:02.2f}'.format((int)(start // 3600),
(int)(start % 3600 // 60),
start % 60)
html.append('ttt<div class="c">n')
html.append(f'tttt<a class="l" href="#{startStr}" id="{startStr}">link</a> |n')
html.append(f'tttt<div class="s"><a href="javascript:void(0);" onclick=setCurrentTime({int(start)})>{startStr}</a></div>n')
html.append(f'tttt<div class="t">{"[Lex]" if dzList[i][2] else "[Yann]"} {c[2]}</div>n')
html.append('ttt</div>nn')
html.append(postS)
s = "".join(html)
with open("lexicap.html", "w") as text_file:
text_file.write(s)
print(s)
在Lablab Discord上,我們討論了此回購以及與人工智能有關的許多其他主題!結帳即將舉行的人工智能黑客馬拉鬆活動