Spaces:
Runtime error
Runtime error
kootaeng2
commited on
Commit
ยท
5883a42
1
Parent(s):
c1b9543
Refactor:Loadmodel from Hugging Face Hub and remove local modelfiles
Browse files- run.py +11 -0
- src/{app.py โ __app__backup.py} +18 -1
- src/__init__.py +23 -0
- src/auth.py +72 -0
- src/main.py +28 -0
- src/models.py +38 -0
run.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/run.py
|
| 2 |
+
# run.py - ์ ํ๋ฆฌ์ผ์ด์
์คํ ํ์ผ
|
| 3 |
+
|
| 4 |
+
from src import create_app
|
| 5 |
+
|
| 6 |
+
app = create_app()
|
| 7 |
+
|
| 8 |
+
if __name__ == '__main__':
|
| 9 |
+
app.run(debug=True)
|
| 10 |
+
|
| 11 |
+
|
src/{app.py โ __app__backup.py}
RENAMED
|
@@ -1,8 +1,9 @@
|
|
| 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
|
|
@@ -127,6 +128,22 @@ def api_recommend():
|
|
| 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, "์ ํ")
|
|
|
|
| 1 |
+
# src/app.py ๋ฐฑ์
์ฉ
|
| 2 |
|
| 3 |
# ---------------------------------
|
| 4 |
# 1. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ๋ชจ๋ ์ํฌํธ
|
| 5 |
# ---------------------------------
|
| 6 |
+
from .models import User,Diary # db๊ด๋ จ ๋ชจ๋ธ
|
| 7 |
import os
|
| 8 |
import random
|
| 9 |
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
|
|
|
|
| 128 |
return jsonify({"error": "์ผ๊ธฐ ๋ด์ฉ์ด ์์ต๋๋ค."}), 400
|
| 129 |
|
| 130 |
predicted_emotion = predict_emotion(emotion_classifier, user_diary)
|
| 131 |
+
|
| 132 |
+
# --- ์ฌ๊ธฐ์ DB ์ ์ฅ ๋ก์ง ์ถ๊ฐ ---
|
| 133 |
+
try:
|
| 134 |
+
user_id = session['user_id']
|
| 135 |
+
# ์๋ก์ด Diary ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด DB์ ์ ์ฅํฉ๋๋ค.
|
| 136 |
+
new_diary_entry = Diary(content=user_diary, emotion=predicted_emotion, user_id=user_id)
|
| 137 |
+
db.session.add(new_diary_entry)
|
| 138 |
+
db.session.commit()
|
| 139 |
+
except Exception as e:
|
| 140 |
+
# DB ์ ์ฅ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์๋ฒ ๋ก๊ทธ์ ๊ธฐ๋กํฉ๋๋ค.
|
| 141 |
+
print(f"DB ์ ์ฅ ์ค๋ฅ: {e}")
|
| 142 |
+
db.session.rollback() # ์ค๋ฅ ๋ฐ์ ์ ์์
์ ๋๋๋ฆฝ๋๋ค.
|
| 143 |
+
|
| 144 |
+
accept_recs = recommender.recommend(predicted_emotion, "์์ฉ")
|
| 145 |
+
|
| 146 |
+
return jsonify(response_data)
|
| 147 |
|
| 148 |
accept_recs = recommender.recommend(predicted_emotion, "์์ฉ")
|
| 149 |
change_recs = recommender.recommend(predicted_emotion, "์ ํ")
|
src/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/__init__.py
|
| 2 |
+
# db ์ด๊ธฐํ, ๋ธ๋ฃจํ๋ฆฐํธ(์ฑ ๋ถ๋ฆฌ ํ์ฌ๋ main, auth๋ถ๋ฆฌ) ๋ฑ๋ก
|
| 3 |
+
|
| 4 |
+
from flask import Flask
|
| 5 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# db ์์ฑ
|
| 9 |
+
db=SQLAlchemy()
|
| 10 |
+
def create_app():
|
| 11 |
+
app=Flask(__name__)
|
| 12 |
+
basedir = os.path.abspath(os.path.dirname(__file__))
|
| 13 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
|
| 14 |
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 15 |
+
app.config['SECRET_KEY'] = 'dev-secret-key-for-flask-session'
|
| 16 |
+
db.init_app(app)
|
| 17 |
+
|
| 18 |
+
from . import main, auth
|
| 19 |
+
app. register_blueprint(main.bp)
|
| 20 |
+
app.register_blueprint(auth.bp)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
return app
|
src/auth.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/ auth.py
|
| 2 |
+
# ๊ธฐ์กด app.py์์ auth์ ๊ด๋ จํด์ ๋ถ๋ฆฌ
|
| 3 |
+
# ๋ก๊ทธ์ธ์ด๋ ํ์๊ฐ์
์ธ์ฆ ๊ด๋ จํ ์คํฌ๋ฆฝํธ
|
| 4 |
+
|
| 5 |
+
from flask import Blueprint, render_template, request, redirect, url_for, session
|
| 6 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 7 |
+
from . import db
|
| 8 |
+
from .models import User
|
| 9 |
+
|
| 10 |
+
bp = Blueprint('auth', __name__, url_prefix='/auth')
|
| 11 |
+
|
| 12 |
+
# ํ์๊ฐ์
ํํธ
|
| 13 |
+
@bp.route('/signup', methods=['GET', 'POST'])
|
| 14 |
+
def signup():
|
| 15 |
+
if request.method == 'POST':
|
| 16 |
+
username = request.form['username']
|
| 17 |
+
password = request.form['password']
|
| 18 |
+
|
| 19 |
+
# ์ด๋ฏธ ์กด์ฌํ๋ ์ฌ์ฉ์์ธ์ง ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ํ์ธ
|
| 20 |
+
if User.query.filter_by(username=username).first():
|
| 21 |
+
# (๋์ค์๋ ํ๋์ ๋ฉ์์ง ๋ฑ์ผ๋ก ์ฌ์ฉ์์๊ฒ ์๋ฆผ)
|
| 22 |
+
return "์ด๋ฏธ ์กด์ฌํ๋ ์ฌ์ฉ์ ์ด๋ฆ์
๋๋ค."
|
| 23 |
+
|
| 24 |
+
# ์ ์ฌ์ฉ์ ์์ฑ ๋ฐ ๋น๋ฐ๋ฒํธ ์ํธํ
|
| 25 |
+
new_user = User(username=username)
|
| 26 |
+
new_user.set_password(password)
|
| 27 |
+
|
| 28 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ถ๊ฐ ๋ฐ ์ ์ฅ
|
| 29 |
+
db.session.add(new_user)
|
| 30 |
+
db.session.commit()
|
| 31 |
+
|
| 32 |
+
# ํ์๊ฐ์
์ฑ๊ณต ํ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ด๋
|
| 33 |
+
return redirect(url_for('auth.login'))
|
| 34 |
+
|
| 35 |
+
# GET ์์ฒญ ์ ํ์๊ฐ์
ํ์ด์ง๋ฅผ ๋ณด์ฌ์ค
|
| 36 |
+
return render_template('signup.html')
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ๋ก๊ทธ์ธ ํํธ
|
| 40 |
+
@bp.route('/login', methods=['GET', 'POST'])
|
| 41 |
+
def login():
|
| 42 |
+
if request.method == 'POST':
|
| 43 |
+
username = request.form['username']
|
| 44 |
+
password = request.form['password']
|
| 45 |
+
|
| 46 |
+
# ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฌ์ฉ์ ์ ๋ณด ์กฐํ
|
| 47 |
+
user = User.query.filter_by(username=username).first()
|
| 48 |
+
|
| 49 |
+
# ์ฌ์ฉ์๊ฐ ์กด์ฌํ๊ณ ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ๋์ง ํ์ธ
|
| 50 |
+
if user and user.check_password(password):
|
| 51 |
+
# ์ธ์
์ ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ
|
| 52 |
+
session.clear()
|
| 53 |
+
session['user_id'] = user.id
|
| 54 |
+
session['username'] = user.username
|
| 55 |
+
|
| 56 |
+
# ๋ก๊ทธ์ธ ์ฑ๊ณต ํ ๋ฉ์ธ ํ์ด์ง๋ก ์ด๋
|
| 57 |
+
return redirect(url_for('main.home'))
|
| 58 |
+
else:
|
| 59 |
+
# (๋์ค์๋ ํ๋์ ๋ฉ์์ง ๋ฑ์ผ๋ก ์ฌ์ฉ์์๊ฒ ์๋ฆผ)
|
| 60 |
+
return "๋ก๊ทธ์ธ ์ ๋ณด๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค."
|
| 61 |
+
|
| 62 |
+
# GET ์์ฒญ ์ ๋ก๊ทธ์ธ ํ์ด์ง๋ฅผ ๋ณด์ฌ์ค
|
| 63 |
+
return render_template('login.html')
|
| 64 |
+
|
| 65 |
+
# ๋ก๊ทธ์์ part
|
| 66 |
+
@bp.route('/logout')
|
| 67 |
+
def logout():
|
| 68 |
+
|
| 69 |
+
# ์ธ์
์์ ์ฌ์ฉ์ ์ ๋ณด ์ ๊ฑฐ
|
| 70 |
+
session.clear()
|
| 71 |
+
# ๋ก๊ทธ์์ ํ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ด๋
|
| 72 |
+
return redirect(url_for('auth.login'))
|
src/main.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/main.py
|
| 2 |
+
# ๊ธฐ์กด app.py์์ main๊ณผ ๊ด๋ จํด์ ๋ถ๋ฆฌ
|
| 3 |
+
|
| 4 |
+
from flask import Blueprint, render_template, request, jsonify, session, redirect, url_for
|
| 5 |
+
from . import db
|
| 6 |
+
from .models import User, Diary
|
| 7 |
+
from .emotion_engine import load_emotion_classifier, predict_emotion
|
| 8 |
+
from .recommender import Recommender
|
| 9 |
+
import random
|
| 10 |
+
|
| 11 |
+
bp=Blueprint('main', __name__)
|
| 12 |
+
|
| 13 |
+
emotion_clssifier=load_emotion_classifier()
|
| 14 |
+
recommender=Recommender()
|
| 15 |
+
emotion_emoji_map={
|
| 16 |
+
'๊ธฐ์จ':'๐', 'ํ๋ณต':'๐', '์ฌ๋':'โค๏ธ',
|
| 17 |
+
'๋ถ์':'๐', '์ฌํ':'๐ข', '์์ฒ':'๐',
|
| 18 |
+
'๋ถ๋
ธ':'๐ ', 'ํ์ค':'๐คข', '์ง์ฆ':'๐ค',
|
| 19 |
+
'๋๋':'๐ฎ',
|
| 20 |
+
'์ค๋ฆฝ':'๐',
|
| 21 |
+
'๊ณตํฌ':'๐ฑ'
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
@bp.route('/')
|
| 25 |
+
def home():
|
| 26 |
+
if 'user_id' in session:
|
| 27 |
+
return redirect(url_for('auth.login'))
|
| 28 |
+
return render_template("emotion_homepage.html", username=session.get('username'))
|
src/models.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# models.py
|
| 2 |
+
# db๊ด๋ จํ ๋ชจ๋ธ ์ฝ๋
|
| 3 |
+
# User, Diary ๋ชจ๋ธ์ ์
|
| 4 |
+
|
| 5 |
+
from . import db
|
| 6 |
+
from werkzeug.security import generate_password_hash, check_password_hash # ๋น๋ฐ๋ฒํธ ํด์ฑ(์ํธํ, *์ฒ๋ฆฌํด์ค)
|
| 7 |
+
import uuid
|
| 8 |
+
|
| 9 |
+
class User(db.Model):
|
| 10 |
+
id = db.Column(db.String(36), primary_key=True, default =lambda :str(uuid.uuid4())) # UUID ํ์์ ๊ณ ์ ID, ์ ์ ๋ ๋ฌธ์์ด๋ก ๋ฃ์์ ์๋ค.
|
| 11 |
+
username = db.Column(db.String(80), unique=True, nullable=False) # ์ฌ์ฉ์์ ์ด๋ฆ์ ํฌ๊ทํ๊ณ , ๊ณต๋ฐฑ์ ์์
|
| 12 |
+
password_hash = db.Column(db.String(128), nullable=False) # ๋น๋ฐ๋ฒํธ๋ ํด์๊ฐ์ผ๋ก ์ ์ฅ
|
| 13 |
+
|
| 14 |
+
# User๊ฐ ์ญ์ ๋๋ฉด, ๊ด๋ จ๋ ๋ชจ๋ Diary๋ ํจ๊ป ์ญ์ ๋๋๋ก cascade ์ต์
์ถ๊ฐ
|
| 15 |
+
diaries = db.relationship('Diary', backref='author', lazy=True, cascade="all, delete-orphan")
|
| 16 |
+
|
| 17 |
+
# ๋น๋ฐ๋ฒํธ๋ฅผ ํด์ฑํ์ฌ ์ ์ฅ
|
| 18 |
+
def set_password(self, password):
|
| 19 |
+
self.password_hash = generate_password_hash(password) # ๋น๋ฐ๋ฒํธ๋ฅผ ํด์ฑํ์ฌ ์ ์ฅ
|
| 20 |
+
|
| 21 |
+
# ํด์๋ ๋น๋ฐ๋ฒํธ์ ์
๋ ฅ๋ ๋น๋ฐ๋ฒํธ๋ฅผ ๋น๊ต
|
| 22 |
+
def check_password(self, password):
|
| 23 |
+
return check_password_hash(self.password_hash, password) # ํด์๋ ๋น๋ฐ๋ฒํธ์ ์
๋ ฅ๋ ๋น๋ฐ๋ฒํธ๋ฅผ ๋น๊ต
|
| 24 |
+
|
| 25 |
+
# ์ ์ ํธ์ถ๋ช
ํ์ธ
|
| 26 |
+
def __repr__(self):
|
| 27 |
+
return f"<User id={self.id} username='{self.username}'>"
|
| 28 |
+
|
| 29 |
+
class Diary(db.Model):
|
| 30 |
+
id = db.Column(db.String(36), primary_key=True, default =lambda :str(uuid.uuid4())) # UUID ํ์์ ๊ณ ์ ID
|
| 31 |
+
user_id = db.Column(db.String(36), db.ForeignKey('user.id'), nullable=False) # ์์ฑ์ ID (User ๋ชจ๋ธ๊ณผ ์ฐ๊ด)
|
| 32 |
+
content = db.Column(db.Text, nullable=False) # ์ผ๊ธฐ ๋ด์ฉ
|
| 33 |
+
emotion = db.Column(db.String(20), nullable=False) # ๊ฐ์ ๋ผ๋ฒจ
|
| 34 |
+
timestamp = db.Column(db.DateTime, server_default=db.func.now()) # ์์ฑ ์๊ฐ (๊ธฐ๋ณธ๊ฐ: ํ์ฌ ์๊ฐ)
|
| 35 |
+
|
| 36 |
+
user = db.relationship('User', backref=db.backref('diaries', lazy=True)) # User ๋ชจ๋ธ๊ณผ์ ๊ด๊ณ ์ค์
|
| 37 |
+
|
| 38 |
+
|