|
|
""" |
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
if creds_json := os.getenv("GOOGLE_CREDENTIALS_JSON"): |
|
|
try: |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
os.environ.setdefault("DATABASE_TYPE", "firestore") |
|
|
os.environ.setdefault("ENABLE_AUTH", "true") |
|
|
os.environ.setdefault("ENVIRONMENT", "production") |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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...") |
|
|
|
|
|
|
|
|
config = get_app_config() |
|
|
|
|
|
|
|
|
setup_telemetry() |
|
|
|
|
|
|
|
|
if config.enable_auth: |
|
|
print("π Multi-user authentication enabled") |
|
|
setup_authentication(config) |
|
|
else: |
|
|
print("β οΈ Running in single-user mode") |
|
|
|
|
|
|
|
|
model_spec = os.getenv("CHAT_AGENT_MODEL", "hf:meta-llama/Llama-3.2-3B-Instruct") |
|
|
print(f"π€ Using model: {model_spec}") |
|
|
|
|
|
|
|
|
setup_agent(model_spec) |
|
|
|
|
|
|
|
|
demo = create_interface() |
|
|
|
|
|
print("β
Application initialized successfully") |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|