Weyaxi commited on
Commit
5a667ef
1 Parent(s): d87add0

Auto restart and errror handling when fetching open llm leaderboard

Browse files
Files changed (1) hide show
  1. app.py +39 -18
app.py CHANGED
@@ -6,6 +6,9 @@ from tqdm import tqdm
6
  from bs4 import BeautifulSoup
7
  from huggingface_hub import HfApi, list_models, list_datasets, list_spaces
8
  import gradio as gr
 
 
 
9
 
10
  api = HfApi()
11
 
@@ -26,26 +29,32 @@ def get_sum(df_for_sum_function):
26
  return {"Downloads": sum_downloads, "Likes": sum_likes}
27
 
28
  def get_openllm_leaderboard():
29
- url = 'https://huggingfaceh4-open-llm-leaderboard.hf.space/'
30
- response = requests.get(url)
31
- soup = BeautifulSoup(response.content, 'html.parser')
32
- script_elements = soup.find_all('script')
33
- data = json.loads(str(script_elements[1])[31:-10])
34
-
35
- component_index = 19
36
-
37
- result_list = []
38
- i = 0
39
- while True:
40
- try:
41
- normal_name = data['components'][component_index]['props']['value']['data'][i][-1]
42
- result_list.append(normal_name)
43
- i += 1
44
- except (IndexError, AttributeError):
45
- return result_list
 
 
 
 
46
 
47
 
48
  def get_ranking(model_list, target_org):
 
 
49
  for index, model in enumerate(model_list):
50
  if model.split("/")[0].lower() == target_org.lower():
51
  return [index+1, model]
@@ -208,10 +217,14 @@ def get_ranking_trend(json_data, org_name):
208
  else:
209
  return {"id": "Not Found", "rank": "Not Found"}
210
 
 
 
 
 
211
  with open("org_names.txt", "r") as f:
212
  org_names_in_list = [i.rstrip("\n") for i in f.readlines()]
213
 
214
-
215
  INTRODUCTION_TEXT = f"""
216
  🎯 The Organization Leaderboard aims to track organization rankings. This space is inspired by the [Open LLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard).
217
 
@@ -232,6 +245,10 @@ INTRODUCTION_TEXT = f"""
232
  **🌐 Note:** In the model's dataframe, there are some columns related to the [Open LLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard). This data is also retrieved through web scraping.
233
 
234
  **🌐 Note:** In trending models, first 300 models/datasets/spaces is being retrieved from huggingface.
 
 
 
 
235
  """
236
 
237
  with gr.Blocks() as demo:
@@ -268,4 +285,8 @@ with gr.Blocks() as demo:
268
  headers = ["🔢 Serial Number", "🏢 Organization Name", "👍 Total Likes", "🚀 Number of Spaces", "📈 Average Likes per Space", "❤️ Most Liked Space", "👍 Most Like Count", "🔥 Trending Space", "👑 Best Rank at Trending Spaces"]
269
  gr.Dataframe(spaces_df.head(200), headers=headers, interactive=False, datatype=["str", "markdown", "str", "str", "str", "markdown", "str", "markdown", "str"])
270
 
 
 
 
 
271
  demo.launch()
 
6
  from bs4 import BeautifulSoup
7
  from huggingface_hub import HfApi, list_models, list_datasets, list_spaces
8
  import gradio as gr
9
+ from apscheduler.schedulers.background import BackgroundScheduler
10
+ import datetime
11
+
12
 
13
  api = HfApi()
14
 
 
29
  return {"Downloads": sum_downloads, "Likes": sum_likes}
30
 
31
  def get_openllm_leaderboard():
32
+ try:
33
+ url = 'https://huggingfaceh4-open-llm-leaderboard.hf.space/'
34
+ response = requests.get(url)
35
+ soup = BeautifulSoup(response.content, 'html.parser')
36
+ script_elements = soup.find_all('script')
37
+ data = json.loads(str(script_elements[1])[31:-10])
38
+
39
+ component_index = 19
40
+
41
+ result_list = []
42
+ i = 0
43
+ while True:
44
+ try:
45
+ normal_name = data['components'][component_index]['props']['value']['data'][i][-1]
46
+ result_list.append(normal_name)
47
+ i += 1
48
+ except (IndexError, AttributeError):
49
+ return result_list
50
+ except Exception as e:
51
+ print("Error on open llm leaderboard: ", e)
52
+ return []
53
 
54
 
55
  def get_ranking(model_list, target_org):
56
+ if model_list == []:
57
+ return "Error on Leaderboard"
58
  for index, model in enumerate(model_list):
59
  if model.split("/")[0].lower() == target_org.lower():
60
  return [index+1, model]
 
217
  else:
218
  return {"id": "Not Found", "rank": "Not Found"}
219
 
220
+ def restart_space():
221
+ api.restart_space(repo_id="TFLai/organization-leaderboard", token=HF_TOKEN)
222
+
223
+
224
  with open("org_names.txt", "r") as f:
225
  org_names_in_list = [i.rstrip("\n") for i in f.readlines()]
226
 
227
+ datetime = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
228
  INTRODUCTION_TEXT = f"""
229
  🎯 The Organization Leaderboard aims to track organization rankings. This space is inspired by the [Open LLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard).
230
 
 
245
  **🌐 Note:** In the model's dataframe, there are some columns related to the [Open LLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard). This data is also retrieved through web scraping.
246
 
247
  **🌐 Note:** In trending models, first 300 models/datasets/spaces is being retrieved from huggingface.
248
+
249
+ ## Last Update
250
+
251
+ ⌛ This space is last updated in **{datetime}**.
252
  """
253
 
254
  with gr.Blocks() as demo:
 
285
  headers = ["🔢 Serial Number", "🏢 Organization Name", "👍 Total Likes", "🚀 Number of Spaces", "📈 Average Likes per Space", "❤️ Most Liked Space", "👍 Most Like Count", "🔥 Trending Space", "👑 Best Rank at Trending Spaces"]
286
  gr.Dataframe(spaces_df.head(200), headers=headers, interactive=False, datatype=["str", "markdown", "str", "str", "str", "markdown", "str", "markdown", "str"])
287
 
288
+
289
+
290
+ scheduler = BackgroundScheduler()
291
+ scheduler.add_job(restart_space, "interval", seconds=21600)
292
  demo.launch()