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)