Spaces:
No application file
No application file
Download app.py from macken39/Kid-Planner-2: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/spaces/macken39/Kid-Planner-2/resolve/main/app.py
- Command line
-
hf download hf://spaces/macken39/Kid-Planner-2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/macken39/Kid-Planner-2/resolve/main/app.py
12.3 kB
| import streamlit as st | |
| import json | |
| import pandas as pd | |
| from datetime import datetime | |
| import re | |
| # Import OpenAI with error handling | |
| try: | |
| from openai import OpenAI | |
| OPENAI_AVAILABLE = True | |
| except ImportError: | |
| st.error("OpenAI library not installed. Please install it with: pip install openai") | |
| OPENAI_AVAILABLE = False | |
| # Page config | |
| st.set_page_config( | |
| page_title="Summer Activity Planner", | |
| page_icon="ποΈ", | |
| layout="wide" | |
| ) | |
| # Initialize session state | |
| if 'activities' not in st.session_state: | |
| st.session_state.activities = [] | |
| if 'search_performed' not in st.session_state: | |
| st.session_state.search_performed = False | |
| def get_activity_recommendations(zip_code, days_of_week, personality_traits, age, budget, activity_type, openai_api_key): | |
| """ | |
| Use OpenAI API to get summer activity recommendations | |
| """ | |
| if not OPENAI_AVAILABLE: | |
| st.error("OpenAI library is not available. Please install it first.") | |
| return [] | |
| try: | |
| client = OpenAI(api_key=openai_api_key) | |
| # Format the personality traits and days | |
| traits_str = ", ".join(personality_traits) | |
| days_str = ", ".join(days_of_week) | |
| # Budget mapping | |
| budget_mapping = { | |
| "Free": "free or very low cost (under $25)", | |
| "Low": "budget-friendly ($25-100)", | |
| "Moderate": "moderate cost ($100-300)", | |
| "High": "higher-end ($300+)" | |
| } | |
| budget_desc = budget_mapping.get(budget, "any budget") | |
| prompt = f""" | |
| Find summer activities, camps, and programs in or near zip code {zip_code} for a {age}-year-old child. | |
| Requirements: | |
| - Days available: {days_str} | |
| - Child's personality: {traits_str} | |
| - Budget range: {budget_desc} | |
| - Activity type: {activity_type} | |
| - Focus on summer 2025 activities | |
| Please provide 8-12 diverse recommendations. For each activity, include: | |
| 1. Activity name | |
| 2. Brief description (2-3 sentences) | |
| 3. Location/venue name | |
| 4. Estimated cost or cost range | |
| 5. Age appropriateness | |
| 6. Whether it's time-bound (specific dates/sessions) or anytime (ongoing/flexible) | |
| 7. If time-bound, include typical session dates/times | |
| 8. Duration (half-day, full-day, week-long, etc.) | |
| 9. Why it matches the child's personality | |
| Format your response as a JSON array with each activity as an object with these keys: | |
| name, description, location, cost, age_range, time_type, dates_times, duration, personality_match | |
| Make sure to include a mix of camps, classes, sports, arts, outdoor activities, and educational programs. | |
| """ | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant that specializes in finding local summer activities for children. Provide detailed, accurate information in the requested JSON format."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0.7, | |
| max_tokens=3000 | |
| ) | |
| # Extract JSON from response | |
| content = response.choices[0].message.content | |
| # Try to extract JSON from the response | |
| json_match = re.search(r'\[.*\]', content, re.DOTALL) | |
| if json_match: | |
| activities_json = json_match.group() | |
| activities = json.loads(activities_json) | |
| return activities | |
| else: | |
| # If no JSON found, try to parse the entire content | |
| return json.loads(content) | |
| except Exception as e: | |
| st.error(f"Error getting recommendations: {str(e)}") | |
| return [] | |
| except Exception as e: | |
| st.error(f"Error with OpenAI setup: {str(e)}") | |
| return [] | |
| def filter_activities(activities, cost_filter, time_filter, location_filter): | |
| """ | |
| Filter activities based on user selections | |
| """ | |
| filtered = activities.copy() | |
| if cost_filter != "All": | |
| cost_keywords = { | |
| "Free": ["free", "$0", "no cost"], | |
| "Low": ["$25", "$50", "$75", "$100"], | |
| "Moderate": ["$100", "$200", "$250", "$300"], | |
| "High": ["$300", "$400", "$500", "$600", "$700", "$800", "$900", "$1000"] | |
| } | |
| if cost_filter in cost_keywords: | |
| filtered = [a for a in filtered if any(keyword.lower() in a.get('cost', '').lower() for keyword in cost_keywords[cost_filter]) or cost_filter.lower() in a.get('cost', '').lower()] | |
| if time_filter != "All": | |
| filtered = [a for a in filtered if a.get('time_type', '').lower() == time_filter.lower()] | |
| if location_filter: | |
| filtered = [a for a in filtered if location_filter.lower() in a.get('location', '').lower()] | |
| return filtered | |
| def display_activity_card(activity, index): | |
| """ | |
| Display an individual activity card | |
| """ | |
| with st.container(): | |
| st.markdown(f"### {activity.get('name', 'Unnamed Activity')}") | |
| col1, col2 = st.columns([2, 1]) | |
| with col1: | |
| st.write(f"**Description:** {activity.get('description', 'No description available')}") | |
| st.write(f"**Location:** {activity.get('location', 'Location not specified')}") | |
| st.write(f"**Age Range:** {activity.get('age_range', 'Not specified')}") | |
| st.write(f"**Duration:** {activity.get('duration', 'Not specified')}") | |
| # Display dates/times based on time type | |
| time_type = activity.get('time_type', '').lower() | |
| if 'time-bound' in time_type or 'timebound' in time_type: | |
| st.write(f"**π Session Dates/Times:** {activity.get('dates_times', 'Dates TBD')}") | |
| else: | |
| st.write(f"**π Availability:** Anytime/Ongoing - {activity.get('dates_times', 'Flexible scheduling')}") | |
| with col2: | |
| st.write(f"**π° Cost:** {activity.get('cost', 'Cost not specified')}") | |
| st.write(f"**β° Type:** {activity.get('time_type', 'Not specified')}") | |
| # Personality match | |
| if activity.get('personality_match'): | |
| st.write(f"**π― Why it's a good fit:** {activity.get('personality_match')}") | |
| st.markdown("---") | |
| # Main app | |
| def main(): | |
| st.title("ποΈ Summer Activity Planner for Kids") | |
| st.markdown("Find the perfect summer activities, camps, and programs for your child!") | |
| # Get OpenAI API key from Hugging Face secrets | |
| try: | |
| openai_api_key = st.secrets["OPENAI_API_KEY"] | |
| st.success("β OpenAI API key found successfully!") | |
| except Exception as e: | |
| st.error("β οΈ Unable to access secrets from Hugging Face.") | |
| st.write(f"**Error:** {str(e)}") | |
| st.markdown(""" | |
| **This appears to be a Hugging Face Spaces configuration issue.** | |
| **Temporary Solution:** | |
| Since Hugging Face secrets aren't working, please enter your OpenAI API key below: | |
| """) | |
| openai_api_key = st.text_input( | |
| "OpenAI API Key", | |
| type="password", | |
| help="Enter your OpenAI API key temporarily while we troubleshoot the Hugging Face secrets issue" | |
| ) | |
| if not openai_api_key: | |
| st.error("Please enter your OpenAI API key to continue.") | |
| st.markdown(""" | |
| **To fix the Hugging Face secrets issue:** | |
| 1. Make sure you're using a **Streamlit SDK** (not Gradio or other) | |
| 2. Verify the secret is saved in Space Settings β Variables and secrets | |
| 3. Try restarting the Space | |
| 4. Contact Hugging Face support if the issue persists | |
| """) | |
| st.stop() | |
| # Sidebar for inputs | |
| with st.sidebar: | |
| st.header("Tell us about your needs:") | |
| # Zip code | |
| zip_code = st.text_input("Zip Code", placeholder="12345") | |
| # Age | |
| age = st.number_input("Child's Age", min_value=3, max_value=18, value=8) | |
| # Days of week | |
| st.subheader("Available Days") | |
| days_options = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
| days_of_week = st.multiselect("Select available days:", days_options, default=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]) | |
| # Personality traits | |
| st.subheader("Child's Personality") | |
| personality_options = ["Outdoorsy", "Creative", "Athletic", "Quiet/Introverted", "Social", "Academic", "Artistic", "Adventurous"] | |
| personality_traits = st.multiselect("Select personality traits:", personality_options) | |
| # Budget | |
| budget = st.selectbox("Budget Range", ["Free", "Low", "Moderate", "High"]) | |
| # Activity type | |
| activity_type = st.selectbox("Activity Type", ["Kid Only", "Family Activities", "Both"]) | |
| # Search button | |
| search_button = st.button("π Find Activities", type="primary") | |
| # Main content area | |
| if search_button: | |
| if not zip_code: | |
| st.error("Please enter your zip code.") | |
| elif not days_of_week: | |
| st.error("Please select at least one available day.") | |
| elif not personality_traits: | |
| st.error("Please select at least one personality trait.") | |
| else: | |
| with st.spinner("Finding the perfect activities for your child..."): | |
| activities = get_activity_recommendations( | |
| zip_code, days_of_week, personality_traits, age, budget, activity_type, openai_api_key | |
| ) | |
| if activities: | |
| st.session_state.activities = activities | |
| st.session_state.search_performed = True | |
| st.success(f"Found {len(activities)} activities!") | |
| else: | |
| st.error("No activities found. Please try adjusting your search criteria.") | |
| # Display results if search has been performed | |
| if st.session_state.search_performed and st.session_state.activities: | |
| st.header("π― Recommended Activities") | |
| # Filtering options | |
| with st.expander("π Filter Results", expanded=False): | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| cost_filter = st.selectbox("Filter by Cost", ["All", "Free", "Low", "Moderate", "High"]) | |
| with col2: | |
| time_filter = st.selectbox("Filter by Time Type", ["All", "Time-bound", "Anytime"]) | |
| with col3: | |
| location_filter = st.text_input("Filter by Location (keyword)", placeholder="e.g., YMCA, Park") | |
| # Apply filters | |
| filtered_activities = filter_activities(st.session_state.activities, cost_filter, time_filter, location_filter) | |
| if filtered_activities: | |
| st.write(f"Showing {len(filtered_activities)} activities:") | |
| # Display activities | |
| for i, activity in enumerate(filtered_activities): | |
| display_activity_card(activity, i) | |
| else: | |
| st.warning("No activities match your filter criteria. Try adjusting the filters.") | |
| elif not st.session_state.search_performed: | |
| # Welcome message | |
| st.markdown(""" | |
| ## Welcome to the Summer Activity Planner! | |
| This tool helps you find perfect summer activities for your child by: | |
| - πΊοΈ **Location-based search** using your zip code | |
| - π **Personality matching** to find activities your child will love | |
| - π° **Budget filtering** to stay within your price range | |
| - π **Schedule compatibility** with your available days | |
| - β° **Time flexibility** showing both scheduled camps and anytime activities | |
| **Get started by filling out the form in the sidebar and clicking "Find Activities"!** | |
| """) | |
| if __name__ == "__main__": | |
| main() |