Spaces:
Runtime error
Runtime error
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") | |
# 首頁路由 | |
def home(): | |
return render_template("index.html") | |
# 接收語音檔案並分析 | |
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) | |