File size: 1,073 Bytes
be5673e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9dfad5c
be5673e
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gradio as gr
from transformers import WhisperProcessor, WhisperForConditionalGeneration
import torch

# モデルとプロセッサの読み込み
model_name = "openai/whisper-large-v3"
processor = WhisperProcessor.from_pretrained(model_name)
model = WhisperForConditionalGeneration.from_pretrained(model_name)

# 音声ファイルを文字起こしする関数
def transcribe_audio(audio):
    # 音声を処理
    audio_input = processor(audio, return_tensors="pt", sampling_rate=16000)
    
    # モデルによる文字起こし
    with torch.no_grad():
        predicted_ids = model.generate(input_ids=audio_input.input_ids)
    
    # 文字起こし結果のデコード
    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()