submission-form / utils.py
lewtun's picture
lewtun HF staff
Refactor to mathc new AutoTrain API
1b95f45
import json
import jsonschema
import requests
def load_schema():
"""Load the GEM schema"""
with open("schema.json", "r", encoding="utf8") as file:
schema = json.load(file)
return schema
def validate_json(json_data):
execute_api_schema = load_schema()
try:
jsonschema.validate(instance=json_data, schema=execute_api_schema)
except jsonschema.exceptions.ValidationError as err:
err = "❌ Submission does not match GEM schema. Please fix the submission file πŸ™ˆ"
return False, err
message = "βœ… Submission matches GEM schema!"
return True, message
def get_auth_headers(token: str, prefix: str = "autonlp"):
return {"Authorization": f"{prefix} {token}"}
def http_post(path: str, token: str, payload=None, domain: str = None, params=None) -> requests.Response:
"""HTTP POST request to the AutoNLP API, raises UnreachableAPIError if the API cannot be reached"""
try:
response = requests.post(
url=domain + path, json=payload, headers=get_auth_headers(token=token), allow_redirects=True, params=params
)
except requests.exceptions.ConnectionError:
print("❌ Failed to reach AutoNLP API, check your internet connection")
response.raise_for_status()
return response
def http_get(
path: str,
token: str,
domain: str = None,
) -> requests.Response:
"""HTTP POST request to the AutoNLP API, raises UnreachableAPIError if the API cannot be reached"""
try:
response = requests.get(url=domain + path, headers=get_auth_headers(token=token), allow_redirects=True)
except requests.exceptions.ConnectionError:
print("❌ Failed to reach AutoNLP API, check your internet connection")
response.raise_for_status()
return response