Spaces:
Running
Running
| import pathlib | |
| from pathlib import Path | |
| import tempfile | |
| from typing import BinaryIO, Literal | |
| import json | |
| import gradio as gr | |
| from datetime import datetime | |
| from about import API, SUBMISSIONS_REPO | |
| def submit_boundary( | |
| problem_type: Literal["geometrical", "simple_to_build", "mhd_stable"], | |
| boundary_file: BinaryIO, | |
| user_state | |
| ) -> tuple[Literal[ | |
| "✅ Your submission has been received! Sit tight and your scores will appear on the leaderboard shortly."], str]: | |
| if user_state is None: | |
| raise gr.Error("You must submit your username to submit a file.") | |
| file_path = boundary_file.name | |
| if not file_path: | |
| raise gr.Error("Uploaded file object does not have a valid file path.") | |
| path_obj = pathlib.Path(file_path) | |
| timestamp = datetime.utcnow().isoformat() | |
| with ( | |
| path_obj.open("rb") as f_in, | |
| tempfile.NamedTemporaryFile(delete=False, suffix=".json") as tmp_boundary, | |
| ): | |
| file_content = f_in.read() | |
| tmp_boundary.write(file_content) | |
| tmp_boundary_path = pathlib.Path(tmp_boundary.name) | |
| # write to dataset | |
| filename = f"{problem_type}/{timestamp.replace(':', '-')}_{problem_type}.json" | |
| record = { | |
| "submission_filename": filename, | |
| "submission_time": timestamp, | |
| "problem_type": problem_type, | |
| "boundary_json": file_content.decode("utf-8"), | |
| "evaluated": False, | |
| "user": user_state, | |
| } | |
| with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp: | |
| json.dump(record, tmp) | |
| 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 {problem_type} at {timestamp}" | |
| ) | |
| pathlib.Path(tmp_name).unlink() | |
| tmp_boundary_path.unlink() | |
| return "✅ Your submission has been received! Sit tight and your scores will appear on the leaderboard shortly.", filename |