Peiiiiiiiiru commited on
Commit
4445929
·
verified ·
1 Parent(s): 2d533c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -41
app.py CHANGED
@@ -1,57 +1,27 @@
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)
 
1
  from flask import Flask, render_template, request, jsonify
2
  from transformers import pipeline
3
  import os
 
4
 
5
  app = Flask(__name__)
6
 
7
+ # 設置 Hugging Face Cache 路徑
 
 
8
  os.environ["HF_HOME"] = "./cache"
9
 
10
+ # 加載 Hugging Face 模型
 
 
11
  emotion_analysis = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
 
12
 
 
13
  @app.route("/")
14
  def home():
15
+ return {"message": "Welcome to the Emotional Assistant!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ @app.route("/analyze_emotion", methods=["POST"])
18
+ def analyze_emotion():
19
+ user_input = request.json.get("message", "")
20
+ if not user_input:
21
+ return jsonify({"error": "No input provided"}), 400
22
 
23
+ emotions = emotion_analysis(user_input)
24
+ return jsonify({"result": emotions})
 
 
 
 
25
 
26
  if __name__ == "__main__":
27
+ app.run(host="0.0.0.0", port=7860)