soiz commited on
Commit
be5673e
·
verified ·
1 Parent(s): 3de8a3a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
3
+ import torch
4
+
5
+ # モデルとプロセッサの読み込み
6
+ model_name = "openai/whisper-large-v3"
7
+ processor = WhisperProcessor.from_pretrained(model_name)
8
+ model = WhisperForConditionalGeneration.from_pretrained(model_name)
9
+
10
+ # 音声ファイルを文字起こしする関数
11
+ def transcribe_audio(audio):
12
+ # 音声を処理
13
+ audio_input = processor(audio, return_tensors="pt", sampling_rate=16000)
14
+
15
+ # モデルによる文字起こし
16
+ with torch.no_grad():
17
+ predicted_ids = model.generate(input_ids=audio_input.input_ids)
18
+
19
+ # 文字起こし結果のデコード
20
+ transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
21
+
22
+ return transcription
23
+
24
+ # Gradioのインターフェース作成
25
+ interface = gr.Interface(
26
+ fn=transcribe_audio,
27
+ inputs=gr.Audio(source="microphone", type="filepath"),
28
+ outputs="text",
29
+ live=True
30
+ )
31
+
32
+ # インターフェースの起動
33
+ interface.launch()