File size: 1,648 Bytes
321acf4
 
 
7494bfb
be5673e
321acf4
 
 
 
7494bfb
be5673e
321acf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7494bfb
321acf4
7494bfb
321acf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e954b
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import base64
import tempfile
from flask import Flask, request, jsonify
from transformers import pipeline

app = Flask(__name__)

# Whisperモデルを準備
model_name = "openai/whisper-small"
transcriber = pipeline("automatic-speech-recognition", model=model_name)

def decode_audio(data_url):
    """
    dataURLをデコードして一時ファイルに保存し、そのファイルパスを返す
    """
    # `dataURL`形式からヘッダーとデータ部分を分離
    header, encoded = data_url.split(",", 1)
    audio_data = base64.b64decode(encoded)

    # 一時ファイルに保存
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
        temp_audio_file.write(audio_data)
        return temp_audio_file.name

@app.route('/transcribe', methods=['POST'])
def transcribe_audio():
    """
    POSTリクエストで送信されたdataURLを文字起こし
    """
    try:
        # JSONからdataURLを取得
        data = request.json
        data_url = data.get("dataURL")
        if not data_url:
            return jsonify({"error": "Missing 'dataURL' in request"}), 400

        # 音声データをデコードして一時ファイルパスを取得
        audio_file_path = decode_audio(data_url)

        # Whisperで文字起こし
        result = transcriber(audio_file_path)
        text = result["text"]

        # 一時ファイルを削除(必要なら実装)
        # os.remove(audio_file_path)

        return jsonify({"transcription": text})

    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(port=7860)