Spaces:
Sleeping
Sleeping
import streamlit as st | |
import anthropic | |
import os | |
# Function to get a meal and exercise plan from Claude AI | |
def get_nutrition_and_exercise_plan(name, age, gender, height, weight, medical_conditions, medications, | |
dietary_restrictions, health_goals, meal_plan_preferences, meal_frequency, | |
cuisine_preferences): | |
# Directly use the API key | |
api_key = os.getenv('anthropy') | |
client = anthropic.Client(api_key=api_key) | |
prompt = f"""{anthropic.HUMAN_PROMPT} | |
You are a world-class nutritionist and fitness expert. Based on the following details, generate a personalized meal plan and exercise plan: | |
**Personal Information** | |
- Name: {name} | |
- Age: {age} | |
- Gender: {gender} | |
- Height: {height} cm | |
- Weight: {weight} kg | |
**Health and Medical Information** | |
- Medical Conditions: {medical_conditions} | |
- Medications: {medications} | |
- Dietary Restrictions: {dietary_restrictions} | |
- Health Goals: {health_goals} | |
**Personalized Settings** | |
- Meal Plan Preferences: {meal_plan_preferences} | |
- Meal Frequency: {meal_frequency} meals/day | |
- Cuisine Preferences: {', '.join(cuisine_preferences)} | |
**Instructions** | |
- Provide a detailed meal plan including breakfast, lunch, dinner, and snack options for one day. | |
- Ensure the meals are balanced and aligned with the health goals and dietary restrictions. | |
- Provide a detailed exercise plan including various exercises, their duration, and instructions suitable for the user's health goals and medical conditions. | |
- Present the information in clear and organized markdown format with appropriate headings and bullet points. | |
{anthropic.AI_PROMPT}""" | |
response = client.completions.create( | |
prompt=prompt, | |
model="claude-2", | |
max_tokens_to_sample=1000, | |
temperature=0.7, | |
) | |
return response.completion | |
# Streamlit app layout | |
st.set_page_config(page_title="Nutritionist Iftikhar Ahmad", page_icon="π", layout="wide") | |
# Header with custom background color | |
st.markdown( | |
""" | |
<style> | |
.main-header { | |
background-color: #03bafc; | |
padding: 20px; | |
color: white; | |
text-align: center; | |
border-radius: 5px; | |
} | |
.sidebar .sidebar-content { | |
background-color: #f0f2f6; | |
padding: 10px; | |
border-radius: 10px; | |
} | |
.stButton>button { | |
background-color: #03bafc; | |
color: white; | |
padding: 10px; | |
border-radius: 5px; | |
} | |
</style> | |
""", unsafe_allow_html=True | |
) | |
# Main title with a logo | |
st.markdown('<div class="main-header"><h1>π Welcome! This is Nutritionist Iftikhar Ahmad</h1></div>', unsafe_allow_html=True) | |
st.markdown("### Your Personalized Diet and Exercise Plan") | |
# Sidebar for user input | |
with st.sidebar: | |
st.header("Personal Information") | |
name = st.text_input("Name") | |
age = st.number_input("Age", min_value=0, step=1) | |
gender = st.selectbox("Gender", ["Male", "Female", "Other"]) | |
height = st.number_input("Height (cm)", min_value=0, step=1) | |
weight = st.number_input("Weight (kg)", min_value=0.0, step=0.1) | |
st.header("Health and Medical Information") | |
medical_conditions = st.selectbox("Medical Conditions", ["Diabetes", "Hypertension", "Fit"]) | |
dietary_restrictions = st.selectbox("Dietary Restrictions", ["Vegetarian", "Vegan", "Gluten-Free"]) | |
health_goals = st.selectbox("Health Goals", ["Weight Loss", "Muscle Gain"]) | |
medications = st.text_area("Medications and Supplements") | |
st.header("Personalized Settings") | |
meal_plan_preferences = st.selectbox("Meal Plan Preferences", ["Low Carb", "High Protein", "Balanced", "Mediterranean", "Keto", "Paleo"]) | |
meal_frequency = st.slider("Meal Frequency (meals per day)", 1, 6, 3) | |
cuisine_preferences = st.multiselect("Cuisine Preferences", ["Asian", "Mediterranean", "Italian", "Mexican", "Indian", "Middle Eastern", "American", "Other"]) | |
# Main section to display results | |
if st.button("Generate Personalized Plan"): | |
if not all([name, age, gender, height, weight, health_goals]): | |
st.error("Please fill out all required fields in the sidebar.") | |
else: | |
with st.spinner("Generating your personalized plan..."): | |
try: | |
plan_response = get_nutrition_and_exercise_plan( | |
name, age, gender, height, weight, medical_conditions, medications, | |
dietary_restrictions, health_goals, meal_plan_preferences, meal_frequency, cuisine_preferences | |
) | |
st.subheader("Your Personalized Meal and Exercise Plan") | |
st.markdown(plan_response) | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
# Footer with copyright and year | |
st.markdown("---") | |
st.markdown('<div style="text-align: center;">Β© 2024 Nutritionist Iftikhar Ahmad. All rights reserved.</div>', unsafe_allow_html=True) | |