File size: 1,922 Bytes
5e9f459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, request, jsonify
from transformers import pipeline
import os
from werkzeug.utils import secure_filename

app = Flask(__name__)

# 設定上傳目錄與 Hugging Face 快取
UPLOAD_FOLDER = "static/uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.environ["HF_HOME"] = "./cache"

app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER

# 載入 Hugging Face 模型
emotion_analysis = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
text_to_speech = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")

# 首頁路由
@app.route("/")
def home():
    return render_template("index.html")

# 接收語音檔案並分析
@app.route("/upload_audio", methods=["POST"])
def upload_audio():
    if "file" not in request.files:
        return jsonify({"error": "No file part"}), 400

    file = request.files["file"]
    if file.filename == "":
        return jsonify({"error": "No selected file"}), 400

    # 儲存檔案
    filename = secure_filename(file.filename)
    filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
    file.save(filepath)

    # 使用語音轉文字工具(假設使用預處理工具生成文本)
    transcribed_text = "This is a placeholder for transcribed audio text."  # 替換為實際語音轉文字工具
    emotions = emotion_analysis(transcribed_text)

    # 生成語音建議
    advice_text = "Based on your tone and words, you may want to relax and open up to others."
    speech_output = text_to_speech(advice_text)
    advice_audio_path = os.path.join(app.config["UPLOAD_FOLDER"], "advice_output.wav")
    speech_output.save(advice_audio_path)

    return jsonify({
        "transcription": transcribed_text,
        "emotions": emotions,
        "advice_text": advice_text,
        "advice_audio": f"/{advice_audio_path}"
    })

if __name__ == "__main__":
    app.run(debug=True)