Spaces:
Sleeping
Sleeping
from flask import Flask, request, jsonify, render_template, 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() # クライアントから送られてきたJSONデータ | |
audio_data = data.get('audio_data') # Base64エンコードされた音声データ | |
if not audio_data: | |
return jsonify({"error": "音声データが送信されていません"}), 400 | |
# Base64デコード | |
audio_binary = base64.b64decode(audio_data) | |
# 永続化用ディレクトリ "data" がなければ作成 | |
persist_dir = "data" | |
if not os.path.exists(persist_dir): | |
os.makedirs(persist_dir) | |
# WAVファイルとして "data" フォルダに保存 | |
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: | |
return jsonify({"error": str(e)}), 500 | |
if __name__ == '__main__': | |
port = int(os.environ.get("PORT", 7860)) | |
app.run(debug=True, host="0.0.0.0", port=port) | |