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 | |
| from transcription import TranscriptionMaker | |
| process=AudioProcessor() | |
| transcription = TranscriptionMaker() | |
| app = Flask(__name__) | |
| users = [] | |
| segments_dir | |
| # トップページ(テンプレート: index.html) | |
| def index(): | |
| return render_template('index.html', users = users) | |
| # フィードバック画面(テンプレート: 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') | |
| #人数確認 | |
| # 基本的にGETで取得する想定なので、GETのみに変更 | |
| def confirm(): | |
| return jsonify({'members': users}), 200 | |
| # 書き起こし作成エンドポイント | |
| def transcription(): | |
| global segments_dir | |
| text = transcription.create_transcription(segments_dir) | |
| return jsonify({'transcription': text}),200 | |
| # 音声アップロード&解析エンドポイント | |
| def upload_audio(): | |
| global segments_dir | |
| try: | |
| data = request.get_json() | |
| # name か users のいずれかが必須。どちらも無い場合はエラー | |
| if not data or 'audio_data' not in data or ('name' not in data and 'users' not in data): | |
| return jsonify({"error": "音声データまたは名前がありません"}), 400 | |
| # Base64デコードして音声バイナリを取得 | |
| audio_binary = base64.b64decode(data['audio_data']) | |
| upload_name = 'tmp' | |
| audio_dir = "/tmp/data" | |
| os.makedirs(audio_dir, exist_ok=True) | |
| audio_path = os.path.join(audio_dir, f"{upload_name}.wav") | |
| with open(audio_path, 'wb') as f: | |
| f.write(audio_binary) | |
| print(users) | |
| # 各ユーザーの参照音声ファイルのパスをリストに格納 | |
| reference_paths = [] | |
| base_audio_dir = "/tmp/data/base_audio" | |
| for user in users: | |
| ref_path = os.path.abspath(os.path.join(base_audio_dir, f"{user}.wav")) | |
| if not os.path.exists(ref_path): | |
| return jsonify({"error": "参照音声ファイルが見つかりません", "details": ref_path}), 500 | |
| reference_paths.append(ref_path) | |
| # 複数人の場合は参照パスのリストを、1人の場合は単一のパスを渡す | |
| if len(users) > 1: | |
| print("複数人の場合の処理") | |
| matched_time, unmatched_time,segments_dir = process.process_multi_audio(reference_paths, audio_path, threshold=0.05) | |
| else: | |
| matched_time, unmatched_time, segments_dir = process.process_audio(reference_paths[0], 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 reset(): | |
| global users | |
| users=[] | |
| return 200 | |
| def upload_base_audio(): | |
| global users#グローバル変数を編集できるようにする | |
| try: | |
| data = request.get_json() | |
| if not data or 'audio_data' not in data or 'name' not in data: | |
| return jsonify({"error": "音声データまたは名前がありません"}), 400 | |
| name = data['name'] # 名前を取得 | |
| print(name) | |
| users.append(name) | |
| users=list(set(users))#重複排除 | |
| print(users) | |
| audio_path=process.save_audio_from_base64( | |
| base64_audio=data['audio_data'], # 音声データ | |
| output_dir= "/tmp/data/base_audio", #保存先 | |
| output_filename=f"{name}.wav" # 固定ファイル名(必要に応じて generate_filename() で一意のファイル名に変更可能) | |
| ) | |
| return jsonify({"state": "Registration Success!", "path": audio_path}), 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) |