Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| import logging | |
| # --- ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก ์ค์ --- | |
| # ์ด ์คํฌ๋ฆฝํธ๊ฐ 'scripts' ํด๋ ์์ ์์ผ๋ฏ๋ก, ๋ถ๋ชจ ๋๋ ํ ๋ฆฌ(ํ๋ก์ ํธ ๋ฃจํธ)๋ฅผ ๊ฒฝ๋ก์ ์ถ๊ฐํฉ๋๋ค. | |
| project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| if project_root not in sys.path: | |
| sys.path.insert(0, project_root) | |
| from src import create_app, db | |
| from src.models import Diary | |
| from src.main import generate_recommendation, recommender # generate_recommendation ์ฌ์ฉ | |
| import datetime | |
| # --- ๋ก๊น ์ค์ --- | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| def migrate_diaries_with_recommendations(): | |
| """ | |
| ๊ธฐ์กด์ ๋ชจ๋ ์ผ๊ธฐ๋ฅผ ์ํํ๋ฉฐ 'recommendation' ํ๋๊ฐ ๋น์ด์๋ ๊ฒฝ์ฐ, | |
| ์๋ก์ด ์ถ์ฒ์ ์์ฑํ์ฌ ์ฑ์๋ฃ์ต๋๋ค. | |
| """ | |
| app = create_app() | |
| with app.app_context(): | |
| logging.info("๋ฐ์ดํฐ ๋ง์ด๊ทธ๋ ์ด์ ์ ์์ํฉ๋๋ค...") | |
| # ์ถ์ฒ์ด ๋น์ด์๋ ๋ชจ๋ ์ผ๊ธฐ๋ฅผ ์กฐํํฉ๋๋ค. | |
| diaries_to_update = Diary.query.filter((Diary.recommendation == None) | (Diary.recommendation == '')).all() | |
| if not diaries_to_update: | |
| logging.info("์ ๋ฐ์ดํธํ ์ผ๊ธฐ๊ฐ ์์ต๋๋ค. ๋ชจ๋ ์ผ๊ธฐ์ ์ถ์ฒ์ด ์ด๋ฏธ ์กด์ฌํฉ๋๋ค.") | |
| return | |
| logging.info(f"์ด {len(diaries_to_update)}๊ฐ์ ์ผ๊ธฐ๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค.") | |
| updated_count = 0 | |
| failed_count = 0 | |
| for diary in diaries_to_update: | |
| try: | |
| logging.info(f"ID: {diary.id} ์ผ๊ธฐ ์ฒ๋ฆฌ ์ค...") | |
| # 1. Gemini API๋ฅผ ํตํด ์ถ์ฒ ์์ฑ ์๋ | |
| recommendation_text = generate_recommendation(diary.content, diary.emotion) | |
| # 2. ์คํจ ์, Recommender ํด๋์ค๋ก ๋์ฒด | |
| if recommendation_text is None: | |
| logging.warning(f"ID: {diary.id} - Gemini ์ถ์ฒ ์คํจ. Recommender ํด๋์ค๋ก ๋์ฒดํฉ๋๋ค.") | |
| su_yoong_recs = recommender.recommend(diary.emotion, '์์ฉ') | |
| jeon_hwan_recs = recommender.recommend(diary.emotion, '์ ํ') | |
| # diary_logic.js๊ฐ ํ์ฑํ ์ ์๋ ํ์์ผ๋ก ๋ง๋ญ๋๋ค. | |
| recommendation_text = f"## [์์ฉ]\n" | |
| for rec in su_yoong_recs: | |
| recommendation_text += f"* {rec}\n" | |
| recommendation_text += f"\n## [์ ํ]\n" | |
| for rec in jeon_hwan_recs: | |
| recommendation_text += f"* {rec}\n" | |
| # 3. ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ | |
| diary.recommendation = recommendation_text | |
| updated_count += 1 | |
| logging.info(f"ID: {diary.id} - ์ถ์ฒ ์์ฑ ์๋ฃ.") | |
| except Exception as e: | |
| failed_count += 1 | |
| logging.error(f"ID: {diary.id} ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {e}") | |
| if updated_count > 0: | |
| try: | |
| db.session.commit() | |
| logging.info(f"์ฑ๊ณต: {updated_count}๊ฐ ์ผ๊ธฐ์ ์ถ์ฒ ์ ๋ณด ์ ๋ฐ์ดํธ ์๋ฃ.") | |
| except Exception as e: | |
| db.session.rollback() | |
| logging.error(f"๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฐ ์ค ์ค๋ฅ ๋ฐ์: {e}") | |
| if failed_count > 0: | |
| logging.warning(f"์คํจ: {failed_count}๊ฐ ์ผ๊ธฐ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์.") | |
| logging.info("๋ง์ด๊ทธ๋ ์ด์ ์๋ฃ.") | |
| if __name__ == '__main__': | |
| migrate_diaries_with_recommendations() |