Weyaxi Wauplin HF staff commited on
Commit
1a014ff
1 Parent(s): 0df3458

Add "Sign in with Hugging Face" button (+ remove login() use) (#5)

Browse files

- [WIP] (9ffa4ede6a84919caace7f088921a2fb20dfbf3d)
- Update app.py (f4ded7174aecf595512b6ffc02123b659a5b09a8)

Co-authored-by: Lucain Pouget <Wauplin@users.noreply.huggingface.co>

Files changed (2) hide show
  1. README.md +4 -1
  2. app.py +33 -8
README.md CHANGED
@@ -4,9 +4,12 @@ emoji: 🧐
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 4.3.0
8
  app_file: app.py
9
  pinned: false
 
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 4.14.0
8
  app_file: app.py
9
  pinned: false
10
+ hf_oauth: true
11
+ hf_oauth_scopes:
12
+ - read-repos
13
  ---
14
 
15
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,4 +1,5 @@
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
@@ -133,8 +134,8 @@ Detailed results can be found [here]({get_details_url(repo)})
133
  return text
134
 
135
 
136
- def get_edited_yaml_readme(repo):
137
- card = ModelCard.load(repo)
138
  results = search(df, repo)
139
 
140
  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}"}
@@ -153,21 +154,26 @@ def get_edited_yaml_readme(repo):
153
  return str(card)
154
 
155
 
156
- 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
157
- login(hf_token)
 
 
 
 
 
158
  edited = {"revision": f"refs/pr/{pr_number}"} if pr_number else {"create_pr": True}
159
 
160
  try:
161
  try: # check if there is a readme already
162
- readme_text = get_edited_yaml_readme(repo) + get_eval_results(repo)
163
  except Exception as e:
164
  if "Repo card metadata block was not found." in str(e): # There is no readme
165
- readme_text = get_edited_yaml_readme(repo)
166
  else:
167
  print(f"Something went wrong: {e}")
168
 
169
  liste = [CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=readme_text.encode())]
170
- commit = (create_commit(repo_id=repo, operations=liste, commit_message=message, commit_description=desc, repo_type="model", **edited).pr_url)
171
 
172
  return commit
173
 
@@ -203,6 +209,25 @@ The leaderboard's backend mainly runs on the [Hugging Face Hub API](https://hugg
203
  - Special thanks to [Lucain Pouget (Wauplin)](https://huggingface.co/Wauplin) for assisting with the [Hugging Face Hub API](https://huggingface.co/docs/huggingface_hub/v0.5.1/en/package_reference/hf_api).
204
  """
205
 
206
- demo = gr.Interface(title=gradio_title, description=gradio_desc, fn=commit, inputs=["text", "text"], outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
  demo.launch()
 
1
  import os
2
+ import time
3
  os.system("wget https://raw.githubusercontent.com/Weyaxi/scrape-open-llm-leaderboard/main/openllm.py")
4
  from huggingface_hub import CommitOperationAdd, create_commit, HfApi, HfFileSystem, login
5
  from huggingface_hub import ModelCardData, EvalResult, ModelCard
 
134
  return text
135
 
136
 
137
+ def get_edited_yaml_readme(repo, token: str | None):
138
+ card = ModelCard.load(repo, token=token)
139
  results = search(df, repo)
140
 
141
  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}"}
 
154
  return str(card)
155
 
156
 
157
+ 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
158
+ if oauth_token is None:
159
+ raise gr.Error("You must be logged in to open a PR. Click on 'Sign in with Huggingface' first.")
160
+ if oauth_token.expires_at < time.time():
161
+ raise gr.Error("Token expired. Logout and try again.")
162
+ token = oauth_token.token
163
+
164
  edited = {"revision": f"refs/pr/{pr_number}"} if pr_number else {"create_pr": True}
165
 
166
  try:
167
  try: # check if there is a readme already
168
+ readme_text = get_edited_yaml_readme(repo, token=token) + get_eval_results(repo)
169
  except Exception as e:
170
  if "Repo card metadata block was not found." in str(e): # There is no readme
171
+ readme_text = get_edited_yaml_readme(repo, token=token)
172
  else:
173
  print(f"Something went wrong: {e}")
174
 
175
  liste = [CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=readme_text.encode())]
176
+ commit = (create_commit(repo_id=repo, token=token, operations=liste, commit_message=message, commit_description=desc, repo_type="model", **edited).pr_url)
177
 
178
  return commit
179
 
 
209
  - Special thanks to [Lucain Pouget (Wauplin)](https://huggingface.co/Wauplin) for assisting with the [Hugging Face Hub API](https://huggingface.co/docs/huggingface_hub/v0.5.1/en/package_reference/hf_api).
210
  """
211
 
212
+ with gr.Blocks() as demo:
213
+ gr.Markdown(f"""<h1 align="center" id="space-title">{gradio_title}</h1>""")
214
+ gr.Markdown(gradio_desc)
215
+
216
+ with gr.Row(equal_height=False):
217
+ with gr.Column():
218
+ space_id = gr.Textbox(label="Model ID or URL", lines=1)
219
+ gr.LoginButton()
220
+
221
+ with gr.Column():
222
+ output = gr.Textbox(label="Output", lines=1)
223
+ gr.LogoutButton()
224
+
225
+ submit_btn = gr.Button("Submit", variant="primary")
226
+ try:
227
+ space_id = "/".join(space_id.split('/')[-2::]) if space_id.startswith("https://huggingface.co/") else space_id
228
+ except Exception as e:
229
+ gr.Error(f"There is an error with your model ID, check it again: {e}")
230
+
231
+ submit_btn.click(commit, space_id, output)
232
 
233
  demo.launch()