Commit
Β·
c154927
1
Parent(s):
c1d0fb4
Refactor Gradio interface to enhance submission handling and improve user experience; update layout, add search functionality, and streamline data processing.
Browse files- gradio_interface.py +42 -64
gradio_interface.py
CHANGED
@@ -24,8 +24,7 @@ print("Account token used to connect to HuggingFace: ", whoami()['name'])
|
|
24 |
|
25 |
SUBMISSION_REPO = "SimulaMet/medvqa-submissions"
|
26 |
hub_path = None
|
27 |
-
|
28 |
-
submissions = None # [{"user": u, "task": t, "submitted_time": ts}]
|
29 |
last_submission_update_time = datetime.now(timezone.utc)
|
30 |
|
31 |
|
@@ -35,30 +34,28 @@ def refresh_submissions():
|
|
35 |
shutil.rmtree(hub_path, ignore_errors=True)
|
36 |
print("Deleted existing submissions")
|
37 |
|
38 |
-
hub_path = snapshot_download(
|
39 |
-
|
40 |
-
print("Downloaded submissions to:
|
41 |
if not os.path.exists(hub_path):
|
42 |
-
os.makedirs(hub_path)
|
43 |
-
|
44 |
all_jsons = glob.glob(hub_path + "/**/*.json", recursive=True)
|
45 |
json_files = [f.split("/")[-1] for f in all_jsons]
|
46 |
print("json_files count:", len(json_files))
|
|
|
47 |
submissions = []
|
48 |
for file in json_files:
|
49 |
username, sub_timestamp, task = file.replace(
|
50 |
".json", "").split("-_-_-")
|
51 |
submissions.append({"user": username, "task": task,
|
52 |
"submitted_time": sub_timestamp})
|
53 |
-
last_submission_update_time = datetime.now(timezone.utc)
|
54 |
|
|
|
55 |
return hub_path
|
56 |
|
57 |
|
58 |
hub_path = refresh_submissions()
|
59 |
-
|
60 |
-
print(f"{SUBMISSION_REPO} downloaded to {hub_path}")
|
61 |
-
# remove strings after snapshot in hub_path
|
62 |
hub_dir = hub_path.split("snapshot")[0] + "snapshot"
|
63 |
|
64 |
|
@@ -79,82 +76,67 @@ def filter_submissions(task_type, search_query):
|
|
79 |
def display_submissions(task_type="all", search_query=""):
|
80 |
if submissions is None or ((datetime.now(timezone.utc) - last_submission_update_time).total_seconds() > 3600):
|
81 |
refresh_submissions()
|
82 |
-
print("Displaying submissions...", submissions)
|
83 |
filtered_submissions = filter_submissions(task_type, search_query)
|
84 |
-
return
|
85 |
|
86 |
|
87 |
def add_submission(file):
|
88 |
global submissions
|
89 |
try:
|
90 |
-
print("Received submission: ", file)
|
91 |
with open(file, 'r', encoding='utf-8') as f:
|
92 |
data = json.load(f)
|
93 |
|
94 |
-
|
|
|
95 |
".json", "").split("-_-_-")
|
96 |
submission_time = datetime.fromtimestamp(
|
97 |
int(sub_timestamp), tz=timezone.utc)
|
|
|
98 |
assert task in ["task1", "task2"], "Invalid task type"
|
99 |
assert len(username) > 0, "Invalid username"
|
100 |
assert submission_time < datetime.now(
|
101 |
timezone.utc), "Invalid submission time"
|
102 |
-
|
103 |
upload_file(
|
104 |
repo_type="dataset",
|
105 |
path_or_fileobj=file,
|
106 |
-
path_in_repo=task+"/"+
|
107 |
repo_id=SUBMISSION_REPO
|
108 |
)
|
109 |
refresh_submissions()
|
110 |
return "πͺππ Submissions registered successfully to the system!"
|
111 |
except Exception as e:
|
112 |
-
|
113 |
|
114 |
|
115 |
def refresh_page():
|
116 |
return "Pong! Submission server is alive! π"
|
117 |
|
118 |
|
119 |
-
# Define Gradio
|
120 |
-
output_table = gr.Dataframe(headers=[
|
121 |
-
"User", "Task", "Submitted Time"], interactive=False, value=[], scale=5,)
|
122 |
-
task_type_dropdown = gr.Dropdown(
|
123 |
-
choices=["all", "task1", "task2"],
|
124 |
-
value="all",
|
125 |
-
label="Task Type",
|
126 |
-
info="Filter submissions by Task 1 (VQA) or Task 2 (Synthetic Image Generation)"
|
127 |
-
)
|
128 |
-
search_box = gr.Textbox(
|
129 |
-
value="",
|
130 |
-
label="Search by Username",
|
131 |
-
info="Enter a username to filter specific submissions"
|
132 |
-
)
|
133 |
-
|
134 |
-
upload_button = gr.File(label="Upload JSON", file_types=["json"])
|
135 |
-
|
136 |
-
# Create a tabbed interface
|
137 |
with gr.Blocks(title="πImageCLEFmed-MEDVQA-GI 2025 Submissions π") as demo:
|
138 |
-
# gr.Markdown("""
|
139 |
-
# # Welcome to the official submission portal for the [MEDVQA-GI 2025](https://www.imageclef.org/2025/medical/vqa) challenge!
|
140 |
-
# - π [Challenge Homepage](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025) | [Register for ImageCLEF 2025](https://www.imageclef.org/2025#registration)
|
141 |
-
# - π [Submission Insructions](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#-submission-system)
|
142 |
-
# """)
|
143 |
gr.Markdown("""
|
144 |
# π Welcome to the official submission portal for the [MEDVQA-GI 2025](https://www.imageclef.org/2025/medical/vqa) challenge! π₯π§¬
|
145 |
### π [**Challenge Homepage** in GitHub](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025) | π [**Register** for ImageCLEF 2025](https://www.imageclef.org/2025#registration) | π
[**Competition Schedule**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#:~:text=Schedule) | π¦ [**Submission Instructions**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#-submission-system)π₯π₯
|
146 |
### π₯ [**Available Datasets**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#-data) | π‘ [Tasks & Example Training **Notebooks**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#-task-descriptions)π₯π₯
|
147 |
-
|
148 |
""")
|
|
|
149 |
with gr.Tab("View Submissions"):
|
150 |
-
gr.Markdown("### Submissions
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
gr.Markdown(
|
159 |
f'''
|
160 |
π Last refreshed: {last_submission_update_time.strftime('%Y-%m-%d %H:%M:%S')} UTC | π Total Submissions: {len(submissions)}
|
@@ -163,21 +145,17 @@ with gr.Blocks(title="πImageCLEFmed-MEDVQA-GI 2025 Submissions π") as demo
|
|
163 |
''')
|
164 |
|
165 |
with gr.Tab("Upload Submission", visible=False):
|
166 |
-
file_input = gr.File(label="Upload JSON", file_types=["json"])
|
167 |
-
upload_output = gr.Textbox(label="Result")
|
168 |
-
file_input.upload(add_submission,
|
169 |
-
upload_output)
|
170 |
|
171 |
with gr.Tab("Refresh API", visible=False):
|
172 |
-
gr.
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
description="Hidden interface to refresh the API."
|
179 |
-
)
|
180 |
-
demo.load(lambda: gr.update(value=[[s["user"], s["task"], s["submitted_time"]]
|
181 |
-
for s in filter_submissions("all", "")]), inputs=[], outputs=output_table)
|
182 |
|
183 |
demo.launch()
|
|
|
24 |
|
25 |
SUBMISSION_REPO = "SimulaMet/medvqa-submissions"
|
26 |
hub_path = None
|
27 |
+
submissions = None
|
|
|
28 |
last_submission_update_time = datetime.now(timezone.utc)
|
29 |
|
30 |
|
|
|
34 |
shutil.rmtree(hub_path, ignore_errors=True)
|
35 |
print("Deleted existing submissions")
|
36 |
|
37 |
+
hub_path = snapshot_download(
|
38 |
+
repo_type="dataset", repo_id=SUBMISSION_REPO, allow_patterns=['**/*.json'])
|
39 |
+
print("Downloaded submissions to:", hub_path)
|
40 |
if not os.path.exists(hub_path):
|
41 |
+
os.makedirs(hub_path)
|
42 |
+
|
43 |
all_jsons = glob.glob(hub_path + "/**/*.json", recursive=True)
|
44 |
json_files = [f.split("/")[-1] for f in all_jsons]
|
45 |
print("json_files count:", len(json_files))
|
46 |
+
|
47 |
submissions = []
|
48 |
for file in json_files:
|
49 |
username, sub_timestamp, task = file.replace(
|
50 |
".json", "").split("-_-_-")
|
51 |
submissions.append({"user": username, "task": task,
|
52 |
"submitted_time": sub_timestamp})
|
|
|
53 |
|
54 |
+
last_submission_update_time = datetime.now(timezone.utc)
|
55 |
return hub_path
|
56 |
|
57 |
|
58 |
hub_path = refresh_submissions()
|
|
|
|
|
|
|
59 |
hub_dir = hub_path.split("snapshot")[0] + "snapshot"
|
60 |
|
61 |
|
|
|
76 |
def display_submissions(task_type="all", search_query=""):
|
77 |
if submissions is None or ((datetime.now(timezone.utc) - last_submission_update_time).total_seconds() > 3600):
|
78 |
refresh_submissions()
|
|
|
79 |
filtered_submissions = filter_submissions(task_type, search_query)
|
80 |
+
return [[s["user"], s["task"], s["submitted_time"]] for s in filtered_submissions]
|
81 |
|
82 |
|
83 |
def add_submission(file):
|
84 |
global submissions
|
85 |
try:
|
|
|
86 |
with open(file, 'r', encoding='utf-8') as f:
|
87 |
data = json.load(f)
|
88 |
|
89 |
+
filename = os.path.basename(file)
|
90 |
+
username, sub_timestamp, task = filename.replace(
|
91 |
".json", "").split("-_-_-")
|
92 |
submission_time = datetime.fromtimestamp(
|
93 |
int(sub_timestamp), tz=timezone.utc)
|
94 |
+
|
95 |
assert task in ["task1", "task2"], "Invalid task type"
|
96 |
assert len(username) > 0, "Invalid username"
|
97 |
assert submission_time < datetime.now(
|
98 |
timezone.utc), "Invalid submission time"
|
99 |
+
|
100 |
upload_file(
|
101 |
repo_type="dataset",
|
102 |
path_or_fileobj=file,
|
103 |
+
path_in_repo=task + "/" + filename,
|
104 |
repo_id=SUBMISSION_REPO
|
105 |
)
|
106 |
refresh_submissions()
|
107 |
return "πͺππ Submissions registered successfully to the system!"
|
108 |
except Exception as e:
|
109 |
+
return f"β Error adding submission: {e}"
|
110 |
|
111 |
|
112 |
def refresh_page():
|
113 |
return "Pong! Submission server is alive! π"
|
114 |
|
115 |
|
116 |
+
# Define Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
with gr.Blocks(title="πImageCLEFmed-MEDVQA-GI 2025 Submissions π") as demo:
|
|
|
|
|
|
|
|
|
|
|
118 |
gr.Markdown("""
|
119 |
# π Welcome to the official submission portal for the [MEDVQA-GI 2025](https://www.imageclef.org/2025/medical/vqa) challenge! π₯π§¬
|
120 |
### π [**Challenge Homepage** in GitHub](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025) | π [**Register** for ImageCLEF 2025](https://www.imageclef.org/2025#registration) | π
[**Competition Schedule**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#:~:text=Schedule) | π¦ [**Submission Instructions**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#-submission-system)π₯π₯
|
121 |
### π₯ [**Available Datasets**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#-data) | π‘ [Tasks & Example Training **Notebooks**](https://github.com/simula/ImageCLEFmed-MEDVQA-GI-2025#-task-descriptions)π₯π₯
|
|
|
122 |
""")
|
123 |
+
|
124 |
with gr.Tab("View Submissions"):
|
125 |
+
gr.Markdown("### Filter and Search Submissions")
|
126 |
+
|
127 |
+
task_type_dropdown = gr.Dropdown(
|
128 |
+
choices=["all", "task1", "task2"], value="all", label="Task Type")
|
129 |
+
search_box = gr.Textbox(
|
130 |
+
label="Search by Username", placeholder="Enter username...")
|
131 |
+
|
132 |
+
output_table = gr.Dataframe(
|
133 |
+
headers=["User", "Task", "Submitted Time"], interactive=False, value=[])
|
134 |
+
|
135 |
+
task_type_dropdown.change(fn=display_submissions, inputs=[
|
136 |
+
task_type_dropdown, search_box], outputs=output_table)
|
137 |
+
search_box.change(fn=display_submissions, inputs=[
|
138 |
+
task_type_dropdown, search_box], outputs=output_table)
|
139 |
+
|
140 |
gr.Markdown(
|
141 |
f'''
|
142 |
π Last refreshed: {last_submission_update_time.strftime('%Y-%m-%d %H:%M:%S')} UTC | π Total Submissions: {len(submissions)}
|
|
|
145 |
''')
|
146 |
|
147 |
with gr.Tab("Upload Submission", visible=False):
|
148 |
+
file_input = gr.File(label="Upload JSON", file_types=[".json"])
|
149 |
+
upload_output = gr.Textbox(label="Upload Result")
|
150 |
+
file_input.upload(fn=add_submission,
|
151 |
+
inputs=file_input, outputs=upload_output)
|
152 |
|
153 |
with gr.Tab("Refresh API", visible=False):
|
154 |
+
refresh_button = gr.Button("Refresh")
|
155 |
+
status_output = gr.Textbox(label="Status")
|
156 |
+
refresh_button.click(fn=refresh_page, inputs=[], outputs=status_output)
|
157 |
+
|
158 |
+
demo.load(lambda: display_submissions("all", ""),
|
159 |
+
inputs=[], outputs=output_table)
|
|
|
|
|
|
|
|
|
160 |
|
161 |
demo.launch()
|