Spaces:
Runtime error
Runtime error
| from flask import Flask, request, jsonify, render_template, send_from_directory | |
| import base64 | |
| from pydub import AudioSegment # 変換用にpydubをインポート | |
| import os | |
| import shutil | |
| from process import AudioProcessor | |
| process=AudioProcessor() | |
| app = Flask(__name__) | |
| # トップページ(テンプレート: index.html) | |
| def index(): | |
| return render_template('index.html') | |
| # フィードバック画面(テンプレート: feedback.html) | |
| def feedback(): | |
| return render_template('feedback.html') | |
| # 会話詳細画面(テンプレート: talkDetail.html) | |
| def talk_detail(): | |
| return render_template('talkDetail.html') | |
| # 音声登録画面(テンプレート: userRegister.html) | |
| def userregister(): | |
| return render_template('userregister.html') | |
| # 音声アップロード&解析エンドポイント | |
| def upload_audio(): | |
| try: | |
| data = request.get_json() | |
| if not data or 'audio_data' not in data: | |
| return jsonify({"error": "音声データがありません"}), 400 | |
| # Base64デコードして音声バイナリを取得 | |
| audio_binary = base64.b64decode(data['audio_data']) | |
| audio_dir = "/tmp/data" | |
| os.makedirs(audio_dir, exist_ok=True) | |
| # 固定ファイル名(必要に応じて generate_filename() で一意のファイル名に変更可能) | |
| audio_path = os.path.join(audio_dir, "recorded_audio.wav") | |
| with open(audio_path, 'wb') as f: | |
| f.write(audio_binary) | |
| # 参照音声ファイルのパスを指定(sample.wav を正しい場所に配置すること) | |
| reference_audio = os.path.abspath('/tmp/data/base_audio/recorded_base_audio.wav') | |
| if not os.path.exists(reference_audio): | |
| return jsonify({"error": "参照音声ファイルが見つかりません", "details": reference_audio}), 500 | |
| # 音声解析:参照音声とアップロードされた音声との類似度をセグメント毎に計算 | |
| # threshold の値は調整可能です(例: 0.1) | |
| matched_time, unmatched_time = process.process_audio(reference_audio, audio_path, threshold=0.05) | |
| total_time = matched_time + unmatched_time | |
| rate = (matched_time / total_time) * 100 if total_time > 0 else 0 | |
| return jsonify({"rate": rate}), 200 | |
| except Exception as e: | |
| print("Error in /upload_audio:", str(e)) | |
| return jsonify({"error": "サーバーエラー", "details": str(e)}), 500 | |
| def upload_base_audio(): | |
| try: | |
| data = request.get_json() | |
| if not data or 'audio_data' not in data or 'name' not in data: | |
| return jsonify({"error": "音声データまたは名前がありません"}), 400 | |
| # Base64デコードして音声バイナリを取得 | |
| audio_binary = base64.b64decode(data['audio_data']) | |
| name = data['name'] # 名前を取得 | |
| # 保存先ディレクトリの作成 | |
| audio_dir = "/tmp/data/base_audio" | |
| os.makedirs(audio_dir, exist_ok=True) | |
| # 辞書型を作成(音声データと名前) | |
| audio_info = { | |
| "name": name, | |
| "audio_data": audio_binary # バイナリデータをそのまま格納 | |
| } | |
| # 一時ファイルに保存(実際の形式は WebM などと仮定) | |
| temp_audio_path = os.path.join(audio_dir, "temp_audio") | |
| with open(temp_audio_path, 'wb') as f: | |
| f.write(audio_binary) | |
| # pydub を使って一時ファイルを WAV に変換 | |
| # ※ここでは WebM 形式と仮定していますが、実際の形式に合わせて format の指定を変更してください | |
| try: | |
| audio = AudioSegment.from_file(temp_audio_path, format="webm") | |
| except Exception as e: | |
| # 形式が不明な場合は自動判別させる(ただし変換できない場合もあり) | |
| audio = AudioSegment.from_file(temp_audio_path) | |
| wav_audio_path = os.path.join(audio_dir, "recorded_base_audio.wav") | |
| audio.export(wav_audio_path, format="wav") | |
| # 一時ファイルを削除 | |
| os.remove(temp_audio_path) | |
| return jsonify({"state": "Registration Success!"}), 200 | |
| except Exception as e: | |
| print("Error in /upload_base_audio:", 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) |