shuaige / app.py
wang0507's picture
Update app.py
215bf18
# 匯入必要的函式庫
import os
# 從OpenAI的GitHub儲存庫安裝Whisper函式庫
os.system("pip install git+https://github.com/openai/whisper.git")
# 匯入用於建立使用者介面的Gradio函式庫
import gradio as gr
# 匯入Whisper函式庫
import whisper
# 載入名為"base"的預先訓練Whisper模型
model = whisper.load_model("base")
# 定義使用Whisper進行語言推斷的函數
def inference(audio):
# 使用Whisper載入音訊檔案
audio = whisper.load_audio(audio)
# 確保音訊已正確填充或修剪
audio = whisper.pad_or_trim(audio)
# 從音訊中提取對數梅爾頻譜圖並將其發送到模型的設備
mel = whisper.log_mel_spectrogram(audio).to(model.device)
# 使用Whisper模型檢測語言並獲取機率
_, probs = model.detect_language(mel)
# 設定Whisper的解碼選項
options = whisper.DecodingOptions(fp16=False)
# 解碼梅爾頻譜圖以獲取推斷文本
result = whisper.decode(model, mel, options)
# 返回推斷的文本
return result.text
# 使用音訊輸入和文本輸出創建Gradio介面
iface = gr.Interface(
fn=inference,
inputs=gr.Audio(type="filepath", label="支援格式:WAV、MP3、OGG、FLAC、AAC、M4A、WMA。支援單聲道和多聲道。"),
outputs="text"
)
# 啟動Gradio介面
iface.launch()