Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,47 @@
|
|
| 1 |
-
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
os.system("pip install git+https://github.com/openai/whisper.git")
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import whisper
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
model = whisper.load_model("base")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
def inference(audio):
|
|
|
|
| 14 |
audio = whisper.load_audio(audio)
|
|
|
|
|
|
|
| 15 |
audio = whisper.pad_or_trim(audio)
|
| 16 |
|
|
|
|
| 17 |
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
| 18 |
|
|
|
|
| 19 |
_, probs = model.detect_language(mel)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
result = whisper.decode(model, mel, options)
|
| 23 |
|
|
|
|
| 24 |
return result.text
|
| 25 |
|
|
|
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=inference,
|
| 28 |
-
inputs=gr.Audio(type="filepath", label="
|
| 29 |
outputs="text"
|
| 30 |
)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
iface.launch()
|
|
|
|
| 1 |
+
# 匯入必要的函式庫
|
| 2 |
import os
|
| 3 |
+
|
| 4 |
+
# 從OpenAI的GitHub儲存庫安裝Whisper函式庫
|
| 5 |
os.system("pip install git+https://github.com/openai/whisper.git")
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# 匯入用於建立使用者介面的Gradio函式庫
|
| 8 |
+
import gradio as gr
|
| 9 |
|
| 10 |
+
# 匯入Whisper函式庫
|
| 11 |
+
import whisper
|
| 12 |
|
| 13 |
+
# 載入名為"base"的預先訓練Whisper模型
|
| 14 |
model = whisper.load_model("base")
|
| 15 |
|
| 16 |
+
# 定義使用Whisper進行語言推斷的函數
|
|
|
|
| 17 |
def inference(audio):
|
| 18 |
+
# 使用Whisper載入音訊檔案
|
| 19 |
audio = whisper.load_audio(audio)
|
| 20 |
+
|
| 21 |
+
# 確保音訊已正確填充或修剪
|
| 22 |
audio = whisper.pad_or_trim(audio)
|
| 23 |
|
| 24 |
+
# 從音訊中提取對數梅爾頻譜圖並將其發送到模型的設備
|
| 25 |
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
| 26 |
|
| 27 |
+
# 使用Whisper模型檢測語言並獲取機率
|
| 28 |
_, probs = model.detect_language(mel)
|
| 29 |
|
| 30 |
+
# 設定Whisper的解碼選項
|
| 31 |
+
options = whisper.DecodingOptions(fp16=False)
|
| 32 |
+
|
| 33 |
+
# 解碼梅爾頻譜圖以獲取推斷文本
|
| 34 |
result = whisper.decode(model, mel, options)
|
| 35 |
|
| 36 |
+
# 返回推斷的文本
|
| 37 |
return result.text
|
| 38 |
|
| 39 |
+
# 使用音訊輸入和文本輸出創建Gradio介面
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=inference,
|
| 42 |
+
inputs=gr.Audio(type="filepath", label="支援格式:WAV、MP3、OGG、FLAC、AAC、M4A、WMA。支援單聲道和多聲道。"),
|
| 43 |
outputs="text"
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# 啟動Gradio介面
|
| 47 |
iface.launch()
|