File size: 3,655 Bytes
cb5ad90
29f9ef3
db2374b
29f9ef3
397a06b
cb5ad90
29f9ef3
cb5ad90
29f9ef3
 
397a06b
29f9ef3
cb5ad90
 
 
1da3080
397a06b
f3e5312
a2ae10d
093dde1
a2ae10d
48f9750
397a06b
f3e5312
6ffdbaf
093dde1
86d99b4
48f9750
 
 
bc6ed4d
2a860fb
397a06b
29f9ef3
 
 
 
397a06b
 
 
a1b2149
 
 
 
 
397a06b
 
db2374b
397a06b
 
 
 
 
db2374b
397a06b
 
 
 
29f9ef3
397a06b
 
db2374b
 
 
 
48f9750
 
db2374b
a1b2149
 
 
 
 
 
db2374b
 
 
df19981
29f9ef3
 
48f9750
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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)
@app.route('/')
@app.route('/index', methods=['GET', 'POST'])
def index():
    return render_template('index.html')

# フィードバック画面(テンプレート: feedback.html)
@app.route('/feedback', methods=['GET', 'POST'])
def feedback():
    return render_template('feedback.html')


# 会話詳細画面(テンプレート: talkDetail.html)
@app.route('/talk_detail', methods=['GET', 'POST'])
def talk_detail():
    return render_template('talkDetail.html')

# 音声登録画面(テンプレート: userRegister.html)
@app.route('/userregister', methods=['GET', 'POST'])
def userregister():
    return render_template('userRegister.html')

# 音声アップロード&解析エンドポイント
@app.route('/upload_audio', methods=['POST'])
def upload_audio():
    try:
        data = request.get_json()
        if not data or 'audio_data' not in data:
            return jsonify({"error": "音声データがありません"}), 400
        
        audio_path=process.save_audio_from_base64(
            base64_audio=data['audio_data'],    # 音声データ
            output_dir= "/tmp/data",    #保存先
            output_filename="recorded_audio.wav"    # 固定ファイル名(必要に応じて generate_filename() で一意のファイル名に変更可能)
        )
        
        # 参照音声ファイルのパスを指定(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
@app.route('/upload_base_audio', methods=['POST'])
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
        
        audio_path=process.save_audio_from_base64(
            base64_audio=data['audio_data'],    # 音声データ
            output_dir= "/tmp/data",    #保存先
            output_filename="recorded_audio.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)