File size: 3,653 Bytes
63bdadc 3edbc93 63bdadc 10e69e7 63bdadc 6921c9e 9021dc4 63bdadc 471531b e0516fa 3edbc93 50e75cf 10e69e7 b2a1e67 10e69e7 1de74c6 b2a1e67 6921c9e 813ce52 63bdadc 10e69e7 3edbc93 8f9985e b2a1e67 63bdadc afba419 813ce52 63bdadc 9021dc4 63bdadc 92d0002 10e69e7 a7cc355 8f9985e 6a1bc0f 1de74c6 63bdadc 8f9985e 63bdadc eb50e2e 63bdadc 8f9985e 63bdadc 3edbc93 63bdadc 10e69e7 1de74c6 471531b 10e69e7 62b6599 471531b 10e69e7 1de74c6 10e69e7 3edbc93 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
from pathlib import Path
import tempfile
from typing import BinaryIO
import json
from click import pass_obj
import gradio as gr
from datetime import datetime, timezone
import uuid
from constants import API, SUBMISSIONS_REPO, REGISTRATION_CODE
from validation import validate_csv_file, validate_username
def upload_submission(
file_path: Path,
user_state,
submission_type: str,
model_name: str,
model_description: str,
anonymous: bool = False,
):
timestamp = datetime.now(timezone.utc).isoformat()
date = datetime.now(timezone.utc).date().isoformat()
submission_id = str(uuid.uuid4())
with file_path.open("rb") as f_in:
file_content = f_in.read().decode("utf-8")
validate_csv_file(file_content, submission_type)
# write to dataset
filename = f"{user_state}/{date}_{model_name}_{submission_id}.json" # Writing to a bit more structured name
# Note: This may be represented as Parquet in the future, so the schema needs to stay consistent
record = {
"submission_id": submission_id,
"submission_filename": filename,
"submission_time": timestamp,
"evaluated": False,
"user": user_state,
"model_name": model_name,
"model_description": model_description,
"csv_content": file_content,
"dataset": submission_type,
"anonymous": anonymous,
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp:
json.dump(record, tmp, indent=2)
tmp.flush()
tmp_name = tmp.name
API.upload_file(
path_or_fileobj=tmp_name,
path_in_repo=filename,
repo_id=SUBMISSIONS_REPO,
repo_type="dataset",
commit_message=f"Add submission for {user_state} at {timestamp}",
)
Path(tmp_name).unlink()
def make_submission(
submitted_file: BinaryIO,
user_state,
submission_type: str = "GDPa1",
model_name: str = "",
model_description: str = "",
anonymous: bool = False,
registration_code: str = "",
):
user_state = user_state
validate_username(user_state)
model_name = model_name.strip()
model_description = model_description.strip()
if not model_name:
raise gr.Error("Please provide a model name.")
if not model_description:
model_description = ""
# raise gr.Error("Please provide a model description.") # Not mandatory anymore
if str(registration_code).strip().upper() != REGISTRATION_CODE:
raise gr.Error("Invalid registration code. Please register on the <a href='https://datapoints.ginkgo.bio/ai-competitions/2025-abdev-competition'>Competition Registration page</a> or email <a href='mailto:[email protected]'>[email protected]</a>.")
if submitted_file is None:
raise gr.Error("Please upload a CSV file before submitting.")
file_path = submitted_file.name
if not file_path:
raise gr.Error("Uploaded file object does not have a valid file path.")
path_obj = Path(file_path)
if path_obj.suffix.lower() != ".csv":
raise gr.Error("File must be a CSV file. Please upload a .csv file.")
upload_submission(
file_path=path_obj,
user_state=user_state,
submission_type=submission_type,
model_name=model_name,
model_description=model_description,
anonymous=anonymous,
)
return "✅ Your submission has been received! Sit tight and your scores will appear on the leaderboard shortly."
|