Spaces:
Running
Running
import gradio as gr | |
from transformers import WhisperProcessor, WhisperForConditionalGeneration | |
import torch | |
import librosa | |
# モデルとプロセッサの読み込み | |
model_name = "openai/whisper-large-v3" | |
processor = WhisperProcessor.from_pretrained(model_name) | |
model = WhisperForConditionalGeneration.from_pretrained(model_name) | |
# 音声ファイルを文字起こしする関数 | |
def transcribe_audio(audio): | |
# librosaで音声を読み込む | |
audio_data, sampling_rate = librosa.load(audio, sr=16000) | |
# WhisperProcessorに渡すために、音声データを正しい形式に変換 | |
# 返された辞書を表示して出力形式を確認 | |
audio_input = processor(audio_data, return_tensors="pt", sampling_rate=16000) | |
# 出力形式を確認 | |
print(audio_input) # デバッグ: 出力形式を確認 | |
# input_values ではなく、input_features を使用する場合もある | |
input_values = audio_input.get('input_values') or audio_input.get('input_features') | |
if input_values is None: | |
raise ValueError("音声データが適切に処理されていないか、必要なキーが見つかりませんでした") | |
# モデルによる文字起こし | |
with torch.no_grad(): | |
predicted_ids = model.generate(input_values=input_values) | |
# 文字起こし結果のデコード | |
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True) | |
return transcription | |
# Gradioのインターフェース作成 | |
interface = gr.Interface( | |
fn=transcribe_audio, | |
inputs=gr.Audio(type="filepath"), # マイクやファイルから音声を入力 | |
outputs="text", | |
live=True | |
) | |
# インターフェースの起動 | |
interface.launch(share=True) # `share=True`で公開リンクを生成 | |