File size: 9,678 Bytes
b787f43
 
 
 
 
 
 
 
 
e9177b9
21f0ce7
b787f43
 
 
4bf6626
 
 
 
843e2ce
b787f43
21f0ce7
 
 
b787f43
9b95b87
b787f43
8c44c94
b787f43
9b95b87
b787f43
 
4bf6626
 
 
 
 
 
 
 
e9177b9
b787f43
 
 
 
9b95b87
 
b787f43
 
 
9b95b87
b787f43
 
 
 
9b95b87
e9177b9
 
9b95b87
 
 
 
b787f43
9b95b87
 
b787f43
9b95b87
e9177b9
 
9b95b87
 
 
 
b787f43
9b95b87
 
b787f43
9b95b87
e9177b9
9b95b87
b787f43
9b95b87
 
 
b787f43
9b95b87
b787f43
9b95b87
 
e9177b9
 
9b95b87
 
 
 
b787f43
9b95b87
 
 
 
cded501
e9177b9
9b95b87
 
 
b787f43
9b95b87
 
 
 
 
e9177b9
 
9b95b87
 
 
 
 
 
 
 
 
cded501
e9177b9
9b95b87
 
 
 
 
 
 
 
 
cded501
e9177b9
9b95b87
 
 
 
 
 
 
 
 
f22aa15
9b95b87
 
 
 
 
 
 
 
 
b787f43
 
 
 
 
 
 
e9177b9
b787f43
 
a02bd06
 
f22aa15
b787f43
 
 
 
 
 
 
 
 
 
21f0ce7
b787f43
 
 
 
 
 
 
 
 
 
 
 
 
 
21f0ce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b787f43
 
 
 
 
 
 
 
 
 
 
 
 
21f0ce7
8e2a724
21f0ce7
8e2a724
b787f43
 
 
 
 
50e57c1
b787f43
 
 
 
e9177b9
b787f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9177b9
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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_json_format_data, get_datas
import pandas as pd
import traceback
from huggingface_hub import HfApi

BOT_HF_TOKEN = os.getenv('BOT_HF_TOKEN')


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

source_name = "Open Portuguese LLM Leaderboard"
default_pull_request_title = "Adding the Open Portuguese LLM Leaderboard Evaluation Results"

desc = """
This is an automated PR created with https://huggingface.co/spaces/eduagarcia-temp/portuguese-leaderboard-results-to-modelcard

The purpose of this PR is to add evaluation results from the [🚀 Open Portuguese LLM Leaderboard](https://huggingface.co/spaces/eduagarcia/open_pt_llm_leaderboard) to your model card.

If you encounter any issues, please report them to https://huggingface.co/spaces/eduagarcia-temp/portuguese-leaderboard-results-to-modelcard/discussions
"""

