File size: 5,275 Bytes
fb531d8
7e0a969
b9ace18
 
7e0a969
 
62a872b
 
b9ace18
6be0d08
 
b9ace18
 
7e0a969
6be0d08
 
b9ace18
7e0a969
 
b9ace18
 
 
7e0a969
 
 
 
 
b9ace18
62a872b
b9ace18
 
0d10bbe
b9ace18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e0a969
b9ace18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62a872b
7e0a969
7883aa8
62a872b
 
7883aa8
3ae2913
7883aa8
3ae2913
7883aa8
 
7e0a969
b9ace18
62a872b
 
3ae2913
62a872b
 
 
 
 
d97a87b
7e0a969
b9ace18
62a872b
 
3ae2913
7883aa8
62a872b
 
53acd3d
7883aa8
40f6db2
 
 
 
 
53acd3d
 
 
 
d97a87b
62a872b
 
 
 
d97a87b
62a872b
 
 
 
d97a87b
3ae2913
029d034
 
3ae2913
57f45cf
3ae2913
029d034
 
3ae2913
 
53acd3d
3ae2913
53acd3d
7e0a969
53acd3d
b9ace18
6be0d08
7e0a969
53acd3d
7e181cd
febf51b
53acd3d
 
 
febf51b
 
b9ace18
 
 
 
 
febf51b
 
 
b9ace18
febf51b
 
 
 
 
 
 
 
 
 
 
 
 
7e181cd
febf51b
 
 
 
 
 
b9ace18
 
40f6db2
 
 
 
 
 
 
4d17849
b9ace18
4d17849
b9ace18
e134b0e
b9ace18
4d17849
b9ace18
4d17849
b9ace18
68574ac
b9ace18
4d17849
b9ace18
4d17849
b9ace18
7e181cd
b9ace18
e134b0e
66d8eed
4d17849
888f5c1
b9ace18
 
68574ac
b9ace18
4d17849
888f5c1
4d17849
b9ace18
4d17849
b9ace18
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
import streamlit as st
import streamlit.components.v1 as components
from huggingface_hub import InferenceClient
import os


# ================= BMI FUNCTIONS =================

def calculate_bmi(weight, height):
    if height == 0:
        return 0
    height_m = height / 100
    return weight / (height_m ** 2)



def bmi_category(bmi):
    if bmi < 18.5:
        return "Underweight"
    elif bmi < 25:
        return "Normal Weight"
    elif bmi < 30:
        return "Overweight"
    else:
        return "Obese"


def build_prompt(name, gender, height, weight, goal, fitness_level, equipment):

    bmi = calculate_bmi(weight, height)
    bmi_status = bmi_category(bmi)

    equipment_list = ", ".join(equipment) if equipment else "No Equipment"

    prompt = f"""
You are a certified professional fitness trainer.

Create a structured 5-day personalized workout plan.

User Profile:
- Name: {name}
- Gender: {gender}
- Height: {height} cm
- Weight: {weight} kg
- BMI: {bmi:.2f} ({bmi_status})
- Goal: {goal}
- Fitness Level: {fitness_level}
- Available Equipment: {equipment_list}

Instructions:
1. Divide clearly into Day 1 to Day 5.
2. Include exercise name.
3. Include sets and reps.
4. Include rest period.
5. Adjust intensity based on BMI category.
6. Avoid unsafe exercises for beginners.
7. Keep the plan professional and easy to follow.
"""

    return prompt, bmi, bmi_status



# ================= QUERY MODEL =================

def query_model(prompt):

    try:

        HF_TOKEN = os.getenv("HF_TOKENN")

        client = InferenceClient(
            model="mistralai/Mistral-7B-Instruct-v0.2",
            token=HF_TOKEN
        )

        response = client.chat_completion(

            messages=[
                {"role": "system", "content": "You are a certified professional fitness trainer."},
                {"role": "user", "content": prompt}
            ],

            max_tokens=600,
            temperature=0.7
        )

        return response.choices[0].message.content


    except Exception as e:

        return f"Error: {str(e)}"



# ================= PAGE CONFIG =================

st.set_page_config(
    page_title="FitPlan AI",
    page_icon="πŸ’ͺ",
    layout="wide"
)



# ================= SIDEBAR =================

st.sidebar.title("πŸ’ͺ FitPlan AI")

page = st.sidebar.radio(
    "Navigate",
    ["🏠 Fitness Form", "πŸ“Š BMI Result"]
)



# ================= FORM PAGE =================

if page == "🏠 Fitness Form":

    st.title("🏠 Fitness Profile Form")

    name = st.text_input("Name")

    gender = st.selectbox(
        "Gender",
        ["Male", "Female"]
    )

    fitness_level = st.selectbox(
        "Fitness Level",
        ["Beginner", "Intermediate", "Advanced"]
    )

    height_cm = st.number_input(
        "Height (cm)",
        min_value=0.0
    )

    weight_kg = st.number_input(
        "Weight (kg)",
        min_value=0.0
    )

    goal = st.selectbox(
        "Fitness Goal",
        ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
    )

    equipment = st.multiselect(
        "Available Equipment",
        ["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment"]
    )

    submit = st.button("Submit Profile")

    if submit:

        bmi = calculate_bmi(weight_kg, height_cm)

        category = bmi_category(bmi)

        st.session_state["name"] = name
        st.session_state["gender"] = gender
        st.session_state["fitness_level"] = fitness_level
        st.session_state["bmi"] = bmi
        st.session_state["category"] = category
        st.session_state["goal"] = goal
        st.session_state["equipment"] = equipment

        # ADD THESE TWO (IMPORTANT)
        st.session_state["height"] = height_cm
        st.session_state["weight"] = weight_kg


        st.success("Profile submitted successfully! Go to BMI Result page.")



# ================= RESULT PAGE =================

elif page == "πŸ“Š BMI Result":

    st.title("πŸ“Š BMI Result")

    if "bmi" not in st.session_state:

        st.warning("Please fill the Fitness Form first.")

    else:

        st.write(f"Name: {st.session_state['name']}")
        st.write(f"Gender: {st.session_state['gender']}")
        st.write(f"Fitness Level: {st.session_state['fitness_level']}")
        st.write(f"BMI: {st.session_state['bmi']}")
        st.write(f"Category: {st.session_state['category']}")
        st.write(f"Goal: {st.session_state['goal']}")
        st.write(f"Equipment: {', '.join(st.session_state['equipment'])}")



        gender = st.session_state["gender"]
        bmi_status = st.session_state["category"]
        goal = st.session_state["goal"]
        fitness_level = st.session_state["fitness_level"]
        equipment_list = ", ".join(st.session_state["equipment"])



        prompt, bmi, bmi_status = build_prompt(

    st.session_state["name"],

    st.session_state["gender"],

    st.session_state["height"],

    st.session_state["weight"],

    st.session_state["goal"],

    st.session_state["fitness_level"],

    st.session_state["equipment"]

)




        # FIXED POSITION
        with st.spinner("Generating AI Fitness Plan..."):

            result = query_model(prompt)



        st.subheader("AI Fitness Plan")

        st.write(result)