soiz commited on
Commit
7494bfb
·
verified ·
1 Parent(s): 8ff5286

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -35
app.py CHANGED
@@ -1,40 +1,24 @@
1
  import gradio as gr
2
- from transformers import WhisperProcessor, WhisperForConditionalGeneration
3
- import torch
4
- import librosa
5
 
6
- # モデルとプロセッサの読み込み
7
- model_name = "openai/whisper-large-v3"
8
- processor = WhisperProcessor.from_pretrained(model_name)
9
- model = WhisperForConditionalGeneration.from_pretrained(model_name)
10
 
11
- # 音声ファイルを文字起こしする関数
12
- def transcribe_audio(audio):
13
- # librosaで音声を読み込む
14
- audio_data, sampling_rate = librosa.load(audio, sr=16000)
 
 
15
 
16
- # WhisperProcessorに渡すために、音声データを正しい形式に変換
17
- audio_input = processor(audio_data, return_tensors="pt", sampling_rate=16000)
 
 
 
 
 
18
 
19
- # input_features を取得
20
- input_features = audio_input["input_features"]
21
-
22
- # モデルによる文字起こし
23
- with torch.no_grad():
24
- predicted_ids = model.generate(input_features=input_features)
25
-
26
- # 文字起こし結果のデコード
27
- transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
28
-
29
- return transcription
30
-
31
- # Gradioのインターフェース作成
32
- interface = gr.Interface(
33
- fn=transcribe_audio,
34
- inputs=gr.Audio(type="filepath"), # マイクやファイルから音声を入力
35
- outputs="text",
36
- live=True
37
- )
38
-
39
- # インターフェースの起動
40
- interface.launch(share=True)
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
 
3
 
4
+ # Whisperモデルを読み込み
5
+ model_name = "openai/whisper-small" # 他のサイズも使用可能(tiny, base, largeなど)
6
+ transcriber = pipeline("automatic-speech-recognition", model=model_name)
 
7
 
8
+ def transcribe(audio):
9
+ """
10
+ 音声ファイルを文字起こしする関数
11
+ """
12
+ result = transcriber(audio)
13
+ return result["text"]
14
 
15
+ # Gradioインターフェース
16
+ with gr.Blocks() as demo:
17
+ gr.Markdown("### 音声文字起こしデモ")
18
+ audio_input = gr.Audio(source="upload", type="filepath", label="音声ファイルをアップロード")
19
+ text_output = gr.Textbox(label="文字起こし結果")
20
+ transcribe_button = gr.Button("文字起こし")
21
+ transcribe_button.click(transcribe, inputs=audio_input, outputs=text_output)
22
 
23
+ # アプリケーションを起動
24
+ demo.launch()