def search(query_df, value):
    global df
    if df is None and query_df is None:
        data = get_json_format_data()
        finished_models = get_datas(data)
        df = pd.DataFrame(finished_models)
    if query_df is not None:
        df = query_df
    result_df = df[df["Model Name"] == 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/eduagarcia-temp/llm_pt_leaderboard_raw_results/tree/main/{repo}"


def get_query_url(repo):
  return f"https://huggingface.co/spaces/eduagarcia/open_pt_llm_leaderboard?query={repo}"


def get_task_summary(results):
  return {
      "ENEM":
          {"dataset_type":"eduagarcia/enem_challenge",
          "dataset_name":"ENEM Challenge (No Images)",
          "metric_type":"acc",
          "metric_value":results["ENEM"],
          "dataset_config": None,
          "dataset_split":"train",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 3},
          "metric_name":"accuracy"
          },
      "BLUEX":
          {"dataset_type":"eduagarcia-temp/BLUEX_without_images",
          "dataset_name":"BLUEX (No Images)",
          "metric_type":"acc",
          "metric_value":results["BLUEX"],
          "dataset_config": None,
          "dataset_split":"train",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 3},
          "metric_name":"accuracy"
          },
      "OAB Exams":
          {"dataset_type":"eduagarcia/oab_exams",
          "dataset_name":"OAB Exams",
          "metric_type":"acc",
          "metric_value":results["OAB Exams"],
          "dataset_config": None,
          "dataset_split":"train",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 3},
          "metric_name":"accuracy"
          },
      "ASSIN2 RTE":
          {"dataset_type":"assin2",
          "dataset_name":"Assin2 RTE",
          "metric_type":"f1_macro",
          "metric_value":results["ASSIN2 RTE"],
          "dataset_config": None,
          "dataset_split":"test",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 15},
          "metric_name":"f1-macro"
          },
      "ASSIN2 STS":
          {"dataset_type":"eduagarcia/portuguese_benchmark",
          "dataset_name":"Assin2 STS",
          "metric_type":"pearson",
          "metric_value":results["ASSIN2 STS"],
          "dataset_config": None,
          "dataset_split":"test",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 15},
          "metric_name":"pearson"
          },
      "FAQUAD NLI":
          {"dataset_type":"ruanchaves/faquad-nli",
          "dataset_name":"FaQuAD NLI",
          "metric_type":"f1_macro",
          "metric_value":results["FAQUAD NLI"],
          "dataset_config": None,
          "dataset_split":"test",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 15},
          "metric_name":"f1-macro"
          },
      "HateBR":
          {"dataset_type":"ruanchaves/hatebr",
          "dataset_name":"HateBR Binary",
          "metric_type":"f1_macro",
          "metric_value":results["HateBR"],
          "dataset_config": None,
          "dataset_split":"test",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 25},
          "metric_name":"f1-macro"
          },
      "PT Hate Speech":
          {"dataset_type":"hate_speech_portuguese",
          "dataset_name":"PT Hate Speech Binary",
          "metric_type":"f1_macro",
          "metric_value":results["PT Hate Speech"],
          "dataset_config": None,
          "dataset_split":"test",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 25},
          "metric_name":"f1-macro"
          },
      "tweetSentBR":
          {"dataset_type":"eduagarcia/tweetsentbr_fewshot",
          "dataset_name":"tweetSentBR",
          "metric_type":"f1_macro",
          "metric_value":results["tweetSentBR"],
          "dataset_config": None,
          "dataset_split":"test",
          "dataset_revision":None,
          "dataset_args":{"num_few_shot": 25},
          "metric_name":"f1-macro"
          }
  }

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 = [["Average", f"**{results['Average ⬆️']}**"]] + [[v["dataset_name"], v["metric_value"]] for v in task_summary.values()]

  text = f"""
# Open Portuguese LLM Leaderboard Evaluation Results  

Detailed results can be found [here]({get_details_url(repo)}) and on the [🚀 Open Portuguese LLM Leaderboard](https://huggingface.co/spaces/eduagarcia/open_pt_llm_leaderboard)

{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": source_name, "source_url": get_query_url(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 pr_already_exists(repo, token: str | None = None):
  card = ModelCard.load(repo, token=token)
  if 'eval_results' in card.data and card.data['eval_results']:
    for x in card.data['eval_results']:
      if x.source_name == source_name:
        return True
  if 'Open Portuguese LLM Leaderboard' in card.content:
    return True
  if 'Open PT LLM Leaderboard' in card.content:
    return True

  api = HfApi(token=token)
  for x in api.get_repo_discussions(repo):
    if x.title == default_pull_request_title:
      return True
    if x.author == "leaderboard-pt-pr-bot":
      return True
    if x.author == "eduagarcia" and x.is_pull_request:
      return True
  
  return False

def commit(repo, pr_number=None, message=default_pull_request_title, oauth_token: gr.OAuthToken | None = None, check_if_pr_exists=False): # specify pr number if you want to edit it, don't if you don't want
  if oauth_token is None:
    gr.Warning("You are not logged in; therefore, the leaderboard-pr-bot will open the pull request instead of you. Click on 'Sign in with Huggingface' to log in.")
    token = BOT_HF_TOKEN
  elif oauth_token.expires_at < time.time():
    raise gr.Error("Token expired. Logout and try again.")
  else:
    token = oauth_token.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)}")
      
  if check_if_pr_exists or token == BOT_HF_TOKEN:
    if pr_already_exists(repo, token):
      return "PR already exists, Login to make a duplicate PR"
    
  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).rstrip() + '\n\n' + 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:
        traceback.print_exc()
        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
    
if __name__ == "__main__":
  print(get_eval_results("Qwen/Qwen1.5-72B-Chat"))