File size: 1,704 Bytes
63bdadc
 
3edbc93
63bdadc
 
 
 
9021dc4
63bdadc
3edbc93
 
 
 
9021dc4
 
50e75cf
92d0002
 
63bdadc
3edbc93
63bdadc
 
 
 
3edbc93
63bdadc
9021dc4
63bdadc
3edbc93
 
63bdadc
 
9021dc4
63bdadc
9021dc4
63bdadc
 
3edbc93
63bdadc
92d0002
9021dc4
63bdadc
 
 
 
 
 
 
 
 
 
 
3edbc93
63bdadc
3edbc93
63bdadc
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
from pathlib import Path
import tempfile
from typing import BinaryIO
import json

import gradio as gr
from datetime import datetime
import uuid

from about import API, submissions_repo

def make_submission(
    submitted_file: BinaryIO,
    user_state,
    anonymous_state):

    if user_state is None:
        raise gr.Error("You must submit your username to submit a file.")
    
    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)
    timestamp = datetime.utcnow().isoformat()
    submission_id = uuid.uuid4()

    with (path_obj.open("rb") as f_in):
        file_content = f_in.read().decode("utf-8")

        # write to dataset
        filename = f"{submission_id}.json"
        record = {
            "submission_id": submission_id,
            "submission_filename": filename,
            "submission_time": timestamp,
            "csv_content": file_content,
            "evaluated": False,
            "user": user_state,
            "anonymous": anonymous_state
        }
        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."