# ============================== # app.py # KoBERT 감정 분석 + 앵무새 반응 + 노래 추천 # ============================== from flask import Flask, render_template, request, session import torch from kobert_model import predict_emotion # 파인튜닝한 KoBERT 모델 from spotify_api import search_spotify # Spotify 노래 추천 API app = Flask(__name__) app.secret_key = "yangtal12345" # 💡 변경 가능 # ============================== # 앵무새 이미지 & 반응 # ============================== parrot_images = { "행복": "parrot-happy.png", "슬픔": "parrot-sad.png", "분노": "parrot-angry.png", "중립": "parrot-neutral.png", "혐오": "parrot-disgust.png", "공포": "parrot-panic.png", "놀람": "parrot-surprise.png", } parrot_responses = { "행복": "와! 당신이 행복해서 저도 기분 좋아요! 🎉", "슬픔": "슬프다니, 제가 옆에서 힘이 되어줄게요... 🥺", "분노": "화났군요! 저랑 같이 심호흡 해볼까요? 😤", "중립": "괜찮아요, 평온한 하루도 필요하죠.", "혐오": "그런 감정도 있어요. 힘내요!", "공포": "겁나는 상황이군요. 제가 지켜드릴게요!", "놀람": "우와, 놀랐나요? 저도 깜짝 놀랐어요! 😲", } # ============================== # 라우트 # ============================== @app.route("/") def index(): return render_template("index.html") @app.route("/chat", methods=["GET", "POST"]) def chat(): if "chat_history" not in session: session["chat_history"] = [] if request.method == "POST": user_message = request.form.get("message", "").strip() # 감정 분석 emotion = predict_emotion(user_message) parrot_img = parrot_images.get(emotion, "parrot-neutral.png") parrot_reply = parrot_responses.get(emotion, "제가 듣고 있어요.") # 노래 추천 songs = None if "노래추천해줘" in user_message or "노래 추천" in user_message: songs = search_spotify(emotion) parrot_reply = "🎵 여기 감정에 맞는 노래들을 추천해드릴게요!" # 대화 기록 저장 session["chat_history"].append({ "user": user_message, "parrot": parrot_reply, "parrot_img": parrot_img }) session.modified = True return render_template( "chat.html", chat_history=session["chat_history"], songs=songs ) else: return render_template("chat.html", chat_history=session.get("chat_history", [])) @app.route("/reset") def reset(): session.pop("chat_history", None) return render_template("index.html") if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)