idolezal commited on
Commit
d361e71
1 Parent(s): b5d384e

Added better validation of link to the model

Browse files

Because if link is without host (e.g. only "http://"), the JS of the
svelte will crash with unexpected exception.

Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -15,7 +15,7 @@ from content import (
15
  MORE_DETAILS_MARKDOWN,
16
  ABOUT_MARKDOWN,
17
  )
18
- from server import LeaderboardServer, xmlAndMarkdownEscape, xmlQuoteAttr, api
19
 
20
  HF_SPACE_TOKEN = os.environ["HF_SPACE_TOKEN"]
21
  HF_SPACE_ID = os.environ["HF_SPACE_ID"]
@@ -44,15 +44,33 @@ SUBMISSION_INPUTS = dict.fromkeys((
44
  def on_submit_pressed():
45
  return gr.update(value='Processing submission…', interactive=False)
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def validate_submission_inputs(**inputs):
48
  if any(key for key, value in inputs.items() if key != "description" and value in (None, "")):
49
  raise ValueError('Please fill in all fields (only the description field is optional)')
 
50
  if not os.path.exists(inputs["submission_file"]):
51
  raise ValueError('File does not exist')
52
- if not (inputs["link_to_model"].startswith("http://") or inputs["link_to_model"].startswith("https://")):
53
- raise ValueError('Link does not starts with "http://" or "https://"')
 
 
 
54
  if not inputs["parameters"] > 0:
55
  raise ValueError('Attribute `Parameters (B)` should be greater than zero')
 
56
  if not (inputs["input_length"] > 0 and inputs["input_length"] == int(inputs["input_length"])):
57
  raise ValueError('Attribute `Input length (# tokens)` should be greater than zero and integer type')
58
 
 
15
  MORE_DETAILS_MARKDOWN,
16
  ABOUT_MARKDOWN,
17
  )
18
+ from server import LeaderboardServer, xmlAndMarkdownEscape, xmlQuoteAttr, api, requests
19
 
20
  HF_SPACE_TOKEN = os.environ["HF_SPACE_TOKEN"]
21
  HF_SPACE_ID = os.environ["HF_SPACE_ID"]
 
44
  def on_submit_pressed():
45
  return gr.update(value='Processing submission…', interactive=False)
46
 
47
+ def validate_url(url):
48
+ try:
49
+ response = requests.get(url, timeout=5)
50
+ except requests.exceptions.ConnectTimeout:
51
+ pass
52
+ except requests.exceptions.ConnectionError as e:
53
+ return str(e).rsplit(":", 1)[-1].strip()
54
+ except Exception as e:
55
+ return str(e)
56
+ else:
57
+ if response.status_code != 200:
58
+ return f'Failed to get <{url}>. Status code: {response.status_code}'
59
+
60
  def validate_submission_inputs(**inputs):
61
  if any(key for key, value in inputs.items() if key != "description" and value in (None, "")):
62
  raise ValueError('Please fill in all fields (only the description field is optional)')
63
+
64
  if not os.path.exists(inputs["submission_file"]):
65
  raise ValueError('File does not exist')
66
+
67
+ error_msg = validate_url(inputs["link_to_model"])
68
+ if error_msg:
69
+ raise ValueError(f'Link to the model is not valid: {error_msg}')
70
+
71
  if not inputs["parameters"] > 0:
72
  raise ValueError('Attribute `Parameters (B)` should be greater than zero')
73
+
74
  if not (inputs["input_length"] > 0 and inputs["input_length"] == int(inputs["input_length"])):
75
  raise ValueError('Attribute `Input length (# tokens)` should be greater than zero and integer type')
76