Spaces:
Runtime error
Runtime error
Peiiiiiiiiru
commited on
Commit
•
5e9f459
1
Parent(s):
c5c4f71
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
from transformers import pipeline
|
3 |
+
import os
|
4 |
+
from werkzeug.utils import secure_filename
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# 設定上傳目錄與 Hugging Face 快取
|
9 |
+
UPLOAD_FOLDER = "static/uploads"
|
10 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
11 |
+
os.environ["HF_HOME"] = "./cache"
|
12 |
+
|
13 |
+
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
14 |
+
|
15 |
+
# 載入 Hugging Face 模型
|
16 |
+
emotion_analysis = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
17 |
+
text_to_speech = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")
|
18 |
+
|
19 |
+
# 首頁路由
|
20 |
+
@app.route("/")
|
21 |
+
def home():
|
22 |
+
return render_template("index.html")
|
23 |
+
|
24 |
+
# 接收語音檔案並分析
|
25 |
+
@app.route("/upload_audio", methods=["POST"])
|
26 |
+
def upload_audio():
|
27 |
+
if "file" not in request.files:
|
28 |
+
return jsonify({"error": "No file part"}), 400
|
29 |
+
|
30 |
+
file = request.files["file"]
|
31 |
+
if file.filename == "":
|
32 |
+
return jsonify({"error": "No selected file"}), 400
|
33 |
+
|
34 |
+
# 儲存檔案
|
35 |
+
filename = secure_filename(file.filename)
|
36 |
+
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
|
37 |
+
file.save(filepath)
|
38 |
+
|
39 |
+
# 使用語音轉文字工具(假設使用預處理工具生成文本)
|
40 |
+
transcribed_text = "This is a placeholder for transcribed audio text." # 替換為實際語音轉文字工具
|
41 |
+
emotions = emotion_analysis(transcribed_text)
|
42 |
+
|
43 |
+
# 生成語音建議
|
44 |
+
advice_text = "Based on your tone and words, you may want to relax and open up to others."
|
45 |
+
speech_output = text_to_speech(advice_text)
|
46 |
+
advice_audio_path = os.path.join(app.config["UPLOAD_FOLDER"], "advice_output.wav")
|
47 |
+
speech_output.save(advice_audio_path)
|
48 |
+
|
49 |
+
return jsonify({
|
50 |
+
"transcription": transcribed_text,
|
51 |
+
"emotions": emotions,
|
52 |
+
"advice_text": advice_text,
|
53 |
+
"advice_audio": f"/{advice_audio_path}"
|
54 |
+
})
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
app.run(debug=True)
|