File size: 2,928 Bytes
b552755
 
 
 
8b43096
b552755
8b43096
b552755
 
5883a42
 
b552755
8b43096
 
b552755
 
 
 
 
8b43096
 
 
5883a42
 
8b43096
5883a42
8b43096
 
5883a42
8b43096
5883a42
8b43096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# src/main.py
# ๊ฐ์ • ๋ถ„์„ ๋ฉ”์ธ 
# app.py๋Š” ์ด์ œ ์‚ฌ์šฉ์•ˆํ•จ 

from flask import Blueprint, render_template, session, redirect, url_for, jsonify, request
from . import db # ์ด์ œ __init__์—์„œ๋Š” db๋งŒ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
from .models import Diary, User
from .emotion_engine import load_emotion_classifier, predict_emotion 
from .recommender import Recommender
import random


bp = Blueprint('main', __name__)

print("AI ์—”์ง„ ๋ฐ ์ถ”์ฒœ๊ธฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค...")
emotion_classifier = load_emotion_classifier()
recommender = Recommender()
print("AI ์—”์ง„ ๋ฐ ์ถ”์ฒœ๊ธฐ ๋กœ๋“œ ์™„๋ฃŒ.")

emotion_emoji_map = {
    '๊ธฐ์จ': '๐Ÿ˜„', 'ํ–‰๋ณต': '๐Ÿ˜Š', '์‚ฌ๋ž‘': 'โค๏ธ', '๋ถˆ์•ˆ': '๐Ÿ˜Ÿ', '์Šฌํ””': '๐Ÿ˜ข', '์ƒ์ฒ˜': '๐Ÿ’”',
    '๋ถ„๋…ธ': '๐Ÿ˜ ', 'ํ˜์˜ค': '๐Ÿคข', '์งœ์ฆ': '๐Ÿ˜ค', '๋†€๋žŒ': '๐Ÿ˜ฎ', '์ค‘๋ฆฝ': '๐Ÿ˜',
}

@bp.route("/")
def home():
    # "๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด" ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋ณด๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    if 'user_id' not in session:
        return redirect(url_for('auth.login'))
        
    return render_template("emotion_homepage.html", username=session.get('username'))

@bp.route("/api/recommend", methods=["POST"])
def api_recommend():
    if 'user_id' not in session:
        return jsonify({"error": "๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."}), 401

    user_diary = request.json.get("diary")
    if not user_diary:
        return jsonify({"error": "์ผ๊ธฐ ๋‚ด์šฉ์ด ์—†์Šต๋‹ˆ๋‹ค."}), 400

    predicted_emotion = predict_emotion(emotion_classifier, user_diary)

    try:
        user_id = session['user_id']
        new_diary_entry = Diary(content=user_diary, emotion=predicted_emotion, user_id=user_id)
        db.session.add(new_diary_entry)
        db.session.commit()
    except Exception as e:
        print(f"DB ์ €์žฅ ์˜ค๋ฅ˜: {e}")
        db.session.rollback()
    
    accept_recs = recommender.recommend(predicted_emotion, "์ˆ˜์šฉ")
    change_recs = recommender.recommend(predicted_emotion, "์ „ํ™˜")
    
    accept_choice = random.choice(accept_recs) if accept_recs else "์ถ”์ฒœ ์—†์Œ"
    change_choice = random.choice(change_recs) if change_recs else "์ถ”์ฒœ ์—†์Œ"

    recommendation_text = (
        f"<b>[ ์ด ๊ฐ์ •์„ ๋” ๊นŠ์ด ๋А๋ผ๊ณ  ์‹ถ๋‹ค๋ฉด... (์ˆ˜์šฉ) ]</b><br>"
        f"โ€ข {accept_choice}<br><br>"
        f"<b>[ ์ด ๊ฐ์ •์—์„œ ๋ฒ—์–ด๋‚˜๊ณ  ์‹ถ๋‹ค๋ฉด... (์ „ํ™˜) ]</b><br>"
        f"โ€ข {change_choice}"
    )

    response_data = {
        "emotion": predicted_emotion,
        "emoji": emotion_emoji_map.get(predicted_emotion, '๐Ÿค”'),
        "recommendation": recommendation_text
    }
    return jsonify(response_data)

@bp.route('/my_diary')
def my_diary():
    if 'user_id' not in session:
        return redirect(url_for('auth.login'))
    
    user_id = session['user_id']
    user_diaries = Diary.query.filter_by(user_id=user_id).order_by(Diary.created_at.desc()).all()
    
    return render_template('my_diary.html', diaries=user_diaries)