-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvice.py
More file actions
276 lines (229 loc) · 10.1 KB
/
advice.py
File metadata and controls
276 lines (229 loc) · 10.1 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from flask import Flask, request, jsonify
import pymysql
import os
from dotenv import load_dotenv
import logging
import calendar
from langchain.chat_models import AzureChatOpenAI
from langchain.schema import HumanMessage, SystemMessage
load_dotenv()
app = Flask(__name__)
# DB Connection
db_config = {
'host': os.getenv('DB_HOST'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD'),
'database': os.getenv('DB_NAME')
}
# Logger 설정
logging.basicConfig(level=logging.DEBUG)
# Azure OpenAI 클라이언트 초기화
model = AzureChatOpenAI(
azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT"), # gpt-4o is set by env
temperature=1.0
)
def get_user_nutritional_needs(user_id):
connection = pymysql.connect(**db_config)
try:
with connection.cursor() as cursor:
sql = "SELECT BODY_WEIGHT, RDI FROM USER WHERE ID = %s"
cursor.execute(sql, (user_id,))
result = cursor.fetchone()
if result:
body_weight, rdi = result
return body_weight, rdi
else:
return None
except pymysql.MySQLError as e:
logging.error(f"Database error: {e}")
return None
finally:
connection.close()
def get_daily_totals(user_id, date):
connection = pymysql.connect(**db_config)
try:
with connection.cursor() as cursor:
sql = "SELECT CARBO, PROTEIN, FAT, RD_CARBO, RD_PROTEIN, RD_FAT FROM USER_NT WHERE ID = %s AND DATE = %s"
cursor.execute(sql, (user_id, date))
result = cursor.fetchone()
if result:
return result
else:
return None
except pymysql.MySQLError as e:
logging.error(f"Database error: {e}")
return None
finally:
connection.close()
def get_monthly_data(year, month, user_id):
connection = pymysql.connect(**db_config)
try:
with connection.cursor() as cursor:
sql = """
SELECT DATE, FOOD_INDEX, FOOD_NAME, FOOD_PT, FOOD_FAT, FOOD_CH, FOOD_KCAL
FROM FOOD
WHERE YEAR(DATE) = %s AND MONTH(DATE) = %s AND ID = %s
ORDER BY DATE
"""
cursor.execute(sql, (year, month, user_id))
results = cursor.fetchall()
num_days = calendar.monthrange(year, month)[1] # 해당 월의 일수 계산
foods_list = [[] for _ in range(num_days)] # 각 날짜별 음식 리스트
percentages_list = [{} for _ in range(num_days)] # 각 날짜별 백분율 리스트
for row in results:
day = row[0].day - 1 # 0-based index for lists
food_info = {
"food_index": row[1],
"food_name": row[2],
"protein": row[3],
"fat": row[4],
"carbohydrates": row[5],
"calories": row[6]
}
foods_list[day].append(food_info)
# Add daily percentages
for day in range(num_days):
date_str = f"{year}-{str(month).zfill(2)}-{str(day+1).zfill(2)}" # 1-based day for dates
daily_totals = get_daily_totals(user_id, date_str)
if daily_totals:
carb_total, protein_total, fat_total, rd_carb, rd_protein, rd_fat = daily_totals
percentages_list[day] = {
"carbohydrates_percentage": round((carb_total / rd_carb) * 100, 1) if rd_carb > 0 else 0,
"protein_percentage": round((protein_total / rd_protein) * 100, 1) if rd_protein > 0 else 0,
"fat_percentage": round((fat_total / rd_fat) * 100, 1) if rd_fat > 0 else 0
}
return {
"foods": foods_list,
"percentages": percentages_list
}
except pymysql.MySQLError as e:
logging.error(f"Database error: {e}")
return {"error": "Database error"}
finally:
connection.close()
@app.route('/api/food/quarterly', methods=['GET'])
def get_quarterly_food():
year = request.args.get('year')
start_month = request.args.get('month')
user_id = request.args.get('user_id')
if not year or not start_month or not user_id:
return jsonify({"error": "Year, start month, and user_id are required"}), 400
try:
year = int(year)
start_month = int(start_month)
if start_month < 1 or start_month > 12:
return jsonify({"error": "Invalid month. Please enter a value between 1 and 12."}), 400
except ValueError:
return jsonify({"error": "Year and month must be integers."}), 400
quarterly_data = {}
for i in range(-1, 2): # 이전 달, 현재 달, 다음 달 순서로 데이터를 가져오기
month = (start_month + i - 1) % 12 + 1
current_year = year + (start_month + i - 1) // 12
monthly_data = get_monthly_data(current_year, month, user_id)
quarterly_data[f"{current_year}-{str(month).zfill(2)}"] = monthly_data
return jsonify(quarterly_data)
def get_advice(carbohydrates_percentage, protein_percentage, fat_percentage):
try:
# Chat API 호출
messages = [
SystemMessage(content="You are a nutrition expert providing dietary advice based on user's nutrient intake.Give 5 sentences of advice in Korean"),
HumanMessage(content=f"Here are my monthly nutrient intake percentages:\n"
f"Carbohydrates: {carbohydrates_percentage}%\n"
f"Protein: {protein_percentage}%\n"
f"Fat: {fat_percentage}%\n"
f"Please provide advice on how to improve my diet")
]
response = model(messages)
# 응답 객체의 내용을 로그로 출력
logging.debug(f"LLM Response Object: {response}")
# 응답 내용을 추출
response_content = response[0].content if isinstance(response, list) else response.content
logging.debug(f"LLM Response Content: {response_content}") # LLM 응답을 콘솔에 출력
return response_content
except Exception as e:
logging.error(f"Error in get_advice: {e}")
logging.debug(f"Carbohydrates: {carbohydrates_percentage}, Protein: {protein_percentage}, Fat: {fat_percentage}") # 입력 데이터 디버깅
return {"error": f"Failed to get advice from LLM: {str(e)}"}
@app.route('/api/food/advice', methods=['GET'])
def get_advice_route():
year = request.args.get('year')
month = request.args.get('month')
user_id = request.args.get('user_id')
if not year or not month or not user_id:
return jsonify({"error": "Year, month, and user_id are required"}), 400
try:
year = int(year)
month = int(month)
if month < 1 or month > 12:
return jsonify({"error": "Invalid month. Please enter a value between 1 and 12."}), 400
except ValueError:
return jsonify({"error": "Year and month must be integers."}), 400
# 현재 달의 데이터를 가져옵니다
monthly_data = get_monthly_data(year, month, user_id)
if "error" in monthly_data:
logging.error(f"Failed to get monthly data: {monthly_data['error']}")
return jsonify(monthly_data), 500
percentages_list = monthly_data['percentages']
# 한 달치 평균 계산
total_carbs = 0
total_protein = 0
total_fat = 0
count = 0
for day_data in percentages_list:
if day_data: # 빈 데이터는 건너뜀
total_carbs += day_data.get('carbohydrates_percentage', 0)
total_protein += day_data.get('protein_percentage', 0)
total_fat += day_data.get('fat_percentage', 0)
count += 1
if count == 0:
logging.error("No valid data to calculate averages")
return jsonify({"error": "No valid data to calculate averages"}), 404
average_carbs = total_carbs / count
average_protein = total_protein / count
average_fat = total_fat / count
averages = {
"average_carbohydrates_percentage": round(average_carbs, 1),
"average_protein_percentage": round(average_protein, 1),
"average_fat_percentage": round(average_fat, 1)
}
# LLM을 통해 조언을 받습니다
advice = get_advice(averages["average_carbohydrates_percentage"],
averages["average_protein_percentage"],
averages["average_fat_percentage"])
# 콘솔에 출력
logging.debug(f"LLM Advice: {advice}")
return jsonify({"averages": averages, "advice": advice})
@app.route('/api/food/avg_kcal', methods=['GET'])
def get_avg_kcal():
year = request.args.get('year')
month = request.args.get('month')
user_id = request.args.get('user_id')
if not year or not month or not user_id:
return jsonify({"error": "Year, month, and user_id are required"}), 400
try:
year = int(year)
month = int(month)
if month < 1 or month > 12:
return jsonify({"error": "Invalid month. Please enter a value between 1 and 12."}), 400
except ValueError:
return jsonify({"error": "Year and month must be integers."}), 400
# 현재 달의 데이터를 가져옵니다
monthly_data = get_monthly_data(year, month, user_id)
if "error" in monthly_data:
logging.error(f"Failed to get monthly data: {monthly_data['error']}")
return jsonify(monthly_data), 500
foods_list = monthly_data['foods']
# 한 달치 평균 칼로리 계산
total_kcal = 0
count = 0
for day_foods in foods_list:
for food in day_foods:
total_kcal += food.get('calories', 0)
count += 1
if count == 0:
logging.error("No valid data to calculate averages")
return jsonify({"error": "No valid data to calculate averages"}), 404
average_kcal = total_kcal / count
return jsonify({"average_kcal": round(average_kcal, 1)})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001) # 다른 포트로