Skip to content

lit_sb3.huggingface.push_to_hub

source module lit_sb3.huggingface.push_to_hub

Push trained models to the HuggingFace Hub.

Integrates helpers from:

  • https://github.com/huggingface/huggingface_sb3/blob/main/huggingface_sb3/push_to_hub.py (_evaluate_agent, _generate_replay, _generate_metadata, push_to_hub)
  • https://github.com/DLR-RM/rl-baselines3-zoo/blob/master/rl_zoo3/push_to_hub.py (generate_model_card, save_model_card, package_to_hub)

All stable_baselines3 imports replaced with lit_sb3 equivalents.

Functions

  • generate_model_card — Generate a model card for the HuggingFace Hub (RL-Zoo3 style).

  • save_model_card — Write README.md (model card) to repo_dir.

  • package_to_hub — Evaluate, generate a video and upload a model to the HuggingFace Hub.

  • push_to_hub — Upload a single file to the HuggingFace Hub.

source generate_model_card(algo_name: str, algo_class_name: str, organization: str, env_id: str, mean_reward: float, std_reward: float, hyperparams: dict[str, Any], env_kwargs: dict[str, Any]) → tuple[str, Any]

Generate a model card for the HuggingFace Hub (RL-Zoo3 style).

Adapted from https://github.com/DLR-RM/rl-baselines3-zoo/blob/master/rl_zoo3/push_to_hub.py

Parameters

  • algo_name : str — Algorithm alias (e.g. "ppo").

  • algo_class_name : str — Algorithm class name (e.g. "PPO").

  • organization : str — HuggingFace organisation.

  • env_id : str — Gym environment ID.

  • mean_reward : float — Mean reward of the evaluated agent.

  • std_reward : float — Standard deviation of the mean reward.

  • hyperparams : dict[str, Any] — Hyperparameters used for training.

  • env_kwargs : dict[str, Any] — Additional env keyword arguments.

Returns

  • tuple[str, Any] — Tuple of (model_card_text, metadata).

source save_model_card(repo_dir: Path, generated_model_card: str, metadata: Any) → None

Write README.md (model card) to repo_dir.

Parameters

  • repo_dir : Path — Repository directory.

  • generated_model_card : str — Model card text.

  • metadata : Any — Metadata to embed in README front-matter.

Raises

  • ImportError — If huggingface_hub is not installed.

source package_to_hub(model: Any, model_name: ModelName, algo_name: str, algo_class_name: str, log_path: Path, hyperparams: dict[str, Any], env_kwargs: dict[str, Any], env_name: EnvironmentName, eval_env: Any, repo_id: Any, commit_message: str, is_deterministic: bool = True, n_eval_episodes: int = 10, token: str | None = None, local_repo_path: str = 'hub', video_length: int = 1000, generate_video: bool = False) → str

Evaluate, generate a video and upload a model to the HuggingFace Hub.

Adapted from https://github.com/DLR-RM/rl-baselines3-zoo/blob/master/rl_zoo3/push_to_hub.py

This function:

  1. Clones (or creates) the HuggingFace repository locally.
  2. Saves the model and optional VecNormalize stats.
  3. Copies training config / metric files.
  4. Evaluates the agent and writes results.json.
  5. Optionally records a replay.mp4.
  6. Generates and saves the model card.
  7. Uploads everything to the Hub.

Parameters

  • model : Any — Trained model.

  • model_name : ModelName — :class:~lit_sb3.huggingface.naming_schemes.ModelName.

  • algo_name : str — Algorithm alias (e.g. "ppo").

  • algo_class_name : str — Algorithm class name (e.g. "PPO").

  • log_path : Path — Path to the training output folder.

  • hyperparams : dict[str, Any] — Hyperparameters used for training.

  • env_kwargs : dict[str, Any] — Additional env keyword arguments.

  • env_name : EnvironmentName — Environment name.

  • eval_env : Any — Environment used to evaluate the agent.

  • repo_id : Any — HuggingFace repository id ("{org}/{name}").

  • commit_message : str — Commit message for the Hub upload.

  • is_deterministic : bool — Use deterministic actions.

  • n_eval_episodes : int — Number of evaluation episodes.

  • token : str | None — HuggingFace authentication token.

  • local_repo_path : str — Local base directory for the cloned repo.

  • video_length : int — Video length in timesteps.

  • generate_video : bool — Whether to record a replay video.

Returns

  • str — Repository URL.

Raises

  • ImportError — If huggingface_hub is not installed.

source push_to_hub(repo_id: str, filename: str, commit_message: str | None = None, token: str | None = None) → str

Upload a single file to the HuggingFace Hub.

Adapted from https://github.com/huggingface/huggingface_sb3/blob/main/huggingface_sb3/push_to_hub.py

Parameters

  • repo_id : str — Repository id ("{org}/{name}")).

  • filename : str — Local path to the file to upload.

  • commit_message : str | None — Commit message.

  • token : str | None — HuggingFace authentication token.

Returns

  • str — Repository URL.

Raises

  • ImportError — If huggingface_hub is not installed.