""" HuggingFace Spaces entry point for Garmin Agents web application. This file serves as the deployment template for HuggingFace Spaces. During deployment, it should be copied to the repository root as app.py. HuggingFace Spaces expects: - app.py at repository root - requirements.txt at repository root - All application code accessible from app.py """ import json import os import tempfile # Handle Google Cloud credentials from HF Spaces secret # HF Spaces stores service account JSON as string in GOOGLE_CREDENTIALS_JSON secret if creds_json := os.getenv("GOOGLE_CREDENTIALS_JSON"): try: # Parse JSON string and write to temporary file credentials_data = json.loads(creds_json) with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".json") as f: json.dump(credentials_data, f) os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = f.name print("✓ Google Cloud credentials configured from HF Spaces secret") except json.JSONDecodeError as e: print(f"⚠️ Failed to parse GOOGLE_CREDENTIALS_JSON: {e}") print("Please ensure the secret contains valid JSON") # Set production environment variables os.environ.setdefault("DATABASE_TYPE", "firestore") os.environ.setdefault("ENABLE_AUTH", "true") os.environ.setdefault("ENVIRONMENT", "production") # Verify required environment variables required_vars = { "GOOGLE_CLOUD_PROJECT": "GCP project ID for Firestore", "CHAT_AGENT_MODEL": "AI model specification (e.g., hf:meta-llama/Llama-3.2-3B-Instruct)", } missing_vars = [] for var_name, var_description in required_vars.items(): if not os.getenv(var_name): missing_vars.append(f" - {var_name}: {var_description}") if missing_vars: error_msg = "❌ Missing required environment variables:\n" + "\n".join(missing_vars) print(error_msg) print("\nPlease configure these in HuggingFace Spaces Settings → Variables") raise RuntimeError(error_msg) # Import and launch the web application try: from garmin_web.app import create_interface, setup_agent, setup_authentication, setup_telemetry from garmin_web.config.app_config import get_app_config print("🏃‍♂️ Starting Garmin AI Coach on HuggingFace Spaces...") # Get configuration config = get_app_config() # Setup telemetry setup_telemetry() # Setup authentication if enabled if config.enable_auth: print("🔐 Multi-user authentication enabled") setup_authentication(config) else: print("⚠️ Running in single-user mode") # Get model from environment model_spec = os.getenv("CHAT_AGENT_MODEL", "hf:meta-llama/Llama-3.2-3B-Instruct") print(f"🤖 Using model: {model_spec}") # Initialize agent setup_agent(model_spec) # Create Gradio interface demo = create_interface() print("✅ Application initialized successfully") # Launch the interface # HuggingFace Spaces automatically handles the server configuration if __name__ == "__main__": demo.launch() except ImportError as e: error_msg = f"❌ Failed to import application modules: {e}" print(error_msg) print("Ensure all workspace packages are included in deployment") raise except Exception as e: error_msg = f"❌ Failed to initialize application: {e}" print(error_msg) raise