Spaces:
Runtime error
Runtime error
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) |