soiz commited on
Commit
321acf4
·
verified ·
1 Parent(s): 00d8be3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -18
app.py CHANGED
@@ -1,24 +1,53 @@
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(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()
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import tempfile
3
+ from flask import Flask, request, jsonify
4
  from transformers import pipeline
5
 
6
+ app = Flask(__name__)
7
+
8
+ # Whisperモデルを準備
9
+ model_name = "openai/whisper-small"
10
  transcriber = pipeline("automatic-speech-recognition", model=model_name)
11
 
12
+ def decode_audio(data_url):
13
+ """
14
+ dataURLをデコードして一時ファイルに保存し、そのファイルパスを返す
15
+ """
16
+ # `dataURL`形式からヘッダーとデータ部分を分離
17
+ header, encoded = data_url.split(",", 1)
18
+ audio_data = base64.b64decode(encoded)
19
+
20
+ # 一時ファイルに保存
21
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
22
+ temp_audio_file.write(audio_data)
23
+ return temp_audio_file.name
24
+
25
+ @app.route('/transcribe', methods=['POST'])
26
+ def transcribe_audio():
27
  """
28
+ POSTリクエストで送信されたdataURLを文字起こし
29
  """
30
+ try:
31
+ # JSONからdataURLを取得
32
+ data = request.json
33
+ data_url = data.get("dataURL")
34
+ if not data_url:
35
+ return jsonify({"error": "Missing 'dataURL' in request"}), 400
36
+
37
+ # 音声データをデコードして一時ファイルパスを取得
38
+ audio_file_path = decode_audio(data_url)
39
+
40
+ # Whisperで文字起こし
41
+ result = transcriber(audio_file_path)
42
+ text = result["text"]
43
+
44
+ # 一時ファイルを削除(必要なら実装)
45
+ # os.remove(audio_file_path)
46
+
47
+ return jsonify({"transcription": text})
48
+
49
+ except Exception as e:
50
+ return jsonify({"error": str(e)}), 500
51
+
52
+ if __name__ == '__main__':
53
+ app.run(debug=True)