Spaces:
Runtime error
Runtime error
| from flask import Flask, request, jsonify, send_from_directory | |
| import base64 | |
| import os | |
| app = Flask(__name__) | |
| def index(): | |
| return send_from_directory(".", "index.html") | |
| def upload_audio(): | |
| try: | |
| data = request.get_json() | |
| if not data: | |
| return jsonify({"error": "JSONが送信されていません"}), 400 | |
| audio_data = data.get('audio_data') | |
| if not audio_data: | |
| return jsonify({"error": "音声データが送信されていません"}), 400 | |
| # Base64デコード | |
| try: | |
| audio_binary = base64.b64decode(audio_data) | |
| except Exception as decode_err: | |
| return jsonify({"error": "Base64デコードに失敗しました", "details": str(decode_err)}), 400 | |
| # 書き込み用ディレクトリとして /tmp/data を使用(/tmp は書き込み可能) | |
| persist_dir = "/tmp/data" | |
| os.makedirs(persist_dir, exist_ok=True) | |
| filepath = os.path.join(persist_dir, "recorded_audio.wav") | |
| with open(filepath, 'wb') as f: | |
| f.write(audio_binary) | |
| return jsonify({"message": "音声が正常に保存されました", "filepath": filepath}), 200 | |
| except Exception as e: | |
| app.logger.error("エラー: %s", str(e)) | |
| return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500 | |
| if __name__ == '__main__': | |
| port = int(os.environ.get("PORT", 7860)) | |
| app.run(debug=True, host="0.0.0.0", port=port) |