eren23 commited on
Commit
054b6b7
β€’
1 Parent(s): 3420c0e

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +7 -8
  2. app.py +375 -0
  3. dumpy.py +52 -0
  4. requirements.txt +72 -0
README.md CHANGED
@@ -1,12 +1,11 @@
1
  ---
2
- title: Turkish Translation Dashboard
3
- emoji: πŸ†
4
- colorFrom: green
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 4.23.0
8
  app_file: app.py
9
  pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Template for Dashboards - Multilingual Prompt Evaluation Project
3
+ emoji: πŸ“Š
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.21.0
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
+ ---
 
app.py ADDED
@@ -0,0 +1,375 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from apscheduler.schedulers.background import BackgroundScheduler
2
+ import datetime
3
+ import os
4
+ from typing import Dict, Tuple
5
+ from uuid import UUID
6
+
7
+ import altair as alt
8
+ import argilla as rg
9
+ from argilla.feedback import FeedbackDataset
10
+ from argilla.client.feedback.dataset.remote.dataset import RemoteFeedbackDataset
11
+ import gradio as gr
12
+ import pandas as pd
13
+
14
+ """
15
+ This is the main file for the dashboard application. It contains the main function and the functions to obtain the data and create the charts.
16
+ It's designed as a template to recreate the dashboard for the prompt translation project of any language.
17
+
18
+ To create a new dashboard, you need several environment variables, that you can easily set in the HuggingFace Space that you are using to host the dashboard:
19
+
20
+ - SOURCE_DATASET: The dataset id of the source dataset
21
+ - SOURCE_WORKSPACE: The workspace id of the source dataset
22
+ - TARGET_RECORDS: The number of records that you have as a target to annotate. We usually set this to 500.
23
+ - ARGILLA_API_URL: Link to the Huggingface Space where the annotation effort is being hosted. For example, the Spanish one is https://somosnlp-dibt-prompt-translation-for-es.hf.space/
24
+ - ARGILLA_API_KEY: The API key to access the Huggingface Space. Please, write this as a secret in the Huggingface Space configuration.
25
+ """
26
+
27
+ # Translation of legends and titles
28
+ ANNOTATED = "İşlenenler"
29
+ NUMBER_ANNOTATED = "Toplam İşlenen"
30
+ PENDING = "Bekleyen"
31
+
32
+ NUMBER_ANNOTATORS = "Etiketleyenlerin SayΔ±sΔ±"
33
+ NAME = "KullanΔ±cΔ± AdΔ±"
34
+ NUMBER_ANNOTATIONS = "İşaretlemelerin Sayısı"
35
+
36
+ CATEGORY = "Category"
37
+
38
+
39
+ def obtain_source_target_datasets() -> (
40
+ Tuple[
41
+ FeedbackDataset | RemoteFeedbackDataset, FeedbackDataset | RemoteFeedbackDataset
42
+ ]
43
+ ):
44
+ """
45
+ This function returns the source and target datasets to be used in the application.
46
+
47
+ Returns:
48
+ A tuple with the source and target datasets. The source dataset is filtered by the response status 'pending'.
49
+
50
+ """
51
+
52
+ # Obtain the public dataset and see how many pending records are there
53
+ source_dataset = rg.FeedbackDataset.from_argilla(
54
+ os.getenv("SOURCE_DATASET"), workspace=os.getenv("SOURCE_WORKSPACE")
55
+ )
56
+ filtered_source_dataset = source_dataset.filter_by(response_status=["pending"])
57
+
58
+ # Obtain a list of users from the private workspace
59
+ # target_dataset = rg.FeedbackDataset.from_argilla(
60
+ # os.getenv("RESULTS_DATASET"), workspace=os.getenv("RESULTS_WORKSPACE")
61
+ # )
62
+
63
+ target_dataset = source_dataset.filter_by(response_status=["submitted"])
64
+
65
+ return filtered_source_dataset, target_dataset
66
+
67
+
68
+ def get_user_annotations_dictionary(
69
+ dataset: FeedbackDataset | RemoteFeedbackDataset,
70
+ ) -> Dict[str, int]:
71
+ """
72
+ This function returns a dictionary with the username as the key and the number of annotations as the value.
73
+
74
+ Args:
75
+ dataset: The dataset to be analyzed.
76
+ Returns:
77
+ A dictionary with the username as the key and the number of annotations as the value.
78
+ """
79
+ output = {}
80
+ for record in dataset:
81
+ for response in record.responses:
82
+ if str(response.user_id) not in output.keys():
83
+ output[str(response.user_id)] = 1
84
+ else:
85
+ output[str(response.user_id)] += 1
86
+
87
+ # Changing the name of the keys, from the id to the username
88
+ for key in list(output.keys()):
89
+ output[rg.User.from_id(UUID(key)).username] = output.pop(key)
90
+
91
+ return output
92
+
93
+
94
+ def donut_chart_total() -> alt.Chart:
95
+ """
96
+ This function returns a donut chart with the progress of the total annotations.
97
+ Counts each record that has been annotated at least once.
98
+
99
+ Returns:
100
+ An altair chart with the donut chart.
101
+ """
102
+
103
+ # Load your data
104
+ annotated_records = len(target_dataset)
105
+ pending_records = int(os.getenv("TARGET_RECORDS")) - annotated_records
106
+
107
+ # Prepare data for the donut chart
108
+ source = pd.DataFrame(
109
+ {
110
+ "values": [annotated_records, pending_records],
111
+ "category": [ANNOTATED, PENDING],
112
+ "colors": [
113
+ "#4682b4",
114
+ "#e68c39",
115
+ ], # Blue for Completed, Orange for Remaining
116
+ }
117
+ )
118
+
119
+ domain = source["category"].tolist()
120
+ range_ = source["colors"].tolist()
121
+
122
+ base = alt.Chart(source).encode(
123
+ theta=alt.Theta("values:Q", stack=True),
124
+ radius=alt.Radius(
125
+ "values", scale=alt.Scale(type="sqrt", zero=True, rangeMin=20)
126
+ ),
127
+ color=alt.Color(
128
+ field="category",
129
+ type="nominal",
130
+ scale=alt.Scale(domain=domain, range=range_),
131
+ legend=alt.Legend(title=CATEGORY),
132
+ ),
133
+ )
134
+
135
+ c1 = base.mark_arc(innerRadius=20, stroke="#fff")
136
+
137
+ c2 = base.mark_text(radiusOffset=20).encode(text="values:Q")
138
+
139
+ chart = c1 + c2
140
+
141
+ return chart
142
+
143
+
144
+ def kpi_chart_remaining() -> alt.Chart:
145
+ """
146
+ This function returns a KPI chart with the remaining amount of records to be annotated.
147
+ Returns:
148
+ An altair chart with the KPI chart.
149
+ """
150
+
151
+ pending_records = int(os.getenv("TARGET_RECORDS")) - len(target_dataset)
152
+ # Assuming you have a DataFrame with user data, create a sample DataFrame
153
+ data = pd.DataFrame({"Category": [PENDING], "Value": [pending_records]})
154
+
155
+ # Create Altair chart
156
+ chart = (
157
+ alt.Chart(data)
158
+ .mark_text(fontSize=100, align="center", baseline="middle", color="#e68b39")
159
+ .encode(text="Value:N")
160
+ .properties(title=PENDING, width=250, height=200)
161
+ )
162
+
163
+ return chart
164
+
165
+
166
+ def kpi_chart_submitted() -> alt.Chart:
167
+ """
168
+ This function returns a KPI chart with the total amount of records that have been annotated.
169
+ Returns:
170
+ An altair chart with the KPI chart.
171
+ """
172
+
173
+ total = len(target_dataset)
174
+
175
+ # Assuming you have a DataFrame with user data, create a sample DataFrame
176
+ data = pd.DataFrame({"Category": [NUMBER_ANNOTATED], "Value": [total]})
177
+
178
+ # Create Altair chart
179
+ chart = (
180
+ alt.Chart(data)
181
+ .mark_text(fontSize=100, align="center", baseline="middle", color="steelblue")
182
+ .encode(text="Value:N")
183
+ .properties(title=NUMBER_ANNOTATED, width=250, height=200)
184
+ )
185
+
186
+ return chart
187
+
188
+
189
+ def kpi_chart_total_annotators() -> alt.Chart:
190
+ """
191
+ This function returns a KPI chart with the total amount of annotators.
192
+
193
+ Returns:
194
+ An altair chart with the KPI chart.
195
+ """
196
+
197
+ # Obtain the total amount of annotators
198
+ total_annotators = len(user_ids_annotations)
199
+
200
+ # Assuming you have a DataFrame with user data, create a sample DataFrame
201
+ data = pd.DataFrame({"Category": [NUMBER_ANNOTATORS], "Value": [total_annotators]})
202
+
203
+ # Create Altair chart
204
+ chart = (
205
+ alt.Chart(data)
206
+ .mark_text(fontSize=100, align="center", baseline="middle", color="steelblue")
207
+ .encode(text="Value:N")
208
+ .properties(title=NUMBER_ANNOTATORS, width=250, height=200)
209
+ )
210
+
211
+ return chart
212
+
213
+
214
+ def render_hub_user_link(hub_id: str) -> str:
215
+ """
216
+ This function returns a link to the user's profile on Hugging Face.
217
+
218
+ Args:
219
+ hub_id: The user's id on Hugging Face.
220
+
221
+ Returns:
222
+ A string with the link to the user's profile on Hugging Face.
223
+ """
224
+ link = f"https://huggingface.co/{hub_id}"
225
+ return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{hub_id}</a>'
226
+
227
+
228
+ def obtain_top_users(user_ids_annotations: Dict[str, int], N: int = 50) -> pd.DataFrame:
229
+ """
230
+ This function returns the top N users with the most annotations.
231
+
232
+ Args:
233
+ user_ids_annotations: A dictionary with the user ids as the key and the number of annotations as the value.
234
+
235
+ Returns:
236
+ A pandas dataframe with the top N users with the most annotations.
237
+ """
238
+
239
+ dataframe = pd.DataFrame(
240
+ user_ids_annotations.items(), columns=[NAME, NUMBER_ANNOTATIONS]
241
+ )
242
+ dataframe[NAME] = dataframe[NAME].apply(render_hub_user_link)
243
+ dataframe = dataframe.sort_values(by=NUMBER_ANNOTATIONS, ascending=False)
244
+ return dataframe.head(N)
245
+
246
+
247
+ def fetch_data() -> None:
248
+ """
249
+ This function fetches the data from the source and target datasets and updates the global variables.
250
+ """
251
+
252
+ print(f"Starting to fetch data: {datetime.datetime.now()}")
253
+
254
+ global source_dataset, target_dataset, user_ids_annotations, annotated, remaining, percentage_completed, top_dataframe
255
+ source_dataset, target_dataset = obtain_source_target_datasets()
256
+ user_ids_annotations = get_user_annotations_dictionary(target_dataset)
257
+
258
+ annotated = len(target_dataset)
259
+ remaining = int(os.getenv("TARGET_RECORDS")) - annotated
260
+ percentage_completed = round(
261
+ (annotated / int(os.getenv("TARGET_RECORDS"))) * 100, 1
262
+ )
263
+
264
+ # Print the current date and time
265
+ print(f"Data fetched: {datetime.datetime.now()}")
266
+
267
+
268
+ def get_top(N=50) -> pd.DataFrame:
269
+ """
270
+ This function returns the top N users with the most annotations.
271
+
272
+ Args:
273
+ N: The number of users to be returned. 50 by default
274
+
275
+ Returns:
276
+ A pandas dataframe with the top N users with the most annotations.
277
+ """
278
+
279
+ return obtain_top_users(user_ids_annotations, N=N)
280
+
281
+
282
+ def main() -> None:
283
+
284
+ # Connect to the space with rg.init()
285
+ rg.init(
286
+ api_url=os.getenv("ARGILLA_API_URL"),
287
+ api_key=os.getenv("ARGILLA_API_KEY"),
288
+ )
289
+
290
+ # Fetch the data initially
291
+ fetch_data()
292
+
293
+ # To avoid the orange border for the Gradio elements that are in constant loading
294
+ css = """
295
+ .generating {
296
+ border: none;
297
+ }
298
+ """
299
+
300
+ with gr.Blocks(css=css) as demo:
301
+ gr.Markdown(
302
+ """
303
+ # 🌍 Turkish - Multilingual Prompt Evaluation Project
304
+
305
+ Hugging Face and @argilla are developing [Multilingual Prompt Evaluation Project](https://github.com/huggingface/data-is-better-together/tree/main/prompt_translation) project. It is an open multilingual benchmark for evaluating language models, and of course, also for Turkish.
306
+
307
+ ## The goal is to translate 500 Prompts
308
+ And as always: data is needed for that! The community selected the best 500 prompts that will form the benchmark. In English, of course.
309
+ **That's why we need your help**: if we all translate the 500 prompts, we can add Turkish to the leaderboard.
310
+
311
+ ## How to participate
312
+ Participating is easy. Go to the [annotation space](https://dibt-turkish-prompt-translation-for-turkish.hf.space/), log in or create a Hugging Face account, and you can start working.
313
+ Thanks in advance! Oh, and we'll give you a little push: GPT4 has already prepared a translation suggestion for you.
314
+ """
315
+ )
316
+
317
+ gr.Markdown(
318
+ f"""
319
+ ## πŸš€ Current Progress
320
+ This is what we've achieved so far!
321
+ """
322
+ )
323
+ with gr.Row():
324
+
325
+ kpi_submitted_plot = gr.Plot(label="Plot")
326
+ demo.load(
327
+ kpi_chart_submitted,
328
+ inputs=[],
329
+ outputs=[kpi_submitted_plot],
330
+ )
331
+
332
+ kpi_remaining_plot = gr.Plot(label="Plot")
333
+ demo.load(
334
+ kpi_chart_remaining,
335
+ inputs=[],
336
+ outputs=[kpi_remaining_plot],
337
+ )
338
+
339
+ donut_total_plot = gr.Plot(label="Plot")
340
+ demo.load(
341
+ donut_chart_total,
342
+ inputs=[],
343
+ outputs=[donut_total_plot],
344
+ )
345
+
346
+ gr.Markdown(
347
+ """
348
+ ## πŸ‘Ύ Onur Listesi
349
+ Aşağıda en çok işaretlemeyi yapan kullanıcılar listelenmiştir.
350
+ """
351
+ )
352
+
353
+ with gr.Row():
354
+
355
+ kpi_hall_plot = gr.Plot(label="Plot")
356
+ demo.load(kpi_chart_total_annotators, inputs=[], outputs=[kpi_hall_plot])
357
+
358
+ top_df_plot = gr.Dataframe(
359
+ headers=[NAME, NUMBER_ANNOTATIONS],
360
+ datatype=[
361
+ "markdown",
362
+ "number",
363
+ ],
364
+ row_count=50,
365
+ col_count=(2, "fixed"),
366
+ interactive=False,
367
+ )
368
+ demo.load(get_top, None, [top_df_plot])
369
+
370
+ # Launch the Gradio interface
371
+ demo.launch()
372
+
373
+
374
+ if __name__ == "__main__":
375
+ main()
dumpy.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import logging
3
+ import os
4
+
5
+ import argilla as rg
6
+ from huggingface_hub import HfApi
7
+
8
+ logger = logging.getLogger(__name__)
9
+ logger.setLevel(logging.INFO)
10
+
11
+ if __name__ == "__main__":
12
+ logger.info("*** Initializing Argilla session ***")
13
+ rg.init(
14
+ api_url=os.getenv("ARGILLA_API_URL"),
15
+ api_key=os.getenv("ARGILLA_API_KEY"),
16
+ extra_headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"},
17
+ )
18
+
19
+ logger.info("*** Fetching dataset from Argilla ***")
20
+ dataset = rg.FeedbackDataset.from_argilla(
21
+ os.getenv("SOURCE_DATASET"),
22
+ workspace=os.getenv("SOURCE_WORKSPACE"),
23
+ )
24
+ logger.info("*** Filtering records by `response_status` ***")
25
+ dataset = dataset.filter_by(response_status=["submitted"]) # type: ignore
26
+
27
+ logger.info("*** Calculating users and annotation count ***")
28
+ output = {}
29
+ for record in dataset.records:
30
+ for response in record.responses:
31
+ if response.user_id not in output:
32
+ output[response.user_id] = 0
33
+ output[response.user_id] += 1
34
+
35
+ for key in list(output.keys()):
36
+ output[rg.User.from_id(key).username] = output.pop(key)
37
+
38
+ logger.info("*** Users and annotation count successfully calculated! ***")
39
+
40
+ logger.info("*** Dumping Python dict into `stats.json` ***")
41
+ with open("stats.json", "w") as file:
42
+ json.dump(output, file, indent=4)
43
+
44
+ logger.info("*** Uploading `stats.json` to Hugging Face Hub ***")
45
+ api = HfApi(token=os.getenv("HF_TOKEN"))
46
+ api.upload_file(
47
+ path_or_fileobj="stats.json",
48
+ path_in_repo="stats.json",
49
+ repo_id="DIBT/prompt-collective-dashboard",
50
+ repo_type="space",
51
+ )
52
+ logger.info("*** `stats.json` successfully uploaded to Hugging Face Hub! ***")
requirements.txt ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ altair==5.2.0
3
+ annotated-types==0.6.0
4
+ anyio==4.2.0
5
+ apscheduler==3.10.4
6
+ argilla==1.23.0
7
+ attrs==23.2.0
8
+ backoff==2.2.1
9
+ certifi==2024.2.2
10
+ charset-normalizer==3.3.2
11
+ click==8.1.7
12
+ colorama==0.4.6
13
+ contourpy==1.2.0
14
+ cycler==0.12.1
15
+ Deprecated==1.2.14
16
+ exceptiongroup==1.2.0
17
+ fastapi==0.109.2
18
+ ffmpy==0.3.1
19
+ filelock==3.13.1
20
+ fonttools==4.48.1
21
+ fsspec==2024.2.0
22
+ gradio==4.17.0
23
+ gradio_client==0.9.0
24
+ h11==0.14.0
25
+ httpcore==1.0.2
26
+ httpx==0.26.0
27
+ huggingface-hub==0.20.3
28
+ idna==3.6
29
+ importlib-resources==6.1.1
30
+ Jinja2==3.1.3
31
+ jsonschema==4.21.1
32
+ jsonschema-specifications==2023.12.1
33
+ kiwisolver==1.4.5
34
+ markdown-it-py==3.0.0
35
+ MarkupSafe==2.1.5
36
+ matplotlib==3.8.2
37
+ mdurl==0.1.2
38
+ monotonic==1.6
39
+ numpy==1.23.5
40
+ orjson==3.9.13
41
+ packaging==23.2
42
+ pandas==1.5.3
43
+ pillow==10.2.0
44
+ pydantic==2.6.1
45
+ pydantic_core==2.16.2
46
+ pydub==0.25.1
47
+ Pygments==2.17.2
48
+ pyparsing==3.1.1
49
+ python-dateutil==2.8.2
50
+ python-multipart==0.0.7
51
+ pytz==2024.1
52
+ PyYAML==6.0.1
53
+ referencing==0.33.0
54
+ requests==2.31.0
55
+ rich==13.7.0
56
+ rpds-py==0.17.1
57
+ ruff==0.2.1
58
+ semantic-version==2.10.0
59
+ shellingham==1.5.4
60
+ six==1.16.0
61
+ sniffio==1.3.0
62
+ starlette==0.36.3
63
+ tomlkit==0.12.0
64
+ toolz==0.12.1
65
+ tqdm==4.66.1
66
+ typer==0.9.0
67
+ typing_extensions==4.9.0
68
+ urllib3==2.2.0
69
+ uvicorn==0.27.0.post1
70
+ vega-datasets==0.9.0
71
+ websockets==11.0.3
72
+ wrapt==1.14.1