File size: 13,127 Bytes
a895c78
 
 
 
3d809ab
e613138
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a0a66e
 
 
 
 
 
a573147
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
953c251
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12b5003
 
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12b5003
 
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e613138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a895c78
 
 
 
e613138
12b5003
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e613138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a895c78
 
 
12b5003
 
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e613138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a895c78
 
 
 
12b5003
 
a895c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e613138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a895c78
 
953c251
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# app.py
import streamlit as st
from groq import Groq
import time
import os
import re



# Page Configuration
st.set_page_config(
    page_title="HealthAI Advisor",
    page_icon="🌿",
    layout="centered",
    initial_sidebar_state="expanded"
)

# Initialize Groq client
with st.sidebar:
    # API key input for Groq
    st.session_state["GROQ_API_KEY"] = st.text_input("GROQ API Key", type="password")
    st.sidebar.markdown("πŸ”‘ [Get Groq API Key](https://console.groq.com/keys)")
   
   
   # Initialize Groq client
if st.session_state["GROQ_API_KEY"]:            
    client = Groq(api_key=st.session_state["GROQ_API_KEY"])
else :
    client = Groq(api_key=os.environ.get("GROQ_API_KEY", st.secrets.get("GROQ_API_KEY", "sk-your-key")))

# Custom CSS
st.markdown("""
<style>
    /* Base styles */
    .recommendation-box {
        padding: 1.5rem;
        border-radius: 10px;
        margin-top: 1rem;
        background-color: var(--background-color);
        border: 1px solid var(--secondary-background-color);
        color: var(--text-color);
    }
    
    /* Theme-aware text colors */
    .st-emotion-cache-10trblm {
        color: var(--text-color) !important;
    }
    
    /* Progress bar styling */
    .st-emotion-cache-1ii7l3v {
        background-color: var(--secondary-background-color) !important;
    }
    
    .st-emotion-cache-keje6w {
        background-color: var(--primary-color) !important;
    }
</style>
""", unsafe_allow_html=True)

