rein0421 commited on
Commit
7475ce4
·
verified ·
1 Parent(s): f692912

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, request, jsonify, render_template, send_from_directory
2
  import base64
3
  import os
4
 
@@ -11,28 +11,35 @@ def index():
11
  @app.route('/upload_audio', methods=['POST'])
12
  def upload_audio():
13
  try:
14
- data = request.get_json() # クライアントから送られてきたJSONデータ
15
- audio_data = data.get('audio_data') # Base64エンコードされた音声データ
 
16
 
 
17
  if not audio_data:
18
  return jsonify({"error": "音声データが送信されていません"}), 400
19
-
20
- # Base64デコード
21
- audio_binary = base64.b64decode(audio_data)
22
-
23
- # 永続化用ディレクトリ "data" がなければ作成
 
 
 
24
  persist_dir = "data"
25
- if not os.path.exists(persist_dir):
26
- os.makedirs(persist_dir)
27
-
28
- # WAVファイルとして "data" フォルダに保存
29
  filepath = os.path.join(persist_dir, "recorded_audio.wav")
30
  with open(filepath, 'wb') as f:
31
  f.write(audio_binary)
32
-
33
  return jsonify({"message": "音声が正常に保存されました", "filepath": filepath}), 200
 
34
  except Exception as e:
35
- return jsonify({"error": str(e)}), 500
 
 
36
 
37
  if __name__ == '__main__':
38
  port = int(os.environ.get("PORT", 7860))
 
1
+ from flask import Flask, request, jsonify, send_from_directory
2
  import base64
3
  import os
4
 
 
11
  @app.route('/upload_audio', methods=['POST'])
12
  def upload_audio():
13
  try:
14
+ data = request.get_json()
15
+ if not data:
16
+ return jsonify({"error": "JSONが送信されていません"}), 400
17
 
18
+ audio_data = data.get('audio_data')
19
  if not audio_data:
20
  return jsonify({"error": "音声データが送信されていません"}), 400
21
+
22
+ # Base64デコード(例外発生時はエラー内容を返す)
23
+ try:
24
+ audio_binary = base64.b64decode(audio_data)
25
+ except Exception as decode_err:
26
+ return jsonify({"error": "Base64デコードに失敗しました", "details": str(decode_err)}), 400
27
+
28
+ # 書き込み用ディレクトリ "data" の作成(既にあれば何もしない)
29
  persist_dir = "data"
30
+ os.makedirs(persist_dir, exist_ok=True)
31
+
32
+ # ファイルの保存
 
33
  filepath = os.path.join(persist_dir, "recorded_audio.wav")
34
  with open(filepath, 'wb') as f:
35
  f.write(audio_binary)
36
+
37
  return jsonify({"message": "音声が正常に保存されました", "filepath": filepath}), 200
38
+
39
  except Exception as e:
40
+ # サーバー内部エラーの場合、詳細をログに出力
41
+ app.logger.error("エラー: %s", str(e))
42
+ return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500
43
 
44
  if __name__ == '__main__':
45
  port = int(os.environ.get("PORT", 7860))