Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from services import wiki_service | |
| def app(): | |
| st.title("Workout Plans") | |
| # Workout plan generator | |
| st.subheader("Generate a Personalized Workout Plan") | |
| # User input form | |
| with st.form("workout_form"): | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| fitness_goal = st.selectbox( | |
| "What is your primary fitness goal?", | |
| ["Weight Loss", "Muscle Gain", "Endurance", "Strength", "Flexibility", "General Fitness"] | |
| ) | |
| experience_level = st.selectbox( | |
| "What is your experience level?", | |
| ["Beginner", "Intermediate", "Advanced"] | |
| ) | |
| st.selectbox( | |
| "What is your age range?", | |
| ["Under 18", "18-30", "31-45", "46-60", "Over 60"] | |
| ) | |
| with col2: | |
| days_per_week = st.slider("How many days per week can you workout?", 1, 7, 3) | |
| st.slider("How long can you workout each session (minutes)?", 15, 120, 45, 15) | |
| equipment = st.multiselect( | |
| "What equipment do you have access to?", | |
| ["None/Bodyweight", "Dumbbells", "Barbell", "Resistance Bands", "Cardio Machines", "Full Gym"] | |
| ) | |
| submitted = st.form_submit_button("Generate Workout Plan") | |
| if submitted: | |
| st.success("Your personalized workout plan has been generated!") | |
| # Map UI goals to API goals | |
| goal_mapping = { | |
| "Weight Loss": "weight loss", | |
| "Muscle Gain": "strength", | |
| "Endurance": "endurance", | |
| "Strength": "strength", | |
| "Flexibility": "flexibility", | |
| "General Fitness": "general" | |
| } | |
| # Get the API goal | |
| api_goal = goal_mapping.get(fitness_goal, "general") | |
| # Call the wiki service to get a real workout plan | |
| with st.spinner(f"Creating your {fitness_goal.lower()} workout plan..."): | |
| workout_plan = wiki_service.get_workout_plan_by_goal(api_goal) | |
| # Display the workout plan | |
| st.subheader(workout_plan["title"]) | |
| st.markdown(workout_plan["description"]) | |
| # Add tags for experience level and equipment | |
| st.markdown(f"**Experience Level:** {experience_level}") | |
| if equipment: | |
| st.markdown(f"**Equipment:** {', '.join(equipment)}") | |
| # Display the workout days | |
| if "days" in workout_plan and workout_plan["days"]: | |
| for day in workout_plan["days"]: | |
| # Only show up to the number of days per week the user selected | |
| day_num = int(day["day"].split(" ")[1]) if "Day " in day["day"] else 0 | |
| if day_num <= days_per_week: | |
| with st.expander(f"{day['day']} - {day['focus']}", expanded=day["day"] == "Day 1"): | |
| if "exercises" in day and day["exercises"]: | |
| for exercise in day["exercises"]: | |
| st.markdown(f"**{exercise['name']}**: {exercise['description']}") | |
| else: | |
| st.markdown("Rest day or active recovery") | |
| # Add source information | |
| st.caption(f"Source: {workout_plan.get('source', 'Custom plan')}") | |
| if "url" in workout_plan and workout_plan["url"]: | |
| st.markdown(f"[Learn more]({workout_plan['url']})") | |
| # Add disclaimer | |
| st.info("Always consult with a healthcare professional before starting any new exercise program.") | |
| # Featured workout plans from API | |
| st.subheader("Featured Workout Plans") | |
| # Create tabs for different goals | |
| tab1, tab2, tab3 = st.tabs(["Weight Loss", "Strength Building", "Endurance"]) | |
| # Use our API to get real workout plans | |
| with tab1: | |
| with st.spinner("Loading Weight Loss workout plan..."): | |
| weight_loss_plan = wiki_service.get_workout_plan_by_goal("weight loss") | |
| st.subheader(weight_loss_plan["title"]) | |
| st.markdown(weight_loss_plan["description"]) | |
| # Show a sample of exercises from this plan | |
| if "days" in weight_loss_plan and weight_loss_plan["days"] and len(weight_loss_plan["days"]) > 0: | |
| sample_day = weight_loss_plan["days"][0] | |
| st.markdown(f"**Sample: {sample_day['day']} - {sample_day['focus']}**") | |
| if "exercises" in sample_day and sample_day["exercises"]: | |
| for i, exercise in enumerate(sample_day["exercises"]): | |
| if i < 3: # Show just a few exercises as a preview | |
| st.markdown(f"- **{exercise['name']}**: {exercise['description']}") | |
| st.markdown("[See full plan](#workout_form)") | |
| with tab2: | |
| with st.spinner("Loading Strength workout plan..."): | |
| strength_plan = wiki_service.get_workout_plan_by_goal("strength") | |
| st.subheader(strength_plan["title"]) | |
| st.markdown(strength_plan["description"]) | |
| # Show a sample of exercises from this plan | |
| if "days" in strength_plan and strength_plan["days"] and len(strength_plan["days"]) > 0: | |
| sample_day = strength_plan["days"][0] | |
| st.markdown(f"**Sample: {sample_day['day']} - {sample_day['focus']}**") | |
| if "exercises" in sample_day and sample_day["exercises"]: | |
| for i, exercise in enumerate(sample_day["exercises"]): | |
| if i < 3: # Show just a few exercises as a preview | |
| st.markdown(f"- **{exercise['name']}**: {exercise['description']}") | |
| st.markdown("[See full plan](#workout_form)") | |
| with tab3: | |
| with st.spinner("Loading Endurance workout plan..."): | |
| endurance_plan = wiki_service.get_workout_plan_by_goal("endurance") | |
| st.subheader(endurance_plan["title"]) | |
| st.markdown(endurance_plan["description"]) | |
| # Show a sample of exercises from this plan | |
| if "days" in endurance_plan and endurance_plan["days"] and len(endurance_plan["days"]) > 0: | |
| sample_day = endurance_plan["days"][0] | |
| st.markdown(f"**Sample: {sample_day['day']} - {sample_day['focus']}**") | |
| if "exercises" in sample_day and sample_day["exercises"]: | |
| for i, exercise in enumerate(sample_day["exercises"]): | |
| if i < 3: # Show just a few exercises as a preview | |
| st.markdown(f"- **{exercise['name']}**: {exercise['description']}") | |
| st.markdown("[See full plan](#workout_form)") | |
| # Latest fitness research from API | |
| st.subheader("Latest Fitness Research") | |
| with st.spinner("Loading latest fitness research..."): | |
| research = wiki_service.get_latest_fitness_research() | |
| if "articles" in research and research["articles"]: | |
| for i, article in enumerate(research["articles"]): | |
| if i < 3: # Show only top 3 articles | |
| with st.expander(article["title"], expanded=i == 0): | |
| st.markdown(article["summary"]) | |
| if "last_updated" in article: | |
| st.caption(f"Last updated: {article['last_updated']}") | |
| if "url" in article: | |
| st.markdown(f"[Read more]({article['url']})") | |
| # Exercise comparison section | |
| st.subheader("Compare Exercise Types") | |
| col1, col2 = st.columns([3, 1]) | |
| with col1: | |
| exercise_type = st.selectbox("Select exercise category to compare:", | |
| ["Cardio", "Strength", "Flexibility", "Balance", "Yoga"]) | |
| with col2: | |
| if st.button("Compare"): | |
| with st.spinner(f"Analyzing {exercise_type} exercises..."): | |
| comparison = wiki_service.get_exercise_comparison(exercise_type.lower()) | |
| st.subheader(f"{exercise_type} Exercise Comparison") | |
| if "comparison" in comparison and comparison["comparison"]: | |
| # Create a clean table-like display | |
| has_benefits = any("benefits" in item for item in comparison["comparison"]) | |
| if has_benefits: | |
| for item in comparison["comparison"]: | |
| st.markdown(f"**{item['name']}**") | |
| st.markdown(item["summary"]) | |
| if "benefits" in item: | |
| benefits = ", ".join(item["benefits"]) | |
| st.markdown(f"**Benefits:** {benefits}") | |
| if "difficulty" in item: | |
| st.markdown(f"**Difficulty:** {item['difficulty']}") | |
| st.markdown("---") | |