import os from huggingface_hub import HfApi from dotenv import dotenv_values import argparse def main(): parser = argparse.ArgumentParser(description="Upload .env secrets to Hugging Face Space") parser.add_argument("--repo-id", default="fongci/slack_url_bot", help="Hugging Face Space ID (username/space-name)") parser.add_argument("--env-file", default=".env", help="Path to the .env file") args = parser.parse_args() if not os.path.exists(args.env_file): print(f"Error: {args.env_file} not found.") return # Read .env file config = dotenv_values(args.env_file) # Use explicit token if provided via environment variable token = os.getenv("HF_TOKEN") api = HfApi(token=token) # If no token, we rely on ambient authentication (huggingface-cli login) print(f"Updating secrets for {args.repo_id}...") for key, value in config.items(): if not value: print(f"Skipping empty value for: {key}") continue try: api.add_space_secret(repo_id=args.repo_id, key=key, value=value) print(f"✅ Added secret: {key}") except Exception as e: print(f"❌ Failed to add secret {key}: {e}") if __name__ == "__main__": main()