alozowski commited on
Commit
d42f637
1 Parent(s): efed7dc

Add logic to upvote a user model

Browse files
Files changed (2) hide show
  1. src/display/utils.py +12 -1
  2. src/submission/submit.py +23 -6
src/display/utils.py CHANGED
@@ -1,14 +1,25 @@
1
  from dataclasses import dataclass, make_dataclass
2
  from enum import Enum
 
3
  import json
4
  import logging
5
  from datetime import datetime
6
  import pandas as pd
7
 
8
-
9
  # Configure logging
10
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
11
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Convert ISO 8601 dates to datetime objects for comparison
13
  def parse_iso8601_datetime(date_str):
14
  if date_str.endswith('Z'):
 
1
  from dataclasses import dataclass, make_dataclass
2
  from enum import Enum
3
+ import requests
4
  import json
5
  import logging
6
  from datetime import datetime
7
  import pandas as pd
8
 
 
9
  # Configure logging
10
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
11
 
12
+
13
+ def is_user_in_org(username: str, org: str) -> bool:
14
+ url = f"https://huggingface.co/api/users/{username}/overview"
15
+ response = requests.get(url)
16
+ if response.status_code == 200:
17
+ user_info = response.json()
18
+ return any(org_info['name'] == org for org_info in user_info.get('orgs', []))
19
+ else:
20
+ print(f"Failed to fetch user info for {username}. Status code: {response.status_code}")
21
+ return False
22
+
23
  # Convert ISO 8601 dates to datetime objects for comparison
24
  def parse_iso8601_datetime(date_str):
25
  if date_str.endswith('Z'):
src/submission/submit.py CHANGED
@@ -14,6 +14,8 @@ from src.envs import (
14
  QUEUE_REPO,
15
  RATE_LIMIT_PERIOD,
16
  RATE_LIMIT_QUOTA,
 
 
17
  )
18
  from src.leaderboard.filter_models import DO_NOT_SUBMIT_MODELS
19
  from src.submission.check_validity import (
@@ -24,9 +26,14 @@ from src.submission.check_validity import (
24
  user_submission_permission,
25
  )
26
 
 
 
 
27
  REQUESTED_MODELS = None
28
  USERS_TO_SUBMISSION_DATES = None
29
 
 
 
30
  @dataclass
31
  class ModelSizeChecker:
32
  model: str
@@ -62,7 +69,7 @@ def add_new_eval(
62
  use_chat_template: bool,
63
  profile: gr.OAuthProfile | None
64
  ):
65
- # Login require
66
  if profile is None:
67
  return styled_error("Hub Login Required")
68
 
@@ -87,10 +94,6 @@ def add_new_eval(
87
  if model_type is None or model_type == "":
88
  return styled_error("Please select a model type.")
89
 
90
- # Is user submitting own model?
91
- # Check that username in the org.
92
- # if org_or_user != profile.username:
93
-
94
  # Is the user rate limited?
95
  if org_or_user != "":
96
  user_can_submit, error_msg = user_submission_permission(
@@ -196,6 +199,20 @@ def add_new_eval(
196
  # Remove the local file
197
  os.remove(out_path)
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  return styled_message(
200
  "Your request has been submitted to the evaluation queue!\nPlease wait for up to an hour for the model to show in the PENDING list."
201
- )
 
14
  QUEUE_REPO,
15
  RATE_LIMIT_PERIOD,
16
  RATE_LIMIT_QUOTA,
17
+ VOTES_REPO,
18
+ VOTES_PATH,
19
  )
20
  from src.leaderboard.filter_models import DO_NOT_SUBMIT_MODELS
21
  from src.submission.check_validity import (
 
26
  user_submission_permission,
27
  )
28
 
29
+ from src.voting.vote_system import VoteManager
30
+ from src.display.utils import is_user_in_org
31
+
32
  REQUESTED_MODELS = None
33
  USERS_TO_SUBMISSION_DATES = None
34
 
35
+ vote_manager = VoteManager(VOTES_PATH, EVAL_REQUESTS_PATH, VOTES_REPO)
36
+
37
  @dataclass
38
  class ModelSizeChecker:
39
  model: str
 
69
  use_chat_template: bool,
70
  profile: gr.OAuthProfile | None
71
  ):
72
+ # Login required
73
  if profile is None:
74
  return styled_error("Hub Login Required")
75
 
 
94
  if model_type is None or model_type == "":
95
  return styled_error("Please select a model type.")
96
 
 
 
 
 
97
  # Is the user rate limited?
98
  if org_or_user != "":
99
  user_can_submit, error_msg = user_submission_permission(
 
199
  # Remove the local file
200
  os.remove(out_path)
201
 
202
+
203
+ # Check if the user is submitting their own model or a model from their organization
204
+ if org_or_user == username or is_user_in_org(username, org_or_user):
205
+ # Automatically add a vote for the user's own model or their organization's model
206
+ vote_manager.add_vote(
207
+ selected_model=model,
208
+ pending_models_df=None,
209
+ profile=profile
210
+ )
211
+ print(f"Automatically added a vote for {model} submitted by {username}")
212
+
213
+ # Upload votes to the repository
214
+ vote_manager.upload_votes()
215
+
216
  return styled_message(
217
  "Your request has been submitted to the evaluation queue!\nPlease wait for up to an hour for the model to show in the PENDING list."
218
+ )