File size: 5,491 Bytes
9e4515d
 
f6d47e5
33f88d6
f9f09f4
 
f6d47e5
f9f09f4
9110c66
716f5a5
f9f09f4
12df570
c7c831b
12df570
d602371
 
9e4515d
257177b
c7c831b
c6b2561
 
 
f9f09f4
929f324
6815557
c7c831b
00aad64
 
c7c831b
b445d32
 
 
d602371
 
 
 
 
 
f9f09f4
33f88d6
 
257177b
f9f09f4
257177b
f9f09f4
 
 
441f3cf
9e4515d
c6b2561
 
9e4515d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7c831b
 
9e4515d
 
 
 
 
12df570
8d60fff
12df570
 
8d60fff
12df570
cef7c8f
12df570
441f3cf
12df570
 
716f5a5
 
12df570
 
278edea
12df570
cef7c8f
12df570
 
5d6cd88
12df570
 
 
 
 
9e4515d
 
445b2d0
1ef368a
445b2d0
 
9e4515d
 
 
 
 
 
 
 
 
 
 
b445d32
9e4515d
 
 
b445d32
9e4515d
 
 
cb3460f
 
 
 
 
 
445b2d0
 
 
 
 
 
 
 
 
9e4515d
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import gradio as gr
import json
from datetime import datetime, timezone
from huggingface_hub import upload_file, snapshot_download
import shutil
import os
import glob
from pathlib import Path
from huggingface_hub import whoami
print("Account token used to connect to HuggingFace: ", whoami()['name'])

SUBMISSION_REPO = "SushantGautam/medvqa-submissions"
hub_path = None

submissions = None  # [{"user": u, "task": t, "submitted_time": ts}]


def refresh_submissions():
    global hub_path, submissions
    if hub_path and Path(hub_path).exists():
        shutil.rmtree(hub_path, ignore_errors=True)
        print("Deleted existing submissions")

    hub_path = snapshot_download(repo_type="dataset",
                                 repo_id=SUBMISSION_REPO, allow_patterns=['**/*.json'])
    print("Downloaded submissions to: ", hub_path)
    if not os.path.exists(hub_path):
        os.makedirs(hub_path)  # empty repo case
    print("os.listdir(hub_path):", os.listdir(hub_path))
    all_jsons = glob.glob(hub_path + "/**/*.json", recursive=True)
    print("Downloaded submissions: ", all_jsons)
    json_files = [f.split("/")[-1] for f in all_jsons]
    submissions = []
    for file in json_files:
        username, sub_timestamp, task = file.replace(
            ".json", "").split("-_-_-")
        submissions.append({"user": username, "task": task,
                           "submitted_time": sub_timestamp})
    return hub_path


hub_path = refresh_submissions()

print(f"{SUBMISSION_REPO} downloaded to {hub_path}")
# remove strings after snapshot in hub_path
hub_dir = hub_path.split("snapshot")[0] + "snapshot"


def time_ago(submitted_time):
    delta = datetime.now(timezone.utc) - datetime.fromtimestamp(
        int(submitted_time) / 1000, tz=timezone.utc)
    if delta.days > 0:
        return f"{delta.days} days ago"
    elif delta.seconds // 3600 > 0:
        return f"{delta.seconds // 3600} hours ago"
    elif delta.seconds // 60 > 0:
        return f"{delta.seconds // 60} minutes ago"
    else:
        return "just now"


def filter_submissions(task_type, search_query):
    if search_query == "":
        filtered = [s for s in submissions if task_type ==
                    "all" or s["task"] == task_type]
    else:
        filtered = [s for s in submissions if (
            task_type == "all" or s["task"] == task_type) and search_query.lower() in s["user"].lower()]
    return [{"user": s["user"], "task": s["task"], "submitted_time": time_ago(s["submitted_time"])} for s in filtered]


def display_submissions(task_type="all", search_query=""):
    if submissions is None:
        refresh_submissions()
    filtered_submissions = filter_submissions(task_type, search_query)
    return [[s["user"], s["task"], s["submitted_time"]] for s in filtered_submissions]


def add_submission(file):
    try:
        print("Received submission: ", file)
        with open(file, 'r', encoding='utf-8') as f:
            data = json.load(f)

        username, sub_timestamp, task = file.replace(
            ".json", "").split("-_-_-")
        submission_time = datetime.fromtimestamp(
            int(sub_timestamp) / 1000, tz=timezone.utc)
        assert task in ["task1", "task2"], "Invalid task type"
        assert len(username) > 0, "Invalid username"
        assert submission_time < datetime.now(
            timezone.utc), "Invalid submission time"
        print("Adding submission...", username, task, submission_time)
        upload_file(
            repo_type="dataset",
            path_or_fileobj=file,
            path_in_repo=task+"/"+file.split("/")[-1],
            repo_id=SUBMISSION_REPO
        )
        refresh_submissions()
        submissions.append(
            {"user": username, "task": task, "submitted_time": submission_time})
        return "πŸ’ͺπŸ†πŸŽ‰ Submissions added successfully! Visit this URL ⬆️ to see the entry."
    except Exception as e:
        raise Exception(f"Error adding submission: {e}")


def refresh_page():
    return "Pong! Submission server is alive! 😊"


# Define Gradio interface components
output_table = gr.Dataframe(
    headers=["User", "Task", "Submitted Time"], value=display_submissions(), scale=5,)
task_type_dropdown = gr.Dropdown(
    choices=["all", "task1", "task2"], value="all", label="Task Type")
search_box = gr.Textbox(value="", label="Search User")
upload_button = gr.File(label="Upload JSON", file_types=["json"])

# Create a tabbed interface
with gr.Blocks(title="ImageCLEFmed-MEDVQA-GI-2025 Submissions") as demo:
    with gr.Tab("View Submissions"):
        gr.Markdown("### Submissions Table")
        gr.Interface(
            fn=display_submissions,
            inputs=[task_type_dropdown, search_box],
            outputs=output_table,
            title="ImageCLEFmed-MEDVQA-GI-2025 Submissions",
            description="Filter and search submissions by task type and user."
        )
    with gr.Tab("Upload Submission", visible=True):
        file_input = gr.File(label="Upload JSON", file_types=["json"])
        upload_output = gr.Textbox(label="Result")  # Add this line
        file_input.upload(add_submission, file_input,
                          upload_output)

    with gr.Tab("Refresh API", visible=False):
        gr.Interface(
            api_name="RefreshAPI",
            fn=refresh_page,
            inputs=[],
            outputs="text",
            title="Refresh API",
            description="Hidden interface to refresh the API."
        )

demo.launch()