File size: 781 Bytes
c595eda
 
 
 
 
 
4445929
c595eda
 
4445929
c595eda
 
 
 
4445929
c595eda
4445929
 
 
 
 
c595eda
4445929
 
c595eda
 
4445929
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
from flask import Flask, render_template, request, jsonify
from transformers import pipeline
import os

app = Flask(__name__)

# 設置 Hugging Face Cache 路徑
os.environ["HF_HOME"] = "./cache"

# 加載 Hugging Face 模型
emotion_analysis = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")

@app.route("/")
def home():
    return {"message": "Welcome to the Emotional Assistant!"}

@app.route("/analyze_emotion", methods=["POST"])
def analyze_emotion():
    user_input = request.json.get("message", "")
    if not user_input:
        return jsonify({"error": "No input provided"}), 400

    emotions = emotion_analysis(user_input)
    return jsonify({"result": emotions})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)