Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +176 -0
- requirements.txt +28 -0
app.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from ai71 import AI71
|
5 |
+
import time
|
6 |
+
|
7 |
+
AI71_API_KEY = "api71-api-92fc2ef9-9f3c-47e5-a019-18e257b04af2"
|
8 |
+
ai71 = AI71(AI71_API_KEY)
|
9 |
+
|
10 |
+
def collect_user_data():
|
11 |
+
st.sidebar.header("Your Information")
|
12 |
+
age = st.sidebar.number_input("Enter your age:", min_value=10, max_value=100, value=25)
|
13 |
+
height = st.sidebar.number_input("Enter your height (in cm):", min_value=100, max_value=250, value=170)
|
14 |
+
weight = st.sidebar.number_input("Enter your weight (in kg):", min_value=30, max_value=200, value=70)
|
15 |
+
gender = st.sidebar.selectbox("Select your gender:", ["Male", "Female", "Other"])
|
16 |
+
activity_level = st.sidebar.selectbox("Select your activity level:", ["Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extra Active"])
|
17 |
+
goal = st.sidebar.selectbox("Select your fitness goal:", ["Weight Loss", "Muscle Gain", "Maintenance", "Improve Stamina"])
|
18 |
+
|
19 |
+
# --- Additional Features ---
|
20 |
+
st.sidebar.subheader("Dietary Preferences (Optional)")
|
21 |
+
vegetarian = st.sidebar.checkbox("Vegetarian")
|
22 |
+
vegan = st.sidebar.checkbox("Vegan")
|
23 |
+
allergies = st.sidebar.text_input("Allergies (comma-separated, e.g., peanuts, dairy):")
|
24 |
+
|
25 |
+
st.sidebar.subheader("Workout Preferences (Optional)")
|
26 |
+
workout_days = st.sidebar.slider("How many days a week do you want to work out?", 1, 7, 4)
|
27 |
+
workout_duration = st.sidebar.slider("Average workout duration (in minutes):", 30, 90, 45)
|
28 |
+
|
29 |
+
return {
|
30 |
+
"age": age, "height": height, "weight": weight, "gender": gender,
|
31 |
+
"activity_level": activity_level, "goal": goal,
|
32 |
+
"vegetarian": vegetarian, "vegan": vegan, "allergies": allergies,
|
33 |
+
"workout_days": workout_days, "workout_duration": workout_duration
|
34 |
+
}
|
35 |
+
|
36 |
+
def calculate_bmi(height, weight):
|
37 |
+
bmi = weight / ((height / 100) ** 2)
|
38 |
+
return bmi
|
39 |
+
|
40 |
+
def calculate_bmr(gender, weight, height, age):
|
41 |
+
if gender == "Male":
|
42 |
+
bmr = (10 * weight) + (6.25 * height) - (5 * age) + 5
|
43 |
+
elif gender == "Female":
|
44 |
+
bmr = (10 * weight) + (6.25 * height) - (5 * age) - 161
|
45 |
+
else:
|
46 |
+
bmr = (10 * weight) + (6.25 * height) - (5 * age) # Using male formula as default
|
47 |
+
return bmr
|
48 |
+
|
49 |
+
def calculate_daily_calorie_needs(bmr, activity_level, goal):
|
50 |
+
activity_multipliers = {
|
51 |
+
"Sedentary": 1.2,
|
52 |
+
"Lightly Active": 1.375,
|
53 |
+
"Moderately Active": 1.55,
|
54 |
+
"Very Active": 1.725,
|
55 |
+
"Extra Active": 1.9
|
56 |
+
}
|
57 |
+
calorie_needs = bmr * activity_multipliers[activity_level]
|
58 |
+
if goal == "Weight Loss":
|
59 |
+
calorie_needs -= 500
|
60 |
+
elif goal == "Muscle Gain":
|
61 |
+
calorie_needs += 350 # Average between 250-500
|
62 |
+
return int(calorie_needs)
|
63 |
+
|
64 |
+
def generate_meal_plan(calorie_goal, restrictions=""):
|
65 |
+
prompt = f"""
|
66 |
+
Generate a sample 7-day meal plan for a person with a daily calorie goal of {calorie_goal} calories.
|
67 |
+
The meal plan should include breakfast, lunch, dinner, and two snacks per day.
|
68 |
+
|
69 |
+
Dietary restrictions: {restrictions}
|
70 |
+
|
71 |
+
Provide the meal plan in a clear and organized format, using bullet points for each day.
|
72 |
+
"""
|
73 |
+
response = ai71.chat.completions.create(
|
74 |
+
model="tiiuae/falcon-180b-chat",
|
75 |
+
messages=[{"role": "user", "content": prompt}],
|
76 |
+
stream=False,
|
77 |
+
)
|
78 |
+
return response.choices[0].message.content
|
79 |
+
|
80 |
+
def generate_workout_plan(goal, days, duration, styles=""):
|
81 |
+
prompt = f"""
|
82 |
+
Create a sample weekly workout plan for someone aiming to {goal},
|
83 |
+
who wants to work out {days} days a week, with each workout lasting
|
84 |
+
around {duration} minutes.
|
85 |
+
|
86 |
+
Preferred workout styles: {styles}
|
87 |
+
|
88 |
+
Include a variety of exercises targeting different muscle groups.
|
89 |
+
Provide clear instructions for each exercise and day.
|
90 |
+
"""
|
91 |
+
response = ai71.chat.completions.create(
|
92 |
+
model="tiiuae/falcon-180b-chat",
|
93 |
+
messages=[{"role": "user", "content": prompt}],
|
94 |
+
stream=False,
|
95 |
+
)
|
96 |
+
return response.choices[0].message.content
|
97 |
+
|
98 |
+
def get_ai_response(prompt, chat_history):
|
99 |
+
messages = [{"role": "system", "content": "You are a helpful and knowledgeable fitness assistant."}]
|
100 |
+
for turn in chat_history:
|
101 |
+
messages.append({"role": "user", "content": turn[0]})
|
102 |
+
messages.append({"role": "assistant", "content": turn[1]})
|
103 |
+
messages.append({"role": "user", "content": prompt})
|
104 |
+
|
105 |
+
try:
|
106 |
+
completion = ai71.chat.completions.create(
|
107 |
+
model="tiiuae/falcon-180b-chat",
|
108 |
+
messages=messages,
|
109 |
+
stream=False,
|
110 |
+
)
|
111 |
+
return completion.choices[0].message.content
|
112 |
+
except Exception as e:
|
113 |
+
return f"An error occurred: {str(e)}"
|
114 |
+
|
115 |
+
# Streamlit App
|
116 |
+
st.title("FitGen - Your Personalized Fitness Guide")
|
117 |
+
|
118 |
+
# Initialize chat history
|
119 |
+
if 'chat_history' not in st.session_state:
|
120 |
+
st.session_state.chat_history = []
|
121 |
+
|
122 |
+
# Sidebar
|
123 |
+
user_data = collect_user_data()
|
124 |
+
if st.sidebar.button("Generate Plan"):
|
125 |
+
st.session_state.user_data = user_data
|
126 |
+
|
127 |
+
# Main Content Area
|
128 |
+
user_data = st.session_state.get("user_data", {})
|
129 |
+
if user_data:
|
130 |
+
bmi = calculate_bmi(user_data["height"], user_data["weight"])
|
131 |
+
bmr = calculate_bmr(user_data["gender"], user_data["weight"], user_data["height"], user_data["age"])
|
132 |
+
daily_calories = calculate_daily_calorie_needs(bmr, user_data["activity_level"], user_data["goal"])
|
133 |
+
|
134 |
+
st.header("Your Fitness Plan")
|
135 |
+
st.write(f"**BMI:** {bmi:.2f}")
|
136 |
+
st.write(f"**BMR:** {bmr:.2f} calories/day")
|
137 |
+
st.write(f"**Daily Calorie Needs:** {daily_calories} calories/day")
|
138 |
+
|
139 |
+
dietary_restrictions = ""
|
140 |
+
if user_data["vegetarian"]:
|
141 |
+
dietary_restrictions += "Vegetarian. "
|
142 |
+
if user_data["vegan"]:
|
143 |
+
dietary_restrictions += "Vegan. "
|
144 |
+
if user_data["allergies"]:
|
145 |
+
dietary_restrictions += f"Allergies: {user_data['allergies']}. "
|
146 |
+
|
147 |
+
st.subheader("Your Meal Plan")
|
148 |
+
with st.spinner("Generating meal plan..."):
|
149 |
+
time.sleep(1) # Simulate delay - remove in production
|
150 |
+
meal_plan = generate_meal_plan(daily_calories, dietary_restrictions)
|
151 |
+
st.write(meal_plan)
|
152 |
+
|
153 |
+
st.subheader("Your Workout Plan")
|
154 |
+
with st.spinner("Generating workout plan..."):
|
155 |
+
time.sleep(1) # Simulate delay - remove in production
|
156 |
+
workout_plan = generate_workout_plan(user_data["goal"], user_data["workout_days"], user_data["workout_duration"])
|
157 |
+
st.write(workout_plan)
|
158 |
+
|
159 |
+
st.subheader("Ask Your Fitness AI")
|
160 |
+
user_question = st.text_input("Enter your fitness-related question:")
|
161 |
+
if user_question:
|
162 |
+
with st.spinner("Generating response..."):
|
163 |
+
response = get_ai_response(user_question, st.session_state.chat_history)
|
164 |
+
st.session_state.chat_history.append((user_question, response)) # Update history
|
165 |
+
st.write(response)
|
166 |
+
|
167 |
+
# Display chat history
|
168 |
+
st.subheader("Chat History")
|
169 |
+
for i, (user_msg, bot_msg) in enumerate(st.session_state.chat_history):
|
170 |
+
st.write(f"**You:** {user_msg}")
|
171 |
+
st.write(f"**Bot:** {bot_msg}")
|
172 |
+
if i < len(st.session_state.chat_history) - 1:
|
173 |
+
st.markdown("---")
|
174 |
+
|
175 |
+
else:
|
176 |
+
st.write("Please fill in your information in the sidebar to generate your personalized fitness plan.")
|
requirements.txt
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
plotly
|
4 |
+
requests
|
5 |
+
PyPDF2
|
6 |
+
python-docx
|
7 |
+
matplotlib
|
8 |
+
beautifulsoup4
|
9 |
+
wikipedia-api
|
10 |
+
google-api-python-client
|
11 |
+
httpx
|
12 |
+
langchain
|
13 |
+
langchain-huggingface
|
14 |
+
langchain_community
|
15 |
+
sentence-transformers
|
16 |
+
streamlit-lottie
|
17 |
+
wikipedia
|
18 |
+
faiss-gpu
|
19 |
+
faiss-cpu
|
20 |
+
selenium
|
21 |
+
webdriver_manager
|
22 |
+
scikit-learn
|
23 |
+
networkx
|
24 |
+
tabula-py
|
25 |
+
spacy==3.7.2
|
26 |
+
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
|
27 |
+
pydantic>=1.8.2,<3.0.0
|
28 |
+
ai71==0.0.18
|