|  | from pathlib import Path | 
					
						
						|  | import tempfile | 
					
						
						|  | from typing import BinaryIO | 
					
						
						|  | import json | 
					
						
						|  |  | 
					
						
						|  | import gradio as gr | 
					
						
						|  | from datetime import datetime | 
					
						
						|  | import uuid | 
					
						
						|  |  | 
					
						
						|  | from constants import API, SUBMISSIONS_REPO, ANONYMOUS_SUBMISSION_USERNAME | 
					
						
						|  | from validation import validate_csv_file, validate_username | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | def make_submission( | 
					
						
						|  | submitted_file: BinaryIO, | 
					
						
						|  | user_state, | 
					
						
						|  | submission_type: str = "GDPa1", | 
					
						
						|  | model_name: str = "", | 
					
						
						|  | model_description: str = "", | 
					
						
						|  | ): | 
					
						
						|  | user_state = user_state or ANONYMOUS_SUBMISSION_USERNAME | 
					
						
						|  | 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: | 
					
						
						|  | raise gr.Error("Please provide a model description.") | 
					
						
						|  | 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.") | 
					
						
						|  |  | 
					
						
						|  | timestamp = datetime.utcnow().isoformat() | 
					
						
						|  | submission_id = str(uuid.uuid4()) | 
					
						
						|  |  | 
					
						
						|  | with path_obj.open("rb") as f_in: | 
					
						
						|  | file_content = f_in.read().decode("utf-8") | 
					
						
						|  |  | 
					
						
						|  | validate_csv_file(file_content, submission_type) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | filename = f"{submission_id}.json" | 
					
						
						|  |  | 
					
						
						|  | 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, | 
					
						
						|  | } | 
					
						
						|  | 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() | 
					
						
						|  |  | 
					
						
						|  | return "✅ Your submission has been received! Sit tight and your scores will appear on the leaderboard shortly." | 
					
						
						|  |  |