JoyboyBrian commited on
Commit
3ce8926
1 Parent(s): 05c75c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -31
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import pandas as pd
4
  from gradio_huggingfacehub_search import HuggingfaceHubSearch
5
  import requests
 
6
 
7
  leaderboard_url = os.getenv("LEADERBOARD_URL", "https://leaderboard.nexa4ai.com")
8
 
@@ -10,24 +11,36 @@ get_ranking_url = f"{leaderboard_url}/model/get-ranking-by-category"
10
  get_models_url = f"{leaderboard_url}/model/get-models-by-category"
11
  vote_url = f"{leaderboard_url}/model/vote"
12
  submit_models_url = f"{leaderboard_url}/model/submit-models"
13
-
14
- def update_table(category):
15
- url_with_params = f"{get_ranking_url}?category={category}"
16
-
17
- response = requests.get(url_with_params)
18
-
19
- if response.status_code == 200:
20
- ranking_data = response.json()["ranking"]
21
- api_model_data = {
22
- "Model": [item["model_id"] for item in ranking_data],
23
- "Votes": [item["votes"] for item in ranking_data],
24
- "Categories": [item["categories"] for item in ranking_data] if category == "all" else [category] * len(ranking_data)
25
- }
26
- api_df = pd.DataFrame(api_model_data)
27
- else:
28
- print(f"Failed to submit request: {response.text}")
29
- api_df = pd.DataFrame()
30
- return api_df
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
 
33
  def get_user(profile: gr.OAuthProfile | None) -> str:
@@ -74,30 +87,45 @@ def submit_vote(username, category, models):
74
  return f"Failed to vote: {response.text}"
75
 
76
 
77
- def submit_model(category, model_id):
78
- if not category or not model_id:
79
  return "All fields are required!"
80
-
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  data = {
82
  "model_id": model_id,
83
- "categories": [category]
84
  }
85
-
86
  response = requests.post(submit_models_url, json=data)
87
-
88
  if response.status_code == 200:
89
  return "Your request has been submitted successfully. We will notify you by email once processing is complete."
90
  else:
91
  return f"Failed to submit request: {response.text}"
92
 
 
 
 
 
93
 
94
  theme = gr.themes.Soft()
95
 
96
  with gr.Blocks(theme=theme) as app:
97
  with gr.Tabs():
98
- with gr.TabItem("Table"):
99
  category = gr.Dropdown(
100
- choices=["all", "Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"],
101
  label="Select Category",
102
  value="all"
103
  )
@@ -105,19 +133,20 @@ with gr.Blocks(theme=theme) as app:
105
  initial_data = update_table("all")
106
  table = gr.Dataframe(
107
  headers=["Model", "Votes", "Categories"],
108
- datatype=["str", "number", "str"],
109
  value=initial_data,
110
  col_count=(3, "fixed"),
111
  )
 
112
  category.change(update_table, inputs=category, outputs=table)
113
-
114
  with gr.TabItem("Vote"):
115
  username_text = gr.State(value="")
116
  login_button = gr.LoginButton()
117
  app.load(get_user, inputs=None, outputs=username_text)
118
 
119
  category = gr.Dropdown(
120
- choices=["Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"],
121
  label="Select Category"
122
  )
123
 
@@ -131,10 +160,12 @@ with gr.Blocks(theme=theme) as app:
131
 
132
 
133
  with gr.TabItem("Submit Model"):
134
- category = gr.Dropdown(choices=["Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"], label="Select Category")
 
135
  model_id = HuggingfaceHubSearch(label="Hub Model ID", placeholder="Search for model id on Huggingface", search_type="model")
136
  submit_model_button = gr.Button("Submit Model")
137
  submit_model_result = gr.Markdown()
138
- submit_model_button.click(fn=submit_model, inputs=[category, model_id], outputs=submit_model_result)
 
139
 
140
  app.launch(share=True)
 
3
  import pandas as pd
4
  from gradio_huggingfacehub_search import HuggingfaceHubSearch
5
  import requests
6
+ from urllib.parse import quote
7
 
8
  leaderboard_url = os.getenv("LEADERBOARD_URL", "https://leaderboard.nexa4ai.com")
9
 
 
11
  get_models_url = f"{leaderboard_url}/model/get-models-by-category"
12
  vote_url = f"{leaderboard_url}/model/vote"
13
  submit_models_url = f"{leaderboard_url}/model/submit-models"
