Spaces:
Runtime error
Runtime error
kootaeng2
commited on
Commit
ยท
fd1e3ee
1
Parent(s):
c98f885
Feat: Implement user login and registration system
Browse files- src/app.py +104 -20
- src/database.db +0 -0
- src/templates/login.html +20 -0
- src/templates/signup.html +20 -0
src/app.py
CHANGED
|
@@ -1,24 +1,56 @@
|
|
| 1 |
-
# app.py (
|
| 2 |
|
| 3 |
-
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from .emotion_engine import load_emotion_classifier, predict_emotion
|
| 6 |
from .recommender import Recommender
|
| 7 |
-
import random
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
print("AI ์ฑ๋ด ์๋ฒ๋ฅผ ์ค๋น ์ค์
๋๋ค...")
|
| 18 |
-
# ์๋ฒ๊ฐ ์์๋ ๋ AI ์์ง๊ณผ ์ถ์ฒ๊ธฐ๋ฅผ ๊ฐ๊ฐ ํ ๋ฒ์ฉ๋ง ๋ก๋ํฉ๋๋ค.
|
| 19 |
emotion_classifier = load_emotion_classifier()
|
| 20 |
recommender = Recommender()
|
| 21 |
-
# ์นํ์ด์ง์ ๊ฐ์ ๋ณ ์ด๋ชจ์ง๋ฅผ ๋ณด๋ด์ฃผ๊ธฐ ์ํ ๋์
๋๋ฆฌ์
๋๋ค.
|
| 22 |
emotion_emoji_map = {
|
| 23 |
'๊ธฐ์จ': '๐', 'ํ๋ณต': '๐', '์ฌ๋': 'โค๏ธ',
|
| 24 |
'๋ถ์': '๐', '์ฌํ': '๐ข', '์์ฒ': '๐',
|
|
@@ -28,32 +60,80 @@ emotion_emoji_map = {
|
|
| 28 |
}
|
| 29 |
print("โ
AI ์ฑ๋ด ์๋ฒ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ค๋น๋์์ต๋๋ค.")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@app.route("/")
|
| 32 |
def home():
|
| 33 |
-
"""
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
@app.route("/api/recommend", methods=["POST"])
|
| 38 |
def api_recommend():
|
| 39 |
-
"""
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
| 41 |
user_diary = request.json.get("diary")
|
| 42 |
if not user_diary:
|
| 43 |
return jsonify({"error": "์ผ๊ธฐ ๋ด์ฉ์ด ์์ต๋๋ค."}), 400
|
| 44 |
|
| 45 |
-
# 2. emotion_engine์ ์ฌ์ฉํด ๊ฐ์ ์ ์์ธกํฉ๋๋ค.
|
| 46 |
predicted_emotion = predict_emotion(emotion_classifier, user_diary)
|
| 47 |
|
| 48 |
-
# 3. recommender๋ฅผ ์ฌ์ฉํด '์์ฉ'๊ณผ '์ ํ' ์ถ์ฒ์ ๋ชจ๋ ๋ฐ์ต๋๋ค.
|
| 49 |
accept_recs = recommender.recommend(predicted_emotion, "์์ฉ")
|
| 50 |
change_recs = recommender.recommend(predicted_emotion, "์ ํ")
|
| 51 |
|
| 52 |
-
# 4. ๊ฐ ์ถ์ฒ ๋ชฉ๋ก์์ ๋๋ค์ผ๋ก ํ๋์ฉ ์ ํํฉ๋๋ค. (๊ฒฐ๊ณผ๊ฐ ์์ ๊ฒฝ์ฐ๋ฅผ ๋๋น)
|
| 53 |
accept_choice = random.choice(accept_recs) if accept_recs else "์ถ์ฒ ์์"
|
| 54 |
change_choice = random.choice(change_recs) if change_recs else "์ถ์ฒ ์์"
|
| 55 |
|
| 56 |
-
# 5. ์นํ์ด์ง์ ๋ณด์ฌ์ค ์ต์ข
ํ
์คํธ๋ฅผ ์กฐํฉํฉ๋๋ค.
|
| 57 |
recommendation_text = (
|
| 58 |
f"<b>[ ์ด ๊ฐ์ ์ ๋ ๊น์ด ๋๋ผ๊ณ ์ถ๋ค๋ฉด... (์์ฉ) ]</b><br>"
|
| 59 |
f"โข {accept_choice}<br><br>"
|
|
@@ -61,7 +141,6 @@ def api_recommend():
|
|
| 61 |
f"โข {change_choice}"
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# 6. ์ต์ข
๊ฒฐ๊ณผ๋ฅผ JSON ํํ๋ก ์นํ์ด์ง์ ๋๋ ค์ค๋๋ค.
|
| 65 |
response_data = {
|
| 66 |
"emotion": predicted_emotion,
|
| 67 |
"emoji": emotion_emoji_map.get(predicted_emotion, '๐ค'),
|
|
@@ -69,5 +148,10 @@ def api_recommend():
|
|
| 69 |
}
|
| 70 |
return jsonify(response_data)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 73 |
app.run(debug=True)
|
|
|
|
| 1 |
+
# src/app.py (์ต์ข
ํตํฉ ๋ฒ์ )
|
| 2 |
|
| 3 |
+
# ---------------------------------
|
| 4 |
+
# 1. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ๋ชจ๋ ์ํฌํธ
|
| 5 |
+
# ---------------------------------
|
| 6 |
+
import os
|
| 7 |
+
import random
|
| 8 |
+
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
|
| 9 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 10 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 11 |
+
|
| 12 |
+
# 'ํ์ฌ ํด๋์ ์๋' ๋ชจ๋๋ค์ ์๋ ๊ฒฝ๋ก๋ก ์ํฌํธ
|
| 13 |
from .emotion_engine import load_emotion_classifier, predict_emotion
|
| 14 |
from .recommender import Recommender
|
|
|
|
| 15 |
|
| 16 |
+
# ---------------------------------
|
| 17 |
+
# 2. Flask ์ฑ ์ด๊ธฐํ ๋ฐ ๊ธฐ๋ณธ ์ค์
|
| 18 |
+
# ---------------------------------
|
| 19 |
app = Flask(__name__)
|
| 20 |
|
| 21 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์ผ ๊ฒฝ๋ก ์ค์
|
| 22 |
+
basedir = os.path.abspath(os.path.dirname(__file__))
|
| 23 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
|
| 24 |
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 25 |
+
# ์ธ์
๋ฐ์ดํฐ๋ฅผ ์ํธํํ๊ธฐ ์ํ ์ํฌ๋ฆฟ ํค (โ ๏ธ ์ค์ ๋ฐฐํฌ ์์๋ ๋ ๋ณต์กํ ๊ฐ์ผ๋ก ๋ณ๊ฒฝํ์ธ์!)
|
| 26 |
+
app.config['SECRET_KEY'] = 'dev-secret-key-for-flask-session'
|
| 27 |
|
| 28 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ฒด ์์ฑ
|
| 29 |
+
db = SQLAlchemy(app)
|
| 30 |
+
|
| 31 |
+
# ---------------------------------
|
| 32 |
+
# 3. ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ชจ๋ธ ์ ์
|
| 33 |
+
# ---------------------------------
|
| 34 |
+
class User(db.Model):
|
| 35 |
+
"""์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ์ ์ํ ๋ชจ๋ธ"""
|
| 36 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 37 |
+
username = db.Column(db.String(80), unique=True, nullable=False)
|
| 38 |
+
password_hash = db.Column(db.String(128), nullable=False)
|
| 39 |
+
|
| 40 |
+
def set_password(self, password):
|
| 41 |
+
"""๋น๋ฐ๋ฒํธ๋ฅผ ํด์ฑํ์ฌ ์ ์ฅ"""
|
| 42 |
+
self.password_hash = generate_password_hash(password)
|
| 43 |
|
| 44 |
+
def check_password(self, password):
|
| 45 |
+
"""ํด์๋ ๋น๋ฐ๋ฒํธ์ ์
๋ ฅ๋ ๋น๋ฐ๋ฒํธ๋ฅผ ๋น๊ต"""
|
| 46 |
+
return check_password_hash(self.password_hash, password)
|
| 47 |
+
|
| 48 |
+
# ---------------------------------
|
| 49 |
+
# 4. AI ์์ง ๋ฐ ์ถ์ฒ๊ธฐ ์ด๊ธฐ ๋ก๋ฉ
|
| 50 |
+
# ---------------------------------
|
| 51 |
print("AI ์ฑ๋ด ์๋ฒ๋ฅผ ์ค๋น ์ค์
๋๋ค...")
|
|
|
|
| 52 |
emotion_classifier = load_emotion_classifier()
|
| 53 |
recommender = Recommender()
|
|
|
|
| 54 |
emotion_emoji_map = {
|
| 55 |
'๊ธฐ์จ': '๐', 'ํ๋ณต': '๐', '์ฌ๋': 'โค๏ธ',
|
| 56 |
'๋ถ์': '๐', '์ฌํ': '๐ข', '์์ฒ': '๐',
|
|
|
|
| 60 |
}
|
| 61 |
print("โ
AI ์ฑ๋ด ์๋ฒ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ค๋น๋์์ต๋๋ค.")
|
| 62 |
|
| 63 |
+
# ---------------------------------
|
| 64 |
+
# 5. ๋ผ์ฐํธ(Routes) ์ ์
|
| 65 |
+
# ---------------------------------
|
| 66 |
+
|
| 67 |
+
# --- ์ฌ์ฉ์ ์ธ์ฆ ๊ด๋ จ ๋ผ์ฐํธ ---
|
| 68 |
+
@app.route('/signup', methods=['GET', 'POST'])
|
| 69 |
+
def signup():
|
| 70 |
+
if request.method == 'POST':
|
| 71 |
+
username = request.form['username']
|
| 72 |
+
password = request.form['password']
|
| 73 |
+
|
| 74 |
+
if User.query.filter_by(username=username).first():
|
| 75 |
+
return "์ด๋ฏธ ์กด์ฌํ๋ ์ฌ์ฉ์ ์ด๋ฆ์
๋๋ค."
|
| 76 |
+
|
| 77 |
+
new_user = User(username=username)
|
| 78 |
+
new_user.set_password(password)
|
| 79 |
+
db.session.add(new_user)
|
| 80 |
+
db.session.commit()
|
| 81 |
+
|
| 82 |
+
return redirect(url_for('login'))
|
| 83 |
+
|
| 84 |
+
return render_template('signup.html')
|
| 85 |
+
|
| 86 |
+
@app.route('/login', methods=['GET', 'POST'])
|
| 87 |
+
def login():
|
| 88 |
+
if request.method == 'POST':
|
| 89 |
+
username = request.form['username']
|
| 90 |
+
password = request.form['password']
|
| 91 |
+
user = User.query.filter_by(username=username).first()
|
| 92 |
+
|
| 93 |
+
if user and user.check_password(password):
|
| 94 |
+
session['user_id'] = user.id
|
| 95 |
+
session['username'] = user.username
|
| 96 |
+
return redirect(url_for('home'))
|
| 97 |
+
else:
|
| 98 |
+
return "๋ก๊ทธ์ธ ์ ๋ณด๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค."
|
| 99 |
+
|
| 100 |
+
return render_template('login.html')
|
| 101 |
+
|
| 102 |
+
@app.route('/logout')
|
| 103 |
+
def logout():
|
| 104 |
+
session.pop('user_id', None)
|
| 105 |
+
session.pop('username', None)
|
| 106 |
+
return redirect(url_for('login'))
|
| 107 |
+
|
| 108 |
+
# --- ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
๋ผ์ฐํธ ---
|
| 109 |
@app.route("/")
|
| 110 |
def home():
|
| 111 |
+
"""๋ฉ์ธ ํ์ด์ง. ๋ก๊ทธ์ธํ์ง ์์ ๊ฒฝ์ฐ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ด๋."""
|
| 112 |
+
if 'user_id' not in session:
|
| 113 |
+
return redirect(url_for('login'))
|
| 114 |
+
|
| 115 |
+
# ๋ก๊ทธ์ธํ ์ฌ์ฉ์์ ์ด๋ฆ์ ํ
ํ๋ฆฟ์ผ๋ก ์ ๋ฌ
|
| 116 |
+
return render_template("emotion_homepage.html", username=session.get('username'))
|
| 117 |
|
| 118 |
@app.route("/api/recommend", methods=["POST"])
|
| 119 |
def api_recommend():
|
| 120 |
+
"""์ผ๊ธฐ ๋ถ์ ๋ฐ ์ฝํ
์ธ ์ถ์ฒ API."""
|
| 121 |
+
# API ์์ฒญ๋ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๋ง ๊ฐ๋ฅํ๋๋ก ์ฒดํฌ
|
| 122 |
+
if 'user_id' not in session:
|
| 123 |
+
return jsonify({"error": "๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค."}), 401
|
| 124 |
+
|
| 125 |
user_diary = request.json.get("diary")
|
| 126 |
if not user_diary:
|
| 127 |
return jsonify({"error": "์ผ๊ธฐ ๋ด์ฉ์ด ์์ต๋๋ค."}), 400
|
| 128 |
|
|
|
|
| 129 |
predicted_emotion = predict_emotion(emotion_classifier, user_diary)
|
| 130 |
|
|
|
|
| 131 |
accept_recs = recommender.recommend(predicted_emotion, "์์ฉ")
|
| 132 |
change_recs = recommender.recommend(predicted_emotion, "์ ํ")
|
| 133 |
|
|
|
|
| 134 |
accept_choice = random.choice(accept_recs) if accept_recs else "์ถ์ฒ ์์"
|
| 135 |
change_choice = random.choice(change_recs) if change_recs else "์ถ์ฒ ์์"
|
| 136 |
|
|
|
|
| 137 |
recommendation_text = (
|
| 138 |
f"<b>[ ์ด ๊ฐ์ ์ ๋ ๊น์ด ๋๋ผ๊ณ ์ถ๋ค๋ฉด... (์์ฉ) ]</b><br>"
|
| 139 |
f"โข {accept_choice}<br><br>"
|
|
|
|
| 141 |
f"โข {change_choice}"
|
| 142 |
)
|
| 143 |
|
|
|
|
| 144 |
response_data = {
|
| 145 |
"emotion": predicted_emotion,
|
| 146 |
"emoji": emotion_emoji_map.get(predicted_emotion, '๐ค'),
|
|
|
|
| 148 |
}
|
| 149 |
return jsonify(response_data)
|
| 150 |
|
| 151 |
+
# ---------------------------------
|
| 152 |
+
# 6. ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
| 153 |
+
# ---------------------------------
|
| 154 |
if __name__ == "__main__":
|
| 155 |
+
with app.app_context():
|
| 156 |
+
db.create_all() # (์ ํ์ฌํญ) ์ฑ ์คํ ์ DB๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ์์ฑ.
|
| 157 |
app.run(debug=True)
|
src/database.db
ADDED
|
Binary file (12.3 kB). View file
|
|
|
src/templates/login.html
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="ko">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>๋ก๊ทธ์ธ</title>
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>๋ก๊ทธ์ธ</h1>
|
| 9 |
+
<form method="post">
|
| 10 |
+
<label for="username">์ฌ์ฉ์ ์ด๋ฆ:</label>
|
| 11 |
+
<input type="text" id="username" name="username" required>
|
| 12 |
+
<br><br>
|
| 13 |
+
<label for="password">๋น๋ฐ๋ฒํธ:</label>
|
| 14 |
+
<input type="password" id="password" name="password" required>
|
| 15 |
+
<br><br>
|
| 16 |
+
<button type="submit">๋ก๊ทธ์ธ</button>
|
| 17 |
+
</form>
|
| 18 |
+
<p>๊ณ์ ์ด ์์ผ์ ๊ฐ์? <a href="/signup">ํ์๊ฐ์
</a></p>
|
| 19 |
+
</body>
|
| 20 |
+
</html>
|
src/templates/signup.html
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="ko">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>ํ์๊ฐ์
</title>
|
| 6 |
+
</head>
|
| 7 |
+
<body>
|
| 8 |
+
<h1>ํ์๊ฐ์
</h1>
|
| 9 |
+
<form method="post">
|
| 10 |
+
<label for="username">์ฌ์ฉ์ ์ด๋ฆ:</label>
|
| 11 |
+
<input type="text" id="username" name="username" required>
|
| 12 |
+
<br><br>
|
| 13 |
+
<label for="password">๋น๋ฐ๋ฒํธ:</label>
|
| 14 |
+
<input type="password" id="password" name="password" required>
|
| 15 |
+
<br><br>
|
| 16 |
+
<button type="submit">ํ์๊ฐ์
</button>
|
| 17 |
+
</form>
|
| 18 |
+
<p>์ด๋ฏธ ๊ณ์ ์ด ์์ผ์ ๊ฐ์? <a href="/login">๋ก๊ทธ์ธ</a></p>
|
| 19 |
+
</body>
|
| 20 |
+
</html>
|