File size: 4,871 Bytes
1646a6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import CodeAgent, InferenceClientModel, WebSearchTool, tool, Tool
import gradio as gr

# 🥗 Tool 1: حساب السعرات
@tool
def calculate_calories(meal_description: str) -> str:
    """
    Roughly estimate calories based on the meal description.
    Args:
        meal_description (str): Description of the food consumed.
    """
    meal_description = meal_description.lower()
    calories = 0
    if "egg" in meal_description: calories += 70
    if "bread" in meal_description or "toast" in meal_description: calories += 80
    if "chicken" in meal_description: calories += 250
    if "rice" in meal_description or "pasta" in meal_description: calories += 200
    if "cheese" in meal_description: calories += 100
    if "salad" in meal_description: calories += 50
    return f"Estimated total: around {calories} calories."

# 💡 Tool 2: نصيحة غذائية
@tool
def give_health_tip(meal_description: str) -> str:
    """
    Give a quick health tip based on the meal type.
    Args:
        meal_description (str): Description of the food consumed.
    """
    if "fried" in meal_description:
        return "Try grilling or baking instead of frying for a healthier option."
    elif "sugar" in meal_description or "dessert" in meal_description:
        return "Limit sugary foods to avoid energy crashes."
    elif "salad" in meal_description:
        return "Great choice! Add some protein to make it a complete meal."
    else:
        return "Balance your meal with some vegetables and water."

# 🏋️‍♂️ Tool 3: تمرين اختياري
@tool
def suggest_exercise(meal_description: str) -> str:
    """
    Optional: Suggest a short exercise routine if user asks about workouts.
    Args:
        meal_description (str): Description of the food or time of day.
    """
    meal_description = meal_description.lower()
    if "breakfast" in meal_description or "morning" in meal_description:
        return "Start your day with 15 minutes of stretching or a quick walk."
    elif "lunch" in meal_description:
        return "After lunch, take a 10-minute walk to help digestion."
    elif "dinner" in meal_description or "night" in meal_description:
        return "Try relaxing yoga before bed to improve sleep quality."
    else:
        return "If you want a light workout, a 30-minute walk is always great."

# 🌿 Tool 4: الخطة الأسبوعية + نصايح النوم
class HealthyLifestyleTool(Tool):
    name = "healthy_lifestyle_advisor"
    description = """
    Suggests a healthy weekly meal plan, light exercises, and sleep tips.
    """

    inputs = {
        "goal": {
            "type": "string",
            "description": "Health goal (e.g., 'weight loss', 'muscle gain', 'general health', 'better sleep').",
        }
    }
    output_type = "string"

    def forward(self, goal: str):
        goal = goal.lower()
        if "weight loss" in goal:
            return (
                "🍎 Weight Loss Plan:\n"
                "- Meals: Oats with fruits, grilled chicken with veggies, and light dinner salads.\n"
                "- Exercise: 30 min brisk walking daily.\n"
                "- Sleep Tip: Aim for 7–8 hours, avoid screens before bed."
            )
        elif "muscle gain" in goal:
            return (
                "💪 Muscle Gain Plan:\n"
                "- Meals: Eggs, rice with chicken, protein shakes.\n"
                "- Exercise: Weight training 4x/week.\n"
                "- Sleep Tip: 8–9 hours for muscle recovery."
            )
        elif "better sleep" in goal:
            return (
                "🌙 Better Sleep Routine:\n"
                "- Meals: Avoid caffeine at night.\n"
                "- Exercise: Gentle yoga in the evening.\n"
                "- Sleep Tip: Fixed bedtime, dark room."
            )
        else:
            return (
                "🥗 General Health Tips:\n"
                "- Meals: Balanced diet with fruits and veggies.\n"
                "- Exercise: 30 min walk daily.\n"
                "- Sleep Tip: Regular sleep schedule."
            )

# 🧠 إنشاء الوكيل
agent = CodeAgent(
    tools=[WebSearchTool(), calculate_calories, give_health_tip, suggest_exercise, HealthyLifestyleTool()],
    model=InferenceClientModel(),
    max_steps=10,
    verbosity_level=2
)

def health_assistant(prompt):
    response = agent.run(prompt)
    return response

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("## 🥗 HealthCheck Agent — Powered by smolagents")
    with gr.Row():
        user_input = gr.Textbox(label="💬 Write your question (e.g., 'I had rice and chicken for lunch')")
    output = gr.Textbox(label="🧠 Agent's Answer")
    submit_btn = gr.Button("Ask Alfred 🧑‍⚕️")

    submit_btn.click(health_assistant, inputs=user_input, outputs=output)

demo.launch()