14
+ submit_category_url = f"{leaderboard_url}/model/submit-category"
15
+
16
+ def make_clickable_model(model_id):
17
+ model_name_show = ' '.join(model_id.split('/')[1:])
18
+ link = "https://huggingface.co/" + model_id
19
+ return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{model_name_show}</a>'
20
+
21
+ def update_table(category):
22
+ retries = 3
23
+ while retries > 0:
24
+ try:
25
+ encoded_category = quote(category)
26
+ url_with_params = f"{get_ranking_url}?category={encoded_category}"
27
+ response = requests.get(url_with_params)
28
+ if response.status_code == 200:
29
+ ranking_data = response.json()["ranking"]
30
+ api_model_data = {
31
+ "Model": [make_clickable_model(item["model_id"]) for item in ranking_data],
32
+ "Votes": [item["votes"] for item in ranking_data],
33
+ "Categories": [item["categories"] for item in ranking_data] if category == "all" else [category] * len(ranking_data)
34
+ }
35
+ api_df = pd.DataFrame(api_model_data)
36
+ return api_df
37
+ else:
38
+ print(f"Failed to submit request: {response.text}")
39
+ retries -= 1
40
+ except requests.exceptions.RequestException as e:
41
+ print(f"Error occurred while making request: {e}")
42
+ retries -= 1
43
+ return pd.DataFrame()
44
 
45
 
46
  def get_user(profile: gr.OAuthProfile | None) -> str:
 
87
  return f"Failed to vote: {response.text}"
88
 
89
 
90
+ def submit_model(category, customize_category, model_id):
91
+ if not model_id:
92
  return "All fields are required!"
93
+ if not category or not customize_category:
94
+ return "Please choose a category or customize your own category!"
95
+
96
+ selected_category = category
97
+
98
+ if customize_category:
99
+ category_data = {
100
+ "category": customize_category
101
+ }
102
+ response = requests.post(submit_category_url, json=category_data)
103
+ if response.status_code != 200:
104
+ return f"Failed to submit category: {response.text}"
105
+ selected_category = customize_category
106
+
107
  data = {
108
  "model_id": model_id,
109
+ "categories": [selected_category]
110
  }
 
111
  response = requests.post(submit_models_url, json=data)
 
112
  if response.status_code == 200:
113
  return "Your request has been submitted successfully. We will notify you by email once processing is complete."
114
  else:
115
  return f"Failed to submit request: {response.text}"
116
 
117
+ def update_dropdown(customize_category, category):
118
+ if customize_category:
119
+ return "", gr.update(value="")
120
+ return category, gr.update()
121
 
122
  theme = gr.themes.Soft()
123
 
124
  with gr.Blocks(theme=theme) as app:
125
  with gr.Tabs():
126
+ with gr.TabItem("Table"):
127
  category = gr.Dropdown(
128
+ choices=["all", "Art & Design", "Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Education", "Health", "Law", "Engineering", "Coding", "Science", "Other"],
129
  label="Select Category",
130
  value="all"
131
  )
 
133
  initial_data = update_table("all")
134
  table = gr.Dataframe(
135
  headers=["Model", "Votes", "Categories"],
136
+ datatype=["markdown", "number", "str"],
137
  value=initial_data,
138
  col_count=(3, "fixed"),
139
  )
140
+ app.load(lambda: update_table("all"), outputs=table)
141
  category.change(update_table, inputs=category, outputs=table)
142
+
143
  with gr.TabItem("Vote"):
144
  username_text = gr.State(value="")
145
  login_button = gr.LoginButton()
146
  app.load(get_user, inputs=None, outputs=username_text)
147
 
148
  category = gr.Dropdown(
149
+ choices=["Art & Design", "Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Education", "Health", "Law", "Engineering", "Coding", "Science", "Other"],
150
  label="Select Category"
151
  )
152
 
 
160
 
161
 
162
  with gr.TabItem("Submit Model"):
163
+ category = gr.Dropdown(choices=["Art & Design", "Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Education", "Health", "Law", "Engineering", "Coding", "Science", "Other"], label="Select Category")
164
+ customize_category = gr.Textbox(label="Customize category", placeholder="Can't find your category? Enter your own.")
165
  model_id = HuggingfaceHubSearch(label="Hub Model ID", placeholder="Search for model id on Huggingface", search_type="model")
166
  submit_model_button = gr.Button("Submit Model")
167
  submit_model_result = gr.Markdown()
168
+ customize_category.change(fn=update_dropdown, inputs=[customize_category, category], outputs=[category])
169
+ submit_model_button.click(fn=submit_model, inputs=[category, customize_category, model_id], outputs=submit_model_result)
170
 
171
  app.launch(share=True)