Spaces:
Runtime error
Runtime error
| """ | |
| PregoPal - 菜品推荐模块 | |
| ======================== | |
| 根据孕妇需求推荐今日菜品。 | |
| 当前:规则模板随机推荐(轻量 baseline) | |
| 后续:MiniCPM-o 4.5 AI 对话推荐 | |
| """ | |
| import datetime | |
| import random | |
| from config import MEAL_TEMPLATES, TRIMESTER_ADJUSTMENTS, TRIMESTER_TIPS | |
| class MealRecommender: | |
| """根据孕妇需求推荐今日菜品""" | |
| def __init__(self): | |
| self.templates = MEAL_TEMPLATES | |
| def get_recommendation(self, preference: str = "", trimester: str = "孕中期", restrictions: str = ""): | |
| """ | |
| 根据孕妇偏好和孕期阶段推荐菜品 | |
| Args: | |
| preference: 孕妇的口味偏好或要求 | |
| trimester: 孕期阶段(孕早期/孕中期/孕晚期) | |
| restrictions: 饮食禁忌 | |
| """ | |
| # 根据孕期阶段调整推荐 | |
| adj = TRIMESTER_ADJUSTMENTS.get(trimester, TRIMESTER_ADJUSTMENTS["孕中期"]) | |
| # 根据偏好选择菜品 | |
| breakfast = random.choice(self.templates["早餐"]) | |
| lunch = random.choice(self.templates["午餐"]) | |
| dinner = random.choice(self.templates["晚餐"]) | |
| snack = random.choice(self.templates["加餐"]) | |
| # 如果有偏好,尝试匹配 | |
| if preference: | |
| all_foods = [] | |
| for meal_list in self.templates.values(): | |
| all_foods.extend(meal_list) | |
| matched = [f for f in all_foods if any(kw in f for kw in preference.split())] | |
| if matched: | |
| pass # 简化处理,后续 AI 版本会真正理解偏好 | |
| result = { | |
| "date": datetime.date.today().isoformat(), | |
| "trimester": trimester, | |
| "preference": preference, | |
| "focus": adj["focus"], | |
| "meals": { | |
| "早餐": breakfast, | |
| "午餐": lunch, | |
| "晚餐": dinner, | |
| "加餐": snack | |
| }, | |
| "tips": self._generate_tips(trimester, preference) | |
| } | |
| return result | |
| def _generate_tips(self, trimester, preference): | |
| """生成饮食建议""" | |
| tips = [] | |
| trimester_tips = TRIMESTER_TIPS | |
| tips.append(trimester_tips.get(trimester, "")) | |
| tips.append("💡 建议每天饮水1.5-2L,适量运动如散步30分钟。") | |
| tips.append("💡 保持心情愉快,避免过度焦虑。") | |
| return tips | |
| def format_meal_plan(self, recommendation: dict) -> str: | |
| """将推荐结果格式化为可读文本""" | |
| lines = [ | |
| f"📅 {recommendation['date']} 孕期食谱推荐", | |
| f"🤰 阶段: {recommendation['trimester']}", | |
| f"🎯 重点: {recommendation['focus']}", | |
| "", | |
| "🍳 今日食谱:", | |
| ] | |
| for meal_time, food in recommendation["meals"].items(): | |
| lines.append(f" {meal_time}: {food}") | |
| lines.append("") | |
| for tip in recommendation["tips"]: | |
| lines.append(tip) | |
| return "\n".join(lines) | |