# Helper Functions
def get_groq_response(prompt, model="llama-3.3-70b-versatile"):
    try:
        response = client.chat.completions.create(
            messages=[{"role": "user", "content": prompt}],
            model=model,
            temperature=0.3,
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

def calculate_bmi(weight, height):
    return weight / ((height / 100) ** 2)

# Main App
def main():
    st.sidebar.title("🌱 Navigation")
    page = st.sidebar.radio("Choose Section", 
        ["🏠 Dashboard", "🍎 Diet Planner", "πŸ’ͺ Workout Generator", "🧘 Yoga Advisor", "πŸ€– AI Coach"])

    # Dashboard
    if page == "🏠 Dashboard":
        st.header("🏠 HealthAI Dashboard")
        st.divider()
        
        with st.container():
            col1, col2 = st.columns(2)
            with col1:
                st.subheader("Body Metrics")
                weight = st.number_input("Weight (kg)", min_value=30.0, max_value=200.0, value=65.0)
                height = st.number_input("Height (cm)", min_value=100.0, max_value=250.0, value=170.0)
                
                if st.button("Calculate BMI"):
                    bmi = calculate_bmi(weight, height)
                    st.metric("BMI", f"{bmi:.1f}")
                    
                    # Calculate progress value (0-1 scale)
                    progress_value = min(max((bmi - 15)/(40 - 15), 0), 1)
                    
                    # Create a layout for the progress bar with label
                    col_a, col_b = st.columns([4, 1])
                    with col_a:
                        st.progress(progress_value)
                    with col_b:
                        st.caption("15 ⟷ 40")
                        
                        

            with col2:
                st.subheader("Daily Goals")
                st.metric("Water Intake", "2.4L", "+0.6L remaining")
                st.metric("Steps", "8,432", "12k goal")
                st.metric("Active Minutes", "45", "60min target")

        st.divider()
        st.subheader("Weekly Progress")
        col1, col2, col3 = st.columns(3)
        col1.metric("Calories Burned", "12,430", "1,234 today")
        col2.metric("Exercise Minutes", "324", "45 today")
        col3.metric("Sleep Average", "7.2h", "-0.8h from goal")

    # Diet Planner
    elif page == "🍎 Diet Planner":
        st.header("🍎 AI Nutritionist")
        st.divider()
        
        with st.form("diet_form"):
            col1, col2 = st.columns(2)
            with col1:
                diet_goal = st.selectbox("Primary Goal", 
                    ["Weight Loss", "Muscle Gain", "Maintenance", "Improve Digestion"])
                allergies = st.multiselect("Allergies/Dietary Restrictions",
                    ["Dairy", "Gluten", "Nuts", "Eggs", "Vegetarian", "Vegan"])
            
            with col2:
                calorie_target = st.slider("Daily Calorie Target", 1200, 3000, 2000)
                preferred_cuisine = st.selectbox("Preferred Cuisine",
                    ["Any", "Mediterranean", "Asian", "Mexican", "Continental"])
            
            if st.form_submit_button("🍳 Generate Meal Plan"):
                with st.spinner("Cooking up your personalized plan..."):
                    prompt = f"""Create a {diet_goal} meal plan for {calorie_target} calories, 
                        considering {allergies} restrictions and {preferred_cuisine} cuisine preference. 
                        Include breakfast, lunch, dinner, and snacks with macros with attractive emogies and attractive format"""
                    response = get_groq_response(prompt)
                    st.subheader("🍽️ Your Personalized Meal Plan")
                    # st.markdown(f'<div class="recommendation-box">\n\n{response}</div>', unsafe_allow_html=True)
                    think_match = re.search(r'<think>(.*?)</think>', response, re.DOTALL)
                    if think_match:
                        think_text = think_match.group(1).strip()
                        # Remove the <think>...</think> part from the original response
                        response_without_think = re.sub(r'<think>.*?</think>', '', response, flags=re.DOTALL).strip()
                    else:
                        think_text = ""
                        response_without_think = response

                    # Print the think part using a Streamlit expander if it exists
                    if think_text:
                        with st.expander("Internal Thoughts"):
                            st.write(think_text)

                    # Print the remaining response using st.markdown
                    st.markdown(f'<div class="recommendation-box">\n\n{response_without_think}</div>', unsafe_allow_html=True)

                    

    # Workout Generator
    elif page == "πŸ’ͺ Workout Generator":
        st.header("πŸ’ͺ AI Fitness Coach")
        st.divider()
        
        with st.form("workout_form"):
            col1, col2 = st.columns(2)
            with col1:
                fitness_level = st.selectbox("Fitness Level", 
                    ["Beginner", "Intermediate", "Advanced"])
                workout_type = st.selectbox("Workout Type",
                    ["Full Body", "Upper Body", "Lower Body", "Cardio", "HIIT"])
            
            with col2:
                equipment = st.multiselect("Available Equipment",
                    ["Dumbbells", "Resistance Bands", "None", "Barbell", "Kettlebells"])
                duration = st.slider("Duration (minutes)", 15, 120, 45)
            
            if st.form_submit_button("πŸ‹οΈ Generate Workout"):
                with st.spinner("Building your workout routine..."):
                    prompt = f"""Create a {duration}-minute {workout_type} workout for a {fitness_level} 
                        using {equipment}. Include warm-up, exercises with sets/reps, and cool-down with attractive emogies and attractive format"""
                    response = get_groq_response(prompt)
                    st.subheader("πŸ“ Your Custom Workout Plan")
                    # st.markdown(f'<div class="recommendation-box">\n\n{response}</div>', unsafe_allow_html=True)
                    think_match = re.search(r'<think>(.*?)</think>', response, re.DOTALL)
                    if think_match:
                        think_text = think_match.group(1).strip()
                        # Remove the <think>...</think> part from the original response
                        response_without_think = re.sub(r'<think>.*?</think>', '', response, flags=re.DOTALL).strip()
                    else:
                        think_text = ""
                        response_without_think = response

                    # Print the think part using a Streamlit expander if it exists
                    if think_text:
                        with st.expander("Internal Thoughts"):
                            st.write(think_text)

                    # Print the remaining response using st.markdown
                    st.markdown(f'<div class="recommendation-box">\n\n{response_without_think}</div>', unsafe_allow_html=True)


    # Yoga Advisor
    elif page == "🧘 Yoga Advisor":
        st.header("🧘 AI Yoga Guide")
        st.divider()
        with st.form("yoga_form"):
            col1, col2 = st.columns(2)
            with col1:
                yoga_goal = st.selectbox("Session Focus",
                    ["Stress Relief", "Flexibility", "Strength", "Balance", "Morning Energy"])
                experience_level = st.selectbox("Experience Level",
                    ["Beginner", "Intermediate", "Advanced"])
            
            with col2:
                duration = st.slider("Session Duration (minutes)", 10, 90, 30)
                intensity = st.select_slider("Intensity", 
                    options=["Gentle", "Moderate", "Vigorous"])
            
            if st.form_submit_button("🧘 Generate Yoga Routine"):
                with st.spinner("Creating your zen session..."):
                    prompt = f"""Create a {duration}-minute {yoga_goal} yoga routine for {experience_level} 
                        practitioners with {intensity} intensity. Include warm-up, sequence of poses, 
                        and final relaxation with attractive emogies and attractive format"""
                    response = get_groq_response(prompt)
                    st.subheader("🌸 Your Yoga Sequence")
                    # st.markdown(f'<div class="recommendation-box">\n\n{response}</div>', unsafe_allow_html=True)
                    think_match = re.search(r'<think>(.*?)</think>', response, re.DOTALL)
                    if think_match:
                        think_text = think_match.group(1).strip()
                        # Remove the <think>...</think> part from the original response
                        response_without_think = re.sub(r'<think>.*?</think>', '', response, flags=re.DOTALL).strip()
                    else:
                        think_text = ""
                        response_without_think = response

                    # Print the think part using a Streamlit expander if it exists
                    if think_text:
                        with st.expander("Internal Thoughts"):
                            st.write(think_text)

                    # Print the remaining response using st.markdown
                    st.markdown(f'<div class="recommendation-box">\n\n{response_without_think}</div>', unsafe_allow_html=True)


    # AI Coach
    elif page == "πŸ€– AI Coach":
        st.markdown('<p class="header-text">AI Wellness Coach</p>', unsafe_allow_html=True)
        st.header("πŸ€– AI Wellness Coach")
        st.divider()
        
        if "messages" not in st.session_state:
            st.session_state.messages = []
        
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])
        
        if prompt := st.chat_input("Ask about health, nutrition, or fitness..."):
            st.session_state.messages.append({"role": "user", "content": prompt})
            with st.chat_message("user"):
                st.markdown(prompt)
            
            with st.chat_message("assistant"):
                with st.spinner("Analyzing..."):
                    response = get_groq_response(prompt)
                    # st.session_state.messages.append({"role": "assistant", "content": response})
                    # Extract the internal "think" part from the response
                    think_match = re.search(r'<think>(.*?)</think>', response, re.DOTALL)
                    if think_match:
                        think_text = think_match.group(1).strip()
                        # Remove the <think>...</think> part from the response
                        response_without_think = re.sub(r'<think>.*?</think>', '', response, flags=re.DOTALL).strip()
                    else:
                        think_text = ""
                        response_without_think = response

                    # Display the internal thoughts (if any) inside an expander
                    if think_text:
                        with st.expander("Internal Thoughts"):
                            st.write(think_text)

                    # Display the remaining response in the recommendation box
                    st.markdown(f'<div class="recommendation-box">\n\n{response_without_think}</div>', unsafe_allow_html=True)

if __name__ == "__main__":
    main()