qiqiWav commited on
Commit
d946cfe
·
verified ·
1 Parent(s): f578a9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -48
app.py CHANGED
@@ -1,19 +1,23 @@
 
1
  import gradio as gr
2
  import pandas as pd
3
- # from gradio_huggingfacehub_search import HuggingfaceHubSearch
4
  import requests
5
 
6
- def update_table(category):
7
- # TODO: url -> env
8
- url = "https://leaderboard.nexa4ai.com/model/get-ranking-by-category"
9
- url_with_params = f"{url}?category={category}"
 
 
 
 
 
10
 
11
- print("Getting model table with URL:", url_with_params)
12
  response = requests.get(url_with_params)
13
 
14
  if response.status_code == 200:
15
  ranking_data = response.json()["ranking"]
16
- # Process the API data into a DataFrame
17
  api_model_data = {
18
  "Model": [item["model_id"] for item in ranking_data],
19
  "Votes": [item["votes"] for item in ranking_data],
@@ -25,56 +29,47 @@ def update_table(category):
25
  api_df = pd.DataFrame()
26
  return api_df
27
 
 
28
  def get_user(profile: gr.OAuthProfile | None) -> str:
29
  if profile is None:
30
  return ""
31
  return profile.username
32
 
 
33
  def get_vote_models(category):
34
- print (category)
35
  if not category:
36
  return []
37
-
38
- url = "https://leaderboard.nexa4ai.com/model/get-models-by-category"
39
- url_with_params = f"{url}?category={category}"
40
 
41
- print("Getting models with URL:", url_with_params)
 
42
  response = requests.get(url_with_params)
43
 
44
  if response.status_code == 200:
45
  models_data = response.json()
46
- models.choices = models_data # Set choices of models
47
- print(f"models_data: {models_data}")
48
- print(f"models.choices: {models.choices}")
49
- return models_data
50
  else:
51
  print(f"Failed to get models: {response.text}")
52
  return []
53
 
54
 
55
-
56
- def submit_vote(username, category, model_ids):
57
- if not category or not model_ids:
58
  return "All fields are required!"
59
  if not username:
60
  return "You need to log in to submit a vote."
61
  if username.startswith("Hello "):
62
  username = username[6:]
63
-
64
- url = "https://leaderboard.nexa4ai.com/model/vote"
65
- params = {
66
- "category": category,
67
- "username": username
68
- }
69
  data = {
70
- "model_ids": model_ids
 
 
71
  }
72
 
73
- print("Submitting vote with payload:", data)
74
- response = requests.post(url, params=params, json=data)
75
 
76
  if response.status_code == 200:
77
- return f"Vote '{model_ids}' submitted successfully!"
78
  else:
79
  return f"Failed to vote: {response.text}"
80
 
@@ -83,14 +78,12 @@ def submit_model(category, model_id):
83
  if not category or not model_id:
84
  return "All fields are required!"
85
 
86
- url = "https://leaderboard.nexa4ai.com/" # Will have a new endpoint
87
  data = {
88
- "category": category,
89
- "model_id": model_id
90
  }
91
 
92
- print("Submitting model with payload:", data)
93
- response = requests.post(url, json=data)
94
 
95
  if response.status_code == 200:
96
  return "Your request has been submitted successfully. We will notify you by email once processing is complete."
@@ -98,7 +91,9 @@ def submit_model(category, model_id):
98
  return f"Failed to submit request: {response.text}"
99
 
100
 
101
- with gr.Blocks() as app:
 
 
102
  with gr.Tabs():
103
  with gr.TabItem("Table"):
104
  category = gr.Dropdown(
@@ -125,23 +120,21 @@ with gr.Blocks() as app:
125
  choices=["Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"],
126
  label="Select Category"
127
  )
128
-
129
- models = gr.CheckboxGroup(choices=[], label="Choose your options")
130
- submit_button = gr.Button("Submit Vote")
131
- submit_result = gr.Label()
132
 
133
- category.change(get_vote_models, inputs=[category], outputs=[models]) # Fix: Use models.choices to update choices
134
-
135
- submit_button.click(fn=submit_vote, inputs=[username_text, category, models], outputs=submit_result)
136
 
 
137
 
 
138
 
139
 
140
- # with gr.TabItem("Submit Model"):
141
- # category = gr.Dropdown(choices=["Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"], label="Select Category")
142
- # model_id = HuggingfaceHubSearch(label="Hub Model ID", placeholder="Search for model id on Huggingface", search_type="model")
143
- # submit_model_button = gr.Button("Submit Model")
144
- # submit_model_result = gr.Label()
145
- # submit_model_button.click(fn=submit_model, inputs=[category, model_id], outputs=submit_model_result)
146
 
147
  app.launch()
 
1
+ import os
2
  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
+
9
+ 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],
 
29
  api_df = pd.DataFrame()
30
  return api_df
31
 
32
+
33
  def get_user(profile: gr.OAuthProfile | None) -> str:
34
  if profile is None:
35
  return ""
36
  return profile.username
37
 
38
+
39
  def get_vote_models(category):
 
40
  if not category:
41
  return []
 
 
 
42
 
43
+ url_with_params = f"{get_models_url}?category={category}"
44
+
45
  response = requests.get(url_with_params)
46
 
47
  if response.status_code == 200:
48
  models_data = response.json()
49
+ return gr.CheckboxGroup(choices=models_data, label="Choose your options", interactive=True)
 
 
 
50
  else:
51
  print(f"Failed to get models: {response.text}")
52
  return []
53
 
54
 
55
+ def submit_vote(username, category, models):
56
+ if not category or not models:
 
57
  return "All fields are required!"
58
  if not username:
59
  return "You need to log in to submit a vote."
60
  if username.startswith("Hello "):
61
  username = username[6:]
62
+
 
 
 
 
 
63
  data = {
64
+ "category": category,
65
+ "username": username,
66
+ "model_ids": models
67
  }
68
 
69
+ response = requests.post(vote_url, json=data)
 
70
 
71
  if response.status_code == 200:
72
+ return f"Vote '{models}' submitted successfully!"
73
  else:
74
  return f"Failed to vote: {response.text}"
75
 
 
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."
 
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(
 
120
  choices=["Biology", "Physics", "Business", "Chemistry", "Economics", "Philosophy", "History", "Culture", "Computer Science", "Math", "Health", "Law", "Engineering", "Other"],
121
  label="Select Category"
122
  )
 
 
 
 
123
 
124
+ cbg = gr.CheckboxGroup(choices=[], label="Choose your options",interactive=True)
125
+ submit_button = gr.Button("Submit Vote")
126
+ submit_result = gr.Markdown()
127
 
128
+ category.change(get_vote_models, inputs=category, outputs=cbg)
129
 
130
+ submit_button.click(fn=submit_vote, inputs=[username_text, category, cbg], outputs=submit_result)
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()