Weyaxi commited on
Commit
b73db7d
1 Parent(s): a932099

now with widget support

Browse files
Files changed (1) hide show
  1. app.py +142 -24
app.py CHANGED
@@ -1,11 +1,15 @@
1
  import os
2
  os.system("wget https://raw.githubusercontent.com/Weyaxi/scrape-open-llm-leaderboard/main/openllm.py")
3
  from huggingface_hub import CommitOperationAdd, create_commit, HfApi, HfFileSystem, login
4
- from huggingface_hub.utils import RepositoryNotFoundError as deneme
5
- from openllm import *
6
- import gradio as gr
 
 
 
7
  import requests
8
  import pandas as pd
 
9
 
10
  api = HfApi()
11
  fs = HfFileSystem()
@@ -25,49 +29,163 @@ def get_details_url(repo):
25
  return f"https://huggingface.co/datasets/open-llm-leaderboard/details_{author}__{model}"
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def get_eval_results(repo):
29
  results = search(df, repo)
 
 
 
 
30
 
31
  text = f"""
32
  # [Open LLM Leaderboard Evaluation Results](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard)
33
  Detailed results can be found [here]({get_details_url(repo)})
34
 
35
- | Metric | Value |
36
- |-----------------------|---------------------------|
37
- | Avg. | {results['Average ⬆️']} |
38
- | ARC (25-shot) | {results['ARC']} |
39
- | HellaSwag (10-shot) | {results['HellaSwag']} |
40
- | MMLU (5-shot) | {results['MMLU']} |
41
- | TruthfulQA (0-shot) | {results['TruthfulQA']} |
42
- | Winogrande (5-shot) | {results['Winogrande']} |
43
- | GSM8K (5-shot) | {results['GSM8K']} |
44
  """
45
  return text
46
 
47
 
48
- desc = """
49
- This is an automated PR created with https://huggingface.co/spaces/Weyaxi/open-llm-leaderboard-results-pr
 
50
 
51
- The purpose of this PR is to add evaluation results from the Open LLM Leaderboard to your model card.
52
 
53
- If you encounter any issues, please report them to https://huggingface.co/spaces/Weyaxi/open-llm-leaderboard-results-pr/discussions
54
- """
 
 
 
 
 
 
 
 
55
 
 
 
56
 
57
- def commit(hf_token, repo):
58
  login(hf_token)
 
 
59
  try:
60
  try: # check if there is a readme already
61
- readme_text = fs.read_text(f"{repo}/README.md") + get_eval_results(repo)
62
- except:
63
- readme_text = get_eval_results(repo)
 
 
 
64
 
65
  liste = [CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=readme_text.encode())]
66
- commit = (create_commit(repo_id=repo, operations=liste, commit_message=f"Adding Evaluation Results", commit_description=desc, repo_type="model", create_pr=True).__dict__['pr_url'])
 
67
  return commit
68
- except Exception as e: # unexpected error
69
- return e
70
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  demo = gr.Interface(fn=commit, inputs=["text", "text"], outputs="text")
73
 
 
1
  import os
2
  os.system("wget https://raw.githubusercontent.com/Weyaxi/scrape-open-llm-leaderboard/main/openllm.py")
3
  from huggingface_hub import CommitOperationAdd, create_commit, HfApi, HfFileSystem, login
4
+ from huggingface_hub import ModelCardData, EvalResult, ModelCard
5
+ from huggingface_hub.repocard_data import eval_results_to_model_index
6
+ from huggingface_hub.repocard import RepoCard
7
+ from openllm import get_json_format_data, get_datas
8
+ from tqdm import tqdm
9
+ import time
10
  import requests
11
  import pandas as pd
12
+ from pytablewriter import MarkdownTableWriter
13
 
14
  api = HfApi()
15
  fs = HfFileSystem()
 
29
  return f"https://huggingface.co/datasets/open-llm-leaderboard/details_{author}__{model}"
30
 
31
 
