Spaces:
Runtime error
Runtime error
| import os | |
| import json | |
| from config.settings import client, MODEL | |
| rules_file = os.path.join(os.path.dirname(__file__), 'rules.json') | |
| def get_basic_recommendations(user_data, rules_file=rules_file): | |
| with open(rules_file, 'r', encoding='utf-8') as f: | |
| rules_data = json.load(f) | |
| recommendations = [] | |
| for cond in user_data.get('health_conditions', []): | |
| if cond.lower() in rules_data.get('conditions', {}): | |
| recommendations.extend(rules_data['conditions'][cond.lower()]) | |
| goal = user_data.get('goal', '').lower() | |
| for key, recs in rules_data.get('goals', {}).items(): | |
| if key in goal: | |
| recommendations.extend(recs) | |
| level = user_data.get('fitness_level', 'beginner').lower() | |
| if level in rules_data.get('fitness_level', {}): | |
| recommendations.extend(rules_data['fitness_level'][level]) | |
| return recommendations | |
| def generate_exercise_plan(user_data): | |
| rules = get_basic_recommendations(user_data) | |
| system_prompt = f""" | |
| Bạn là huấn luyện viên cá nhân vui tính 💪🔥. | |
| Thông tin người dùng: | |
| - Tuổi: {user_data.get('age', 'chưa biết')} | |
| - Giới tính: {user_data.get('gender', 'chưa biết')} | |
| - Cân nặng: {user_data.get('weight', 'chưa biết')} | |
| - Chiều cao: {user_data.get('height', 'chưa biết')} | |
| - Thể lực hiện tại: {user_data.get('fitness_level', 'nhẹ')} | |
| - Mục tiêu: {user_data.get('goal', 'cải thiện sức khỏe')} | |
| - Thời gian rảnh mỗi ngày: {user_data.get('available_time', 30)} phút | |
| - Bệnh nền: {', '.join(user_data.get('health_conditions', []))} | |
| Rule cần tuân theo: | |
| {chr(10).join(f"- {r}" for r in rules)} | |
| Yêu cầu: | |
| - Tạo kế hoạch 7 ngày dạng bảng | |
| - Friendly intro trước khi xuất plan | |
| - Mỗi ngày có: Tên buổi tập, thời gian, danh sách bài tập, số hiệp, số lần / thời gian, lưu ý an toàn | |
| - Không hỏi lại thông tin, nếu thiếu dùng default | |
| - Không nhét link hoặc placeholder | |
| """ | |
| response = client.chat.completions.create( | |
| model=MODEL, | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": "Hãy tạo kế hoạch 7 ngày theo yêu cầu."} | |
| ], | |
| temperature=0.7, | |
| max_tokens=3000 | |
| ) | |
| return response.choices[0].message.content | |