Maria Castellanos commited on
Commit
95c0dd3
·
1 Parent(s): 157a3b9

add check for real hf username

Browse files
Files changed (2) hide show
  1. evaluate.py +22 -1
  2. utils.py +35 -0
evaluate.py CHANGED
@@ -10,7 +10,12 @@ from about import (
10
  test_repo,
11
  THROTTLE_MINUTES
12
  )
13
- from utils import bootstrap_metrics, clip_and_log_transform, fetch_dataset_df
 
 
 
 
 
14
  from huggingface_hub import hf_hub_download
15
  import datetime
16
  import io
@@ -51,9 +56,25 @@ class ParticipantRecord(BaseModel):
51
  @field_validator("hf_username")
52
  @classmethod
53
  def validate_hf_username(cls, v: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
54
  v = v.strip()
55
  if not HF_USERNAME_RE.match(v):
56
  raise gr.Error("Invalid Hugging Face username (letters, numbers, -, _; min 2, max ~39).")
 
 
 
 
57
  return v
58
 
59
  @field_validator("display_name")
 
10
  test_repo,
11
  THROTTLE_MINUTES
12
  )
13
+ from utils import (
14
+ bootstrap_metrics,
15
+ clip_and_log_transform,
16
+ fetch_dataset_df,
17
+ check_page_exists,
18
+ )
19
  from huggingface_hub import hf_hub_download
20
  import datetime
21
  import io
 
56
  @field_validator("hf_username")
57
  @classmethod
58
  def validate_hf_username(cls, v: str) -> str:
59
+ """Validate hugging face username exists by checking the url
60
+
61
+ Parameters
62
+ ----------
63
+ username : str
64
+ Hugging Face username
65
+
66
+ Returns
67
+ -------
68
+ bool
69
+ Whether Hugging Face username is real
70
+ """
71
  v = v.strip()
72
  if not HF_USERNAME_RE.match(v):
73
  raise gr.Error("Invalid Hugging Face username (letters, numbers, -, _; min 2, max ~39).")
74
+
75
+ hf_url = f"https://huggingface.co/{v}"
76
+ if not check_page_exists(hf_url, delay=2):
77
+ raise gr.Error("The Hugging Face username used doesn't exist. An account is required to participate.")
78
  return v
79
 
80
  @field_validator("display_name")
utils.py CHANGED
@@ -7,6 +7,41 @@ from about import results_repo_validation, results_repo_test
7
  from about import METRICS, STANDARD_COLS
8
  from loguru import logger
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def make_user_clickable(name: str):
11
  link =f'https://huggingface.co/{name}'
12
  return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{name}</a>'
 
7
  from about import METRICS, STANDARD_COLS
8
  from loguru import logger
9
 
10
+ def check_page_exists(url: str, delay=0.2):
11
+ """Checks if a web page exists at the given URL.
12
+
13
+ Parameters
14
+ ----------
15
+ url : str
16
+ Url of the page
17
+ delay : float, optional
18
+ Seconds to wait until submitting another request, by default 0.2
19
+
20
+ Returns
21
+ -------
22
+ bool
23
+ If the page exists
24
+ """
25
+ import requests
26
+ import time
27
+ try:
28
+ response = requests.get(url, timeout=5)
29
+
30
+ if response.status_code == 429:
31
+ print(f"Warning: Rate limit hit on {url}. Waiting for 5 seconds...")
32
+ time.sleep(5)
33
+ return check_page_exists(url, delay=delay)
34
+
35
+ return response.status_code == 200
36
+
37
+ except requests.exceptions.RequestException as e:
38
+ print(f"Error checking URL {url}: {e}")
39
+ return False
40
+
41
+ finally:
42
+ # Sleep after every request to avoid HTTPS error
43
+ time.sleep(delay)
44
+
45
  def make_user_clickable(name: str):
46
  link =f'https://huggingface.co/{name}'
47
  return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{name}</a>'