File size: 6,848 Bytes
b787f43
 
 
 
 
 
 
bda8f89
b787f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bda8f89
b787f43
 
 
 
 
e7b5f59
b787f43
 
 
9a140a8
b787f43
 
 
 
1148fdd
 
 
a505e9c
1148fdd
 
 
b787f43
1148fdd
 
b787f43
1148fdd
 
 
b787f43
1148fdd
 
 
b787f43
1148fdd
b787f43
 
1148fdd
b787f43
1148fdd
 
 
 
 
 
b787f43
1148fdd
 
b787f43
1148fdd
b787f43
1148fdd
 
 
 
 
 
b787f43
 
1148fdd
b787f43
cb862a5
b787f43
1148fdd
cb862a5
1148fdd
 
 
 
 
 
b787f43
1148fdd
b787f43
1148fdd
 
b787f43
1148fdd
b787f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a140a8
b787f43
 
 
 
 
 
 
 
 
 
 
9a140a8
b787f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ae3fd9
f60b616
bda8f89
f60b616
 
0bae0f5
 
b787f43
0bae0f5
b787f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
from huggingface_hub import CommitOperationAdd, create_commit, RepoUrl
from huggingface_hub import EvalResult, ModelCard
from huggingface_hub.repocard_data import eval_results_to_model_index
import time
from pytablewriter import MarkdownTableWriter
import gradio as gr
from openllm import get_datas
import pandas as pd

BOT_HF_TOKEN = os.getenv('BOT_HF_TOKEN')

data = get_json_format_data()
finished_models = get_datas(data)
df = pd.DataFrame(finished_models)

desc = """
This is an automated PR created with https://huggingface.co/spaces/Weyaxi/open-llm-leaderboard-results-pr

The purpose of this PR is to add evaluation results from the Open LLM Leaderboard to your model card.

If you encounter any issues, please report them to https://huggingface.co/spaces/Weyaxi/open-llm-leaderboard-results-pr/discussions
"""

def search(df, value):
    result_df = df[df["fullname"] == value]
    return result_df.iloc[0].to_dict() if not result_df.empty else None


def get_details_url(repo):
   author, model = repo.split("/")
   return f"https://huggingface.co/datasets/open-llm-leaderboard/{author}__{model}-details"


def get_query_url(repo):
  return f"https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard?query={repo}"


def get_task_summary(results):
  return {
      "IFEval":
          {"dataset_type":"HuggingFaceH4/ifeval",
          "dataset_name":"IFEval (0-Shot)",
          "metric_type": "inst_level_strict_acc and prompt_level_strict_acc",
          "metric_value":results["IFEval"],
          "dataset_config": None, # don't know
          "dataset_split": None, # don't know
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 0},
          "metric_name":"strict accuracy"
          },
      "BBH":
          {"dataset_type":"BBH",
          "dataset_name":"BBH (3-Shot)",
          "metric_type":"acc_norm",
          "metric_value":results["BBH"],
          "dataset_config": None, # don't know
          "dataset_split": None, # don't know
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 3},
          "metric_name":"normalized accuracy"
          },
      "MATH Lvl 5":
      {
          "dataset_type":"hendrycks/competition_math",
          "dataset_name":"MATH Lvl 5 (4-Shot)",
          "metric_type":"exact_match",
          "metric_value":results["MATH Lvl 5"],
          "dataset_config": None, # don't know
          "dataset_split": None, # don't know
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 4},
          "metric_name":"exact match"
      },
      "GPQA":
      {
          "dataset_type":"Idavidrein/gpqa",
          "dataset_name":"GPQA (0-shot)",
          "metric_type":"acc_norm",
          "metric_value":results["GPQA"],
          "dataset_config": None, # don't know
          "dataset_split": None, # don't know
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 0},
          "metric_name":"acc_norm"
      },
      "MuSR":
      {
          "dataset_type":"TAUR-Lab/MuSR",
          "dataset_name":"MuSR (0-shot)",
          "metric_type":"acc_norm",
          "metric_value":results["MUSR"],
          "dataset_config": None, # don't know
          "dataset_split": None, # don't know
          "dataset_args":{"num_few_shot": 0},
          "metric_name":"acc_norm"
      },
      "MMLU-PRO":
      {
          "dataset_type":"TIGER-Lab/MMLU-Pro",
          "dataset_name":"MMLU-PRO (5-shot)",
          "metric_type":"acc",
          "metric_value":results["MMLU-PRO"],
          "dataset_config":"main",
          "dataset_split":"test",
          "dataset_args":{"num_few_shot": 5},
          "metric_name":"accuracy"
      }
  }



def get_eval_results(repo):
  results = search(df, repo)
  task_summary = get_task_summary(results)
  md_writer = MarkdownTableWriter()
  md_writer.headers = ["Metric", "Value"]
  md_writer.value_matrix = [["Avg.", results['Average ⬆️']]] + [[v["dataset_name"], v["metric_value"]] for v in task_summary.values()]

  text = f"""
# [Open LLM Leaderboard Evaluation Results](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard)
Detailed results can be found [here]({get_details_url(repo)})

{md_writer.dumps()}
"""
  return text


def get_edited_yaml_readme(repo, token: str | None):
  card = ModelCard.load(repo, token=token)
  results = search(df, repo)

  common = {"task_type": 'text-generation', "task_name": 'Text Generation', "source_name": "Open LLM Leaderboard", "source_url": f"https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard?query={repo}"}

  tasks_results = get_task_summary(results)

  if not card.data['eval_results']: # No results reported yet, we initialize the metadata
    card.data["model-index"] = eval_results_to_model_index(repo.split('/')[1], [EvalResult(**task, **common) for task in tasks_results.values()])
  else: # We add the new evaluations
    for task in tasks_results.values():
      cur_result = EvalResult(**task, **common)
      if any(result.is_equal_except_value(cur_result) for result in card.data['eval_results']):
        continue
      card.data['eval_results'].append(cur_result)

  return str(card)
    

def commit(repo, pr_number=None, message="Adding Evaluation Results", oauth_token: gr.OAuthToken | None = None): # specify pr number if you want to edit it, don't if you don't want
  global df
  finished_models = get_datas()
  df = pd.DataFrame(finished_models)
    
  if not oauth_token:
    raise gr.Warning("You are not logged in. Click on 'Sign in with Huggingface' to log in.")
  else:
    token = oauth_token

  if repo.startswith("https://huggingface.co/"):
      try:
        repo = RepoUrl(repo).repo_id
      except Exception:
        raise gr.Error(f"Not a valid repo id: {str(repo)}")
    
  edited = {"revision": f"refs/pr/{pr_number}"} if pr_number else {"create_pr": True}

  try:
    try: # check if there is a readme already
      readme_text = get_edited_yaml_readme(repo, token=token) + get_eval_results(repo)
    except Exception as e:
      if "Repo card metadata block was not found." in str(e): # There is no readme
        readme_text = get_edited_yaml_readme(repo, token=token)
      else:
        print(f"Something went wrong: {e}")

    liste = [CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=readme_text.encode())]
    commit = (create_commit(repo_id=repo, token=token, operations=liste, commit_message=message, commit_description=desc, repo_type="model", **edited).pr_url)

    return commit

  except Exception as e:

    if "Discussions are disabled for this repo" in str(e):
      return "Discussions disabled"
    elif "Cannot access gated repo" in str(e):
      return "Gated repo"
    elif "Repository Not Found" in str(e):
      return "Repository Not Found"
    else:
      return e