anselp commited on
Commit
23bce77
verified
1 Parent(s): b9b8d83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -27
app.py CHANGED
@@ -1,38 +1,143 @@
1
- from pathlib import Path
2
  import gradio as gr
 
 
 
3
 
4
- demo = gr.Interface("file", "json")
 
5
 
6
- '''
7
- def upload_file(filepath):
8
- name = Path(filepath).name
9
- return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
10
 
11
- def download_file():
12
- return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]
 
 
 
 
 
 
 
13
 
14
- with gr.Blocks() as demo:
15
- gr.Markdown("First upload a file and and then you'll be able download it (but only once!)")
16
- with gr.Row():
17
- u = gr.UploadButton("Upload a file", file_count="single")
18
- d = gr.DownloadButton("Download the file", visible=False)
 
 
 
19
 
20
- u.upload(upload_file, u, [u, d])
21
- d.click(download_file, None, [u, d])
22
- '''
 
 
 
23
 
24
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
 
26
 
27
- '''
28
- def greet(name, intensity):
29
- return "Hello, " + name + "!" * int(intensity)
30
 
31
- demo = gr.Interface(
32
- fn=greet,
33
- inputs=["text", "slider"],
34
- outputs=["text"],
35
- )
36
 
37
- demo.launch()
38
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import json
3
+ import os
4
+ import random
5
 
6
+ # Nombre del archivo JSON para almacenar la informaci贸n de la leaderboard
7
+ JSON_FILE = "dipromats2024-t2_leaderboard-data.json"
8
 
9
+ # Funci贸n para guardar los datos en el json leaderboard
10
+ def save_data(data):
11
+ with open(JSON_FILE, "w") as f:
12
+ json.dump(data, f, indent=4)
13
 
14
+ # Funci贸n para cargar los datos del json del leaderboard
15
+ def load_data():
16
+ if os.path.exists(JSON_FILE):
17
+ with open(JSON_FILE, "r") as f:
18
+ try:
19
+ return json.load(f)
20
+ except json.JSONDecodeError:
21
+ return []
22
+ return []
23
 
24
+ # Funci贸n para convertir el json del leaderboard en tabla
25
+ def update_table():
26
+ data = load_data()
27
+ table_data = []
28
+ for item in data:
29
+ table_data.append([item.get("team_name", ""), item.get("run_id", ""),
30
+ item.get("lenient_f1", ""), item.get("strict_f1", ""), item.get("average_f1", "")])
31
+ return table_data
32
 
33
+ # Funci贸n para evaluar los resultados
34
+ def evaluate_results(file_path):
35
+ lenient_f1=random.random()
36
+ strict_f1=random.random()
37
+ average_f1=(lenient_f1+strict_f1)/2
38
+ return lenient_f1, strict_f1, average_f1
39
 
40
+ # Funci贸n para procesar el archivo de resultados
41
+ def process_file(file_path, team_input, run_id, description, email):
42
+ warn=False
43
+ if not file_path:
44
+ gr.Warning("File cannot be blank")
45
+ warn=True
46
+ if not team_input:
47
+ gr.Warning("Team name cannot be blank")
48
+ warn=True
49
+ if not run_id:
50
+ gr.Warning("Run ID cannot be blank")
51
+ warn=True
52
+ if not file_path:
53
+ gr.Warning("File cannot be blank")
54
+ warn=True
55
+ if not description:
56
+ gr.Warning("Description cannot be blank")
57
+ warn=True
58
+ if not email:
59
+ gr.Warning("Email cannot be blank")
60
+ warn=True
61
+
62
+ if warn:
63
+ return gr.Button(visible=True), gr.Button(visible=False), None, None, None
64
 
65
+ lenient_f1, strict_f1, average_f1 = evaluate_results(file_path)
66
 
67
+ return gr.Button(visible=False), gr.Button(visible=True), lenient_f1, strict_f1, average_f1
 
 
68
 
 
 
 
 
 
69
 
70
+ # Funci贸n para subir los resultados al leaderboard
71
+ def update_leaderboard(email, team_input, run_id, description, lenient_f1, strict_f1, average_f1):
72
+ data = load_data()
73
+ data.append({
74
+ "email": email,
75
+ "team_name": team_input,
76
+ "run_id": run_id,
77
+ "description": description,
78
+ "lenient_f1": lenient_f1,
79
+ "strict_f1": strict_f1,
80
+ "average_f1": average_f1
81
+ })
82
+ save_data(data)
83
+ return update_table(), gr.Tabs(selected=0), gr.Button(visible=True), gr.Button(visible=False), "", "", "", "", None, None, None, None # Clear inputs
84
+
85
+ # Main
86
+
87
+ with gr.Blocks() as leaderboard:
88
+ gr.Markdown(
89
+ """
90
+ # Dipromats 2024 Task 2 Leaderboard
91
+ # Automatic Detection of Narratives from Diplomats of Major Powers
92
+ This is...
93
+ You can...
94
+ """)
95
+ with gr.Tabs() as tabs:
96
+
97
+ # Tab Leaderboard
98
+ with gr.TabItem("Leaderboard", id=0):
99
+ gr.Markdown(
100
+ """
101
+ #
102
+ # Leaderboard
103
+ """)
104
+ leaderboard_table = gr.Dataframe(headers=["Team", "Run ID", "Lenient F1", "Strict F1", "Average F1"],
105
+ value=update_table(),
106
+ interactive=False)
107
+
108
+ # Tab Evaluate
109
+ with gr.TabItem("Evaluate your results", id=1):
110
+ gr.Markdown(
111
+ """
112
+ # Upload your results and get evaluated
113
+ Then you can decide to submit your results to the leaderboard or not.
114
+ Make sure that you upload a file with the json format described in...
115
+ """)
116
+
117
+ # Submission Form
118
+ with gr.Row():
119
+ with gr.Column():
120
+ with gr.Row():
121
+ team_input = gr.Textbox(label="Team Name")
122
+ run_id = gr.Textbox(label="Run ID")
123
+ email_input = gr.Textbox(label="Email (only for submission verification, it won't be shown)")
124
+ description_input = gr.Textbox(label="System description", lines=6)
125
+ file_input = gr.File(label="Upload a JSON file", file_types=[".json"], type="filepath", file_count="single")
126
+ evaluate_button = gr.Button("Evaluate")
127
+
128
+ # System results table
129
+ with gr.Row(visible=True):
130
+ lenient_f1 = gr.Number(label="Lenient F1", interactive=False)
131
+ strict_f1 = gr.Number(label="Strict F1", interactive=False)
132
+ average_f1 = gr.Number(label="Average F1", interactive=False)
133
+
134
+ # Submit to leaderboard
135
+ submit_button = gr.Button("Submit to leaderboard", visible=False)
136
+
137
+ evaluate_button.click(process_file,
138
+ inputs=[file_input, team_input, run_id, description_input, email_input],
139
+ outputs=[evaluate_button,submit_button,lenient_f1, strict_f1, average_f1])
140
+ submit_button.click(update_leaderboard,
141
+ inputs=[email_input, team_input, run_id, description_input, lenient_f1, strict_f1, average_f1],
142
+ outputs=[leaderboard_table, tabs, evaluate_button, submit_button, team_input, run_id, description_input, email_input, file_input,lenient_f1, strict_f1, average_f1])
143
+ leaderboard.launch()