Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,53 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
6 |
transcriber = pipeline("automatic-speech-recognition", model=model_name)
|
7 |
|
8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
"""
|
10 |
-
|
11 |
"""
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|