frascuchon HF staff commited on
Commit
02be4dc
1 Parent(s): 7c1b028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +241 -20
app.py CHANGED
@@ -1,19 +1,218 @@
 
1
  import os
2
- import datetime
3
- import pandas as pd
4
- import gradio as gr
5
  import argilla as rg
6
- import plotly.graph_objects as go
 
7
  import plotly.colors as colors
 
8
 
9
  client = rg.Argilla(
10
- api_url=os.getenv("ARGILLA_API_URL"), api_key=os.getenv("ARGILLA_API_KEY")
 
11
  )
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- def fetch_data(dataset_name: str, workspace: str):
15
- return client.datasets(dataset_name, workspace=workspace)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def get_progress(dataset: rg.Dataset) -> dict:
19
  dataset_progress = dataset.progress(with_users_distribution=True)
@@ -25,12 +224,13 @@ def get_progress(dataset: rg.Dataset) -> dict:
25
  "total": total,
26
  "annotated": completed,
27
  "progress": progress,
28
- "users": {
29
- username: user_progress["completed"].get("submitted")
30
- for username, user_progress in dataset_progress["users"].items()
31
- }
32
  }
33
 
 
34
  def create_gauge_chart(progress):
35
  fig = go.Figure(
36
  go.Indicator(
@@ -135,17 +335,25 @@ def create_treemap(user_annotations, total_records):
135
 
136
  return fig
137
 
 
 
 
 
 
 
 
 
138
 
139
- def update_dashboard():
140
- dataset = fetch_data(os.getenv("DATASET_NAME"), os.getenv("WORKSPACE"))
141
  progress = get_progress(dataset)
142
-
143
  gauge_chart = create_gauge_chart(progress)
144
  treemap = create_treemap(progress["users"], progress["total"])
145
 
146
  leaderboard_df = pd.DataFrame(
147
  list(progress["users"].items()), columns=["User", "Annotations"]
148
  )
 
149
  leaderboard_df = leaderboard_df.sort_values(
150
  "Annotations", ascending=False
151
  ).reset_index(drop=True)
@@ -153,9 +361,20 @@ def update_dashboard():
153
  return gauge_chart, treemap, leaderboard_df
154
 
155
 
 
156
  with gr.Blocks() as demo:
157
  gr.Markdown("# Argilla Dataset Dashboard")
158
 
 
 
 
 
 
 
 
 
 
 
159
  with gr.Row():
160
  gauge_output = gr.Plot(label="Overall Progress")
161
  treemap_output = gr.Plot(label="User contributions")
@@ -167,15 +386,17 @@ with gr.Blocks() as demo:
167
 
168
  demo.load(
169
  update_dashboard,
170
- inputs=None,
171
  outputs=[gauge_output, treemap_output, leaderboard_output],
172
  every=5,
173
  )
174
- #gr.Button("Refresh").click(
175
- # update_dashboard,
176
- # inputs=None,
177
- # outputs=[gauge_output, treemap_output, leaderboard_output],
178
- #)
 
 
179
 
180
  if __name__ == "__main__":
181
  demo.launch()
 
1
+ # app dashboard from https://huggingface.co/spaces/davanstrien/argilla-progress/blob/main/app.py
2
  import os
3
+ from typing import List
4
+
 
5
  import argilla as rg
6
+ import gradio as gr
7
+ import pandas as pd
8
  import plotly.colors as colors
9
+ import plotly.graph_objects as go
10
 
11
  client = rg.Argilla(
12
+ api_url=os.getenv("ARGILLA_API_URL"),
13
+ api_key=os.getenv("ARGILLA_API_KEY"),
14
  )
15
 
16
+ def get_progress(dataset: rg.Dataset) -> dict:
17
+ dataset_progress = dataset.progress(with_users_distribution=True)
18
+
19
+ total, completed = dataset_progress["total"], dataset_progress["completed"]
20
+ progress = (completed / total) * 100 if total > 0 else 0
21
+
22
+ return {
23
+ "total": total,
24
+ "annotated": completed,
25
+ "progress": progress,
26
+ "users": {
27
+ username: user_progress["completed"].get("submitted")
28
+ for username, user_progress in dataset_progress["users"].items()
29
+ }
30
+ }
31
+
32
+
33
+ def create_gauge_chart(progress):
34
+ fig = go.Figure(
35
+ go.Indicator(
36
+ mode="gauge+number+delta",
37
+ value=progress["progress"],
38
+ title={"text": "Dataset Annotation Progress", "font": {"size": 24}},
39
+ delta={"reference": 100, "increasing": {"color": "RebeccaPurple"}},
40
+ number={"font": {"size": 40}, "valueformat": ".1f", "suffix": "%"},
41
+ gauge={
42
+ "axis": {"range": [None, 100], "tickwidth": 1, "tickcolor": "darkblue"},
43
+ "bar": {"color": "deepskyblue"},
44
+ "bgcolor": "white",
45
+ "borderwidth": 2,
46
+ "bordercolor": "gray",
47
+ "steps": [
48
+ {"range": [0, progress["progress"]], "color": "royalblue"},
49
+ {"range": [progress["progress"], 100], "color": "lightgray"},
50
+ ],
51
+ "threshold": {
52
+ "line": {"color": "red", "width": 4},
53
+ "thickness": 0.75,
54
+ "value": 100,
55
+ },
56
+ },
57
+ )
58
+ )
59
+
60
+ fig.update_layout(
61
+ annotations=[
62
+ dict(
63
+ text=(
64
+ f"Total records: {progress['total']}<br>"
65
+ f"Annotated: {progress['annotated']} ({progress['progress']:.1f}%)<br>"
66
+ f"Remaining: {progress['total'] - progress['annotated']} ({100 - progress['progress']:.1f}%)"
67
+ ),
68
+ # x=0.5,
69
+ # y=-0.2,
70
+ showarrow=False,
71
+ xref="paper",
72
+ yref="paper",
73
+ font=dict(size=16),
74
+ )
75
+ ],
76
+ )
77
+
78
+ fig.add_annotation(
79
+ text=(
80
+ f"Current Progress: {progress['progress']:.1f}% complete<br>"
81
+ f"({progress['annotated']} out of {progress['total']} records annotated)"
82
+ ),
83
+ xref="paper",
84
+ yref="paper",
85
+ x=0.5,
86
+ y=1.1,
87
+ showarrow=False,
88
+ font=dict(size=18),
89
+ align="center",
90
+ )
91
+
92
+ return fig
93
+
94
+
95
+ def create_treemap(user_annotations, total_records):
96
+ sorted_users = sorted(user_annotations.items(), key=lambda x: x[1], reverse=True)
97
+ color_scale = colors.qualitative.Pastel + colors.qualitative.Set3
98
+
99
+ labels, parents, values, text, user_colors = [], [], [], [], []
100
+
101
+ for i, (user, contribution) in enumerate(sorted_users):
102
+ percentage = (contribution / total_records) * 100
103
+ labels.append(user)
104
+ parents.append("Annotations")
105
+ values.append(contribution)
106
+ text.append(f"{contribution} annotations<br>{percentage:.2f}%")
107
+ user_colors.append(color_scale[i % len(color_scale)])
108
+
109
+ labels.append("Annotations")
110
+ parents.append("")
111
+ values.append(total_records)
112
+ text.append(f"Total: {total_records} annotations")
113
+ user_colors.append("#FFFFFF")
114
+
115
+ fig = go.Figure(
116
+ go.Treemap(
117
+ labels=labels,
118
+ parents=parents,
119
+ values=values,
120
+ text=text,
121
+ textinfo="label+text",
122
+ hoverinfo="label+text+value",
123
+ marker=dict(colors=user_colors, line=dict(width=2)),
124
+ )
125
+ )
126
+
127
+ fig.update_layout(
128
+ title_text="User contributions to the total end dataset",
129
+ height=500,
130
+ margin=dict(l=10, r=10, t=50, b=10),
131
+ paper_bgcolor="#F0F0F0", # Light gray background
132
+ plot_bgcolor="#F0F0F0", # Light gray background
133
+ )
134
+
135
+ return fig
136
+
137
+ def get_datasets(client: rg.Argilla) -> List[rg.Dataset]:
138
+ return client.datasets.list()
139
+
140
+ datasets = get_datasets(client)
141
+
142
+ def update_dashboard(dataset_idx: int| None = None):
143
+ if dataset_idx is None:
144
+ return [None, None, None]
145
+
146
+ dataset = datasets[dataset_idx]
147
+ progress = get_progress(dataset)
148
+
149
+ gauge_chart = create_gauge_chart(progress)
150
+ treemap = create_treemap(progress["users"], progress["total"])
151
+
152
+ leaderboard_df = pd.DataFrame(
153
+ list(progress["users"].items()), columns=["User", "Annotations"]
154
+ )
155
+
156
+ leaderboard_df = leaderboard_df.sort_values(
157
+ "Annotations", ascending=False
158
+ ).reset_index(drop=True)
159
+
160
+ return gauge_chart, treemap, leaderboard_df
161
+
162
+
163
+
164
+ with gr.Blocks() as demo:
165
+ gr.Markdown("# Argilla Dataset Dashboard")
166
+
167
+ datasets_dropdown = gr.Dropdown(label="Select your dataset")
168
+ datasets_dropdown.choices = [(dataset.name, idx) for idx, dataset in enumerate(datasets)]
169
 
170
+ def set_selected_dataset(dataset_idx) -> None:
171
+ global selected_dataset
172
 
173
+ dataset = datasets[dataset_idx]
174
+ selected_dataset = dataset
175
+
176
+
177
+ with gr.Row():
178
+ gauge_output = gr.Plot(label="Overall Progress")
179
+ treemap_output = gr.Plot(label="User contributions")
180
+
181
+ with gr.Row():
182
+ leaderboard_output = gr.Dataframe(
183
+ label="Leaderboard", headers=["User", "Annotations"]
184
+ )
185
+
186
+ demo.load(
187
+ update_dashboard,
188
+ inputs=[datasets_dropdown],
189
+ outputs=[gauge_output, treemap_output, leaderboard_output],
190
+ every=5,
191
+ )
192
+
193
+ datasets_dropdown.change(
194
+ update_dashboard,
195
+ inputs=[datasets_dropdown],
196
+ outputs=[gauge_output, treemap_output, leaderboard_output],
197
+ )
198
+
199
+
200
+ if __name__ == "__main__":
201
+ demo.launch()
202
+ # app dashboard from https://huggingface.co/spaces/davanstrien/argilla-progress/blob/main/app.py
203
+ import os
204
+ from typing import List
205
+
206
+ import argilla as rg
207
+ import gradio as gr
208
+ import pandas as pd
209
+ import plotly.colors as colors
210
+ import plotly.graph_objects as go
211
+
212
+ client = rg.Argilla(
213
+ api_url=os.getenv("ARGILLA_API_URL"),
214
+ api_key=os.getenv("ARGILLA_API_KEY"),
215
+ )
216
 
217
  def get_progress(dataset: rg.Dataset) -> dict:
218
  dataset_progress = dataset.progress(with_users_distribution=True)
 
224
  "total": total,
225
  "annotated": completed,
226
  "progress": progress,
227
+ "users": {
228
+ username: user_progress["completed"].get("submitted")
229
+ for username, user_progress in dataset_progress["users"].items()
230
+ }
231
  }
232
 
233
+
234
  def create_gauge_chart(progress):
235
  fig = go.Figure(
236
  go.Indicator(
 
335
 
336
  return fig
337
 
338
+ def get_datasets(client: rg.Argilla) -> List[rg.Dataset]:
339
+ return client.datasets.list()
340
+
341
+ datasets = get_datasets(client)
342
+
343
+ def update_dashboard(dataset_idx: int| None = None):
344
+ if dataset_idx is None:
345
+ return [None, None, None]
346
 
347
+ dataset = datasets[dataset_idx]
 
348
  progress = get_progress(dataset)
349
+
350
  gauge_chart = create_gauge_chart(progress)
351
  treemap = create_treemap(progress["users"], progress["total"])
352
 
353
  leaderboard_df = pd.DataFrame(
354
  list(progress["users"].items()), columns=["User", "Annotations"]
355
  )
356
+
357
  leaderboard_df = leaderboard_df.sort_values(
358
  "Annotations", ascending=False
359
  ).reset_index(drop=True)
 
361
  return gauge_chart, treemap, leaderboard_df
362
 
363
 
364
+
365
  with gr.Blocks() as demo:
366
  gr.Markdown("# Argilla Dataset Dashboard")
367
 
368
+ datasets_dropdown = gr.Dropdown(label="Select your dataset")
369
+ datasets_dropdown.choices = [(dataset.name, idx) for idx, dataset in enumerate(datasets)]
370
+
371
+ def set_selected_dataset(dataset_idx) -> None:
372
+ global selected_dataset
373
+
374
+ dataset = datasets[dataset_idx]
375
+ selected_dataset = dataset
376
+
377
+
378
  with gr.Row():
379
  gauge_output = gr.Plot(label="Overall Progress")
380
  treemap_output = gr.Plot(label="User contributions")
 
386
 
387
  demo.load(
388
  update_dashboard,
389
+ inputs=[datasets_dropdown],
390
  outputs=[gauge_output, treemap_output, leaderboard_output],
391
  every=5,
392
  )
393
+
394
+ datasets_dropdown.change(
395
+ update_dashboard,
396
+ inputs=[datasets_dropdown],
397
+ outputs=[gauge_output, treemap_output, leaderboard_output],
398
+ )
399
+
400
 
401
  if __name__ == "__main__":
402
  demo.launch()