transformers endpoints_compatible region:us
# Necessary installations for running in Google Colab
!apt install swig cmake
!pip install -r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt
!sudo apt-get update
!sudo apt-get install -y python3-opengl
!apt install ffmpeg
!apt install xvfb
!pip3 install pyvirtualdisplay

# Setup a virtual display for rendering environments in Colab
import os
from pyvirtualdisplay import Display

virtual_display = Display(visible=0, size=(1400, 900))
virtual_display.start()

# Import necessary libraries
import gymnasium as gym
from stable_baselines3 import PPO
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.evaluation import evaluate_policy
from huggingface_sb3 import package_to_hub, load_from_hub
from huggingface_hub import notebook_login

# Define the environment
env_id = "LunarLander-v2"
env = gym.make(env_id)

# SOLUTION: Parameters to accelerate the training of PPO model
model = PPO(
    policy='MlpPolicy',
    env=env,
    n_steps=1024,
    batch_size=64,
    n_epochs=4,
    gamma=0.999,
    gae_lambda=0.98,
    ent_coef=0.01,
    verbose=1
)

# Train the PPO agent
model.learn(total_timesteps=1000000)

# Save the model
model_name = "lunarLander1"
model.save(model_name)

# Evaluate the agent
eval_env = Monitor(gym.make(env_id))
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")

# Login to Hugging Face and configure git
notebook_login()
!git config --global credential.helper store

# Define the Hugging Face Hub repository details
repo_id = "FTU/lunarLanderPPO"
commit_message = "The first deep reinforced learning model"

# Assuming 'model' is your trained model object, package and push it to the Hugging Face Hub
package_to_hub(
    model=model,
    model_name=model_name,
    model_architecture="PPO",
    env_id=env_id,
    eval_env=eval_env,
    repo_id=repo_id,
    commit_message=commit_message
)
