File size: 7,171 Bytes
02be4dc
c63233e
f438fdb
02be4dc
6f2b708
02be4dc
 
 
5abd884
f438fdb
 
 
 
 
 
 
5abd884
02be4dc
 
5abd884
 
4ce52ca
02be4dc
 
 
 
 
 
 
54fc32a
02be4dc
 
 
 
4ce52ca
 
02be4dc
4ce52ca
02be4dc
 
 
0c6044d
4ce52ca
02be4dc
4ce52ca
02be4dc
4ce52ca
e3ad135
23af3c5
4ce52ca
02be4dc
4ce52ca
02be4dc
0c6044d
02be4dc
0c6044d
 
4ce52ca
 
 
 
 
 
 
 
 
 
 
 
5abd884
 
0c6044d
 
 
 
 
4ce52ca
5abd884
0c6044d
 
 
 
 
4ce52ca
0c6044d
4ce52ca
 
 
0c6044d
4ce52ca
0c6044d
 
 
 
 
 
4ce52ca
 
 
 
 
 
 
 
 
 
 
 
 
0c6044d
4cf7ecf
4ce52ca
 
 
 
 
 
 
 
 
 
 
0c6044d
 
4cf7ecf
0c6044d
4ce52ca
 
 
 
 
 
 
 
 
 
 
0c6044d
 
4ce52ca
5abd884
 
 
0c6044d
5abd884
 
8d9fbc8
5abd884
8d9fbc8
5abd884
 
 
4ce52ca
5abd884
 
8d9fbc8
5abd884
 
4ce52ca
5abd884
4ce52ca
5abd884
 
 
4ce52ca
02be4dc
 
 
4ce52ca
02be4dc
 
4ce52ca
0c6044d
02be4dc
 
5abd884
02be4dc
6f2b708
02be4dc
0c6044d
 
5abd884
 
3036563
5abd884
02be4dc
f438fdb
 
 
 
5abd884
3036563
5abd884
 
0c6044d
5abd884
 
 
3036563
5abd884
0c6044d
 
4ce52ca
0c6044d
02be4dc
 
 
 
 
 
 
5abd884
0c6044d
 
 
5abd884
 
f438fdb
 
 
 
0c6044d
5abd884
6c25482
 
 
 
 
4ce52ca
5abd884
 
02be4dc
0c6044d
4ce52ca
5abd884
02be4dc
 
 
 
0c6044d
02be4dc
 
5abd884
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# app dashboard from https://huggingface.co/spaces/davanstrien/argilla-progress/blob/main/app.py
import os
from typing import List, Optional

import argilla as rg
import gradio as gr
import pandas as pd
import plotly.graph_objects as go

os.environ["ARGILLA_API_URL"] = (
    "https://data-is-better-together-image-preferences.hf.space/"
)
os.environ["ARGILLA_API_KEY"] = (
    "AbwS-x_-ttKOsoxIi-tbX9MdOBzSBvCFHe91QWKaAtrFo2KppKpl1gSoG-wCBSASWVJzNBLxKaBPzo7SVxnvGUAxmytsbQ5SlJREGbZVB1o"
)

client = rg.Argilla(
    api_url=os.getenv("ARGILLA_API_URL"),
    api_key=os.getenv("ARGILLA_API_KEY"),
)


def get_progress(dataset: rg.Dataset) -> dict:
    dataset_progress = dataset.progress(with_users_distribution=True)

    total, completed = dataset_progress["total"], dataset_progress["completed"]
    progress = (completed / total) * 100 if total > 0 else 0

    return {
        "min_submitted": int(dataset.settings.distribution.min_submitted),
        "total": total,
        "annotated": completed,
        "progress": progress,
        "users": {
            username: user_progress["completed"].get("submitted")
            + user_progress["pending"].get("submitted")
            for username, user_progress in dataset_progress["users"].items()
        },
    }


