CoolFace
Apppublic

skilfoy/Check-my-progress-Deep-RL-Course

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py286 linesDownload Raw Back to root
1import gradio as gr2from huggingface_hub import HfApi, hf_hub_download3from huggingface_hub.repocard import metadata_load4 5import pandas as pd6 7import requests8 9from utils import *10 11api = HfApi()12 13def get_user_models(hf_username, env_tag, lib_tag):14    """15    List the Reinforcement Learning models16    from user given environment and lib17    :param hf_username: User HF username18    :param env_tag: Environment tag19    :param lib_tag: Library tag20    """21    api = HfApi()22    models = api.list_models(author=hf_username, filter=["reinforcement-learning", env_tag, lib_tag])23 24    user_model_ids = [x.modelId for x in models]25    return user_model_ids26 27 28def get_user_sf_models(hf_username, env_tag, lib_tag):29    api = HfApi()30    models_sf = []31    models = api.list_models(author=hf_username, filter=["reinforcement-learning", lib_tag])32 33    user_model_ids = [x.modelId for x in models]34 35    for model in user_model_ids:36        meta = get_metadata(model)37        if meta is None:38            continue39        result = meta["model-index"][0]["results"][0]["dataset"]["name"]40        if result == env_tag:41            models_sf.append(model)42            43    return models_sf44 45 46def get_metadata(model_id):47  """48  Get model metadata (contains evaluation data)49  :param model_id50  """51  try:52    readme_path = hf_hub_download(model_id, filename="README.md")53    return metadata_load(readme_path)54  except requests.exceptions.HTTPError:55    # 404 README.md not found56    return None57 58 59def parse_metrics_accuracy(meta):60  """61  Get model results and parse it62  :param meta: model metadata63  """64  if "model-index" not in meta:65    return None66  result = meta["model-index"][0]["results"]67  metrics = result[0]["metrics"]68  accuracy = metrics[0]["value"]69  70  return accuracy71 72 73def parse_rewards(accuracy):74  """75  Parse mean_reward and std_reward76  :param accuracy: model results77  """78  default_std = -100079  default_reward= -100080  if accuracy !=  None:81      accuracy = str(accuracy)82      parsed =  accuracy.split(' +/- ')83      if len(parsed)>1:84          mean_reward = float(parsed[0])85          std_reward =  float(parsed[1])86      elif len(parsed)==1: #only mean reward   87          mean_reward = float(parsed[0])88          std_reward =  float(0)89      else: 90          mean_reward = float(default_std)91          std_reward = float(default_reward)92  else:93      mean_reward = float(default_std)94      std_reward = float(default_reward)95  96  return mean_reward, std_reward97 98def calculate_best_result(user_model_ids):99  """100  Calculate the best results of a unit101  best_result = mean_reward - std_reward102  :param user_model_ids: RL models of a user103  """104  best_result = -1000105  best_model_id = ""106  for model in user_model_ids:107    meta = get_metadata(model)108    if meta is None:109      continue110    accuracy = parse_metrics_accuracy(meta)111    mean_reward, std_reward = parse_rewards(accuracy)112    result = mean_reward - std_reward113    if result > best_result:114      best_result = result115      best_model_id = model116      117  return best_result, best_model_id118 119def check_if_passed(model):120  """121  Check if result >= baseline122  to know if you pass123  :param model: user model124  """125  if model["best_result"] >= model["min_result"]:126    model["passed_"] = True127 128def certification(hf_username):129  results_certification = [130      {131          "unit": "Unit 1",132          "env": "LunarLander-v2",133          "library": "stable-baselines3",134          "min_result": 200,135          "best_result": 0,136          "best_model_id": "",137          "passed_": False138      },139  {140          "unit": "Unit 2",141          "env": "Taxi-v3",142          "library": "q-learning",143          "min_result": 4,144          "best_result": 0,145          "best_model_id": "",146          "passed_": False147  },148  {149          "unit": "Unit 3",150          "env": "SpaceInvadersNoFrameskip-v4",151          "library": "stable-baselines3",152          "min_result": 200,153          "best_result": 0,154          "best_model_id": "",155          "passed_": False156  },157  {158          "unit": "Unit 4",159          "env": "CartPole-v1",160          "library": "reinforce",161          "min_result": 350,162          "best_result": 0,163          "best_model_id": "",164          "passed_": False165  },166    {167          "unit": "Unit 4",168          "env": "Pixelcopter-PLE-v0",169          "library": "reinforce",170          "min_result": 5,171          "best_result": 0,172          "best_model_id": "",173          "passed_": False174    },175      {176          "unit": "Unit 5",177          "env": "ML-Agents-SnowballTarget",178          "library": "ml-agents",179          "min_result": -100,180          "best_result": 0,181          "best_model_id": "",182          "passed_": False183    },184      {185          "unit": "Unit 5",186          "env": "ML-Agents-Pyramids",187          "library": "ml-agents",188          "min_result": -100,189          "best_result": 0,190          "best_model_id": "",191          "passed_": False192    },193      {194          "unit": "Unit 6",195          "env": "PandaReachDense",196          "library": "stable-baselines3",197          "min_result": -3.5,198          "best_result": 0,199          "best_model_id": "",200          "passed_": False201    },202      {203          "unit": "Unit 7",204          "env": "ML-Agents-SoccerTwos",205          "library": "ml-agents",206          "min_result": -100,207          "best_result": 0,208          "best_model_id": "",209          "passed_": False210    },211      {212          "unit": "Unit 8 PI",213          "env": "LunarLander-v2",214          "library": "deep-rl-course",215          "min_result": -500,216          "best_result": 0,217          "best_model_id": "",218          "passed_": False219    },220      {221          "unit": "Unit 8 PII",222          "env": "doom_health_gathering_supreme",223          "library": "sample-factory",224          "min_result": 5,225          "best_result": 0,226          "best_model_id": "",227          "passed_": False228    },229  ] 230    231  for unit in results_certification:232    if unit["unit"] == "Unit 6":233      # Since Unit 6 can use PandaReachDense-v2 or v3234      user_models = get_user_models(hf_username, "PandaReachDense-v3", unit["library"])235      if len(user_models) == 0:236        print("Empty")237        user_models = get_user_models(hf_username, "PandaReachDense-v2", unit["library"])238    elif unit["unit"] != "Unit 8 PII":239      # Get user model240      user_models = get_user_models(hf_username, unit['env'], unit['library'])241      # For sample factory vizdoom we don't have env tag for now242    else: 243      user_models = get_user_sf_models(hf_username, unit['env'], unit['library'])244    245    # Calculate the best result and get the best_model_id246    best_result, best_model_id = calculate_best_result(user_models)247 248    # Save best_result and best_model_id249    unit["best_result"] = best_result250    unit["best_model_id"] = make_clickable_model(best_model_id)251 252    # Based on best_result do we pass the unit?253    check_if_passed(unit)254    unit["passed"] = pass_emoji(unit["passed_"])255    256  print(results_certification)257 258  df = pd.DataFrame(results_certification)259  df = df[['passed', 'unit', 'env', 'min_result', 'best_result', 'best_model_id']]260  return df261 262 263with gr.Blocks() as demo:264    gr.Markdown(f"""265    # ๐Ÿ† Check your progress in the Deep Reinforcement Learning Course ๐Ÿ†266    You can check your progress here.267    268    - To get a certificate of completion, you must **pass 80% of the assignments**.269    - To get an honors certificate, you must **pass 100% of the assignments**.270 271    There's **no deadlines, the course is self-paced**.272 273    To pass an assignment your model result (mean_reward - std_reward) must be >= min_result274 275    **When min_result = -100 it means that you just need to push a model to pass this hands-on. No need to reach a certain result.**276    277    Just type your Hugging Face Username ๐Ÿค— (in my case skilfoy)278    """)279    280    hf_username = gr.Textbox(placeholder="skilfoy", label="Your Hugging Face Username")281    #email = gr.Textbox(placeholder="skilfoy@huggingface.co", label="Your Email (to receive your certificate)")282    check_progress_button = gr.Button(value="Check my progress")283    output = gr.components.Dataframe(value= certification(hf_username), headers=["Pass?", "Unit", "Environment", "Baseline", "Your best result", "Your best model id"], datatype=["markdown", "markdown", "markdown", "number", "number", "markdown", "bool"])284    check_progress_button.click(fn=certification, inputs=hf_username, outputs=output)285 286demo.launch()