32
+ def get_query_url(repo):
33
+ return f"https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard?query={repo}"
34
+
35
+
36
+ desc = """
37
+ This is an automated PR created with https://huggingface.co/spaces/Weyaxi/open-llm-leaderboard-results-pr
38
+
39
+ The purpose of this PR is to add evaluation results from the Open LLM Leaderboard to your model card.
40
+
41
+ If you encounter any issues, please report them to https://huggingface.co/spaces/Weyaxi/open-llm-leaderboard-results-pr/discussions
42
+ """
43
+
44
+
45
+ def get_task_summary(results):
46
+ return {
47
+ "ARC":
48
+ {"dataset_type":"ai2_arc",
49
+ "dataset_name":"AI2 Reasoning Challenge (25-Shot)",
50
+ "dataset_short_name": "ARC (25-shot)",
51
+ "metric_type":"acc_norm",
52
+ "metric_value":results["ARC"],
53
+ "dataset_config":"ARC-Challenge",
54
+ "dataset_split":"test",
55
+ "dataset_revision":None,
56
+ "dataset_args":{"num_few_shot": 25},
57
+ "metric_name":"normalized accuracy"
58
+ },
59
+ "HellaSwag":
60
+ {"dataset_type":"hellaswag",
61
+ "dataset_name":"HellaSwag (10-Shot)",
62
+ "dataset_short_name": "HellaSwag (10-shot)",
63
+ "metric_type":"acc_norm",
64
+ "metric_value":results["HellaSwag"],
65
+ "dataset_config":None,
66
+ "dataset_split":"validation",
67
+ "dataset_revision":None,
68
+ "dataset_args":{"num_few_shot": 10},
69
+ "metric_name":"normalized accuracy"
70
+ },
71
+ "MMLU":
72
+ {
73
+ "dataset_type":"cais/mmlu",
74
+ "dataset_name":"MMLU (5-Shot)",
75
+ "dataset_short_name": "MMLU (5-Shot)",
76
+ "metric_type":"acc",
77
+ "metric_value":results["MMLU"],
78
+ "dataset_config":"all",
79
+ "dataset_split":"test",
80
+ "dataset_revision":None,
81
+ "dataset_args":{"num_few_shot": 5},
82
+ "metric_name":"accuracy"
83
+ },
84
+ "TruthfulQA":
85
+ {
86
+ "dataset_type":"truthful_qa",
87
+ "dataset_name":"TruthfulQA (0-shot)",
88
+ "dataset_short_name": "TruthfulQA (0-shot)",
89
+ "metric_type":"mc2",
90
+ "metric_value":results["TruthfulQA"],
91
+ "dataset_config":"multiple_choice",
92
+ "dataset_split":"validation",
93
+ "dataset_revision":None,
94
+ "dataset_args":{"num_few_shot": 0},
95
+ "metric_name":None
96
+ },
97
+ "Winogrande":
98
+ {
99
+ "dataset_type":"winogrande",
100
+ "dataset_name":"Winogrande (5-shot)",
101
+ "dataset_short_name": "Winogrande (5-shot)",
102
+ "metric_type":"acc",
103
+ "metric_value":results["Winogrande"],
104
+ "dataset_config":"winogrande_xl",
105
+ "dataset_split":"validation",
106
+ "dataset_args":{"num_few_shot": 5},
107
+ "metric_name":"accuracy"
108
+ },
109
+ "GSM8K":
110
+ {
111
+ "dataset_type":"gsm8k",
112
+ "dataset_name":"GSM8k (5-shot)",
113
+ "dataset_short_name": "GSM8k (5-shot)",
114
+ "metric_type":"acc",
115
+ "metric_value":results["GSM8K"],
116
+ "dataset_config":"main",
117
+ "dataset_split":"test",
118
+ "dataset_args":{"num_few_shot": 5},
119
+ "metric_name":"accuracy"
120
+ }
121
+ }
122
+
123
+
124
+
125
  def get_eval_results(repo):
126
  results = search(df, repo)
127
+ task_summary = get_task_summary(results)
128
+ md_writer = MarkdownTableWriter()
129
+ md_writer.headers = ["Metric", "Value"]
130
+ md_writer.value_matrix = [["Avg.", results['Average ⬆️']]] + [[v["dataset_short_name"], v["metric_value"]] for v in task_summary.items()]
131
 
132
  text = f"""
133
  # [Open LLM Leaderboard Evaluation Results](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard)
134
  Detailed results can be found [here]({get_details_url(repo)})
135
 
136
+ {md_writer.dumps()}
 
 
 
 
 
 
 
 
137
  """
138
  return text
139
 
140
 
141
+ def get_edited_yaml_readme(repo):
142
+ card = ModelCard.load(repo)
143
+ results = search(df, repo)
144
 
145
+ common = {"task_type": 'text-generation', "task_name": 'Text Generation', "source_name": "Open LLM Leaderboard", "source_url": f"https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard?query={repo}"}
146
 
147
+ tasks_results = get_task_summary(results)
148
+
149
+ if not card.data['eval_results']: # No results reported yet, we initialize the metadata
150
+ card.data["model-index"] = eval_results_to_model_index(repo.split('/')[1], [EvalResult(**task, **common) for task in tasks_results.values()])
151
+ else: # We add the new evaluations
152
+ for task in tasks_results.values():
153
+ cur_result = EvalResult(**task, **common)
154
+ if any(result.is_equal_except_value(cur_result) for result in card.data['eval_results']):
155
+ continue
156
+ card.data['eval_results'].append(cur_result)
157
 
158
+ return str(card)
159
+
160
 
161
+ def commit(hf_token, repo, pr_number=None, message="Adding Evaluation Results"): # specify pr number if you want to edit it, don't if you don't want
162
  login(hf_token)
163
+ edited = {"revision": f"refs/pr/{pr_number}"} if pr_number else {"create_pr": True}
164
+
165
  try:
166
  try: # check if there is a readme already
167
+ readme_text = get_edited_yaml_readme(repo) + get_eval_results(repo)
168
+ except Exception as e:
169
+ if "Repo card metadata block was not found." in str(e): # There is no readme
170
+ readme_text = get_edited_yaml_readme(repo)
171
+ else:
172
+ print(f"Something went wrong: {e}")
173
 
174
  liste = [CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=readme_text.encode())]
175
+ commit = (create_commit(repo_id=repo, operations=liste, commit_message=message, commit_description=desc, repo_type="model", **edited).pr_url)
176
+
177
  return commit
 
 
178
 
179
+ except Exception as e:
180
+
181
+ if "Discussions are disabled for this repo" in str(e):
182
+ return "Discussions disabled"
183
+ elif "Cannot access gated repo" in str(e):
184
+ return "Gated repo"
185
+ elif "Repository Not Found" in str(e):
186
+ return "Repository Not Found"
187
+ else:
188
+ return e
189
 
190
  demo = gr.Interface(fn=commit, inputs=["text", "text"], outputs="text")
191