def create_progress_bar(progress):
    top_labels = ["Completed", "Pending"]

    colors = ["rgba(38, 24, 74, 0.8)", "rgba(190, 192, 213, 1)"]

    n_labelled_records = sum(progress["users"].values())
    # n_target_records = progress["total"]*progress["min_submitted"]
    n_target_records = 30000
    x_data = [[n_labelled_records, n_target_records - n_labelled_records]]

    y_data = ["Progress"]

    fig = go.Figure()

    for i in range(0, len(x_data[0])):
        for xd, yd in zip(x_data, y_data):
            fig.add_trace(
                go.Bar(
                    x=[xd[i]],
                    y=[yd],
                    orientation="h",
                    marker=dict(
                        color=colors[i], line=dict(color="rgb(248, 248, 249)", width=1)
                    ),
                    hoverinfo="text",
                    hovertext=f"{top_labels[i]} records: {xd[i]}",
                )
            )

    fig.update_layout(
        xaxis=dict(
            showgrid=False,
            showline=False,
            showticklabels=False,
            zeroline=False,
            domain=[0.15, 1],
        ),
        yaxis=dict(
            showgrid=False,
            showline=False,
            showticklabels=False,
            zeroline=False,
            domain=[0.15, 0.5],
        ),
        barmode="stack",
        paper_bgcolor="rgb(248, 248, 255)",
        plot_bgcolor="rgb(248, 248, 255)",
        margin=dict(l=120, r=10, t=140, b=80),
        showlegend=False,
    )

    annotations = []

    for yd, xd in zip(y_data, x_data):
        # labeling the y-axis
        annotations.append(
            dict(
                xref="paper",
                yref="y",
                x=0.14,
                y=yd,
                xanchor="right",
                text=str(yd),
                font=dict(family="Arial", size=14, color="rgb(67, 67, 67)"),
                showarrow=False,
                align="right",
            )
        )
        # labeling the first percentage of each bar (x_axis)
        if xd[0] > 0:
            annotations.append(
                dict(
                    xref="x",
                    yref="y",
                    x=xd[0] / 2,
                    y=yd,
                    text=str(xd[0]),
                    font=dict(family="Arial", size=14, color="rgb(248, 248, 255)"),
                    showarrow=False,
                )
            )
        space = xd[0]
        for i in range(1, len(xd)):
            if xd[i] > 0:
                # labeling the rest of percentages for each bar (x_axis)
                annotations.append(
                    dict(
                        xref="x",
                        yref="y",
                        x=space + (xd[i] / 2),
                        y=yd,
                        text=str(xd[i]),
                        font=dict(family="Arial", size=14, color="rgb(248, 248, 255)"),
                        showarrow=False,
                    )
                )
                space += xd[i]

    fig.update_layout(annotations=annotations, height=80)
    return fig


def create_piechart(user_annotations):
    sorted_users = sorted(user_annotations.items(), key=lambda x: x[1], reverse=True)

    labels, values = [], []

    for user, contribution in sorted_users:
        labels.append(user)
        values.append(contribution)

    fig = go.Figure(go.Pie(labels=labels, values=values))

    fig.update_layout(
        title_text="User contributions",
        height=500,
        margin=dict(l=10, r=10, t=50, b=10),
        template="ggplot2",
    )
    fig.update_traces(textposition="inside", textinfo="percent+label")

    return fig


def get_datasets(client: rg.Argilla) -> List[rg.Dataset]:
    return client.datasets.list()


datasets = get_datasets(client)


def update_dashboard(dataset_idx: Optional[int] = None):
    if dataset_idx is None:
        return [None, None, None]

    dataset = datasets[dataset_idx]
    progress = get_progress(dataset)

    progress_bar = create_progress_bar(progress)
    piechart = create_piechart(progress["users"])

    leaderboard_df = pd.DataFrame(
        list(progress["users"].items()), columns=["User", "Submitted records"]
    )

    leaderboard_df["User"] = leaderboard_df["User"].apply(
        lambda x: f"[{x}](https://huggingface.co/{x})"
    )

    leaderboard_df = leaderboard_df.sort_values(
        "Submitted records", ascending=False
    ).reset_index(drop=True)

    return progress_bar, piechart, leaderboard_df


with gr.Blocks() as demo:
    gr.Markdown("# Argilla Progress Dashboard")

    dataset_choices = [(dataset.name, idx) for idx, dataset in enumerate(datasets)]
    datasets_dropdown = gr.Dropdown(
        choices=dataset_choices, label="Select your dataset", value=0, visible=True
    )

    def set_selected_dataset(dataset_idx) -> None:
        global selected_dataset

        dataset = datasets[dataset_idx]
        selected_dataset = dataset

    with gr.Row():
        progress_bar_output = gr.Plot(label="Overall Progress")

    gr.Markdown("## Contributor Leaderboard")

    with gr.Row():
        leaderboard_output = gr.Dataframe(
            headers=["User", "Submitted records"],
            datatype=["markdown", "markdown", "html"],
        )
        piechart_output = gr.Plot(label="User contributions")

    demo.load(
        update_dashboard,
        inputs=[datasets_dropdown],
        outputs=[progress_bar_output, piechart_output, leaderboard_output],
    )

    demo.load(
        update_dashboard,
        inputs=[datasets_dropdown],
        outputs=[progress_bar_output, piechart_output, leaderboard_output],
        # every=60,
    )

    datasets_dropdown.change(
        update_dashboard,
        inputs=[datasets_dropdown],
        outputs=[progress_bar_output, piechart_output, leaderboard_output],
    )


if __name__ == "__main__":
    demo.launch()