ignacioct commited on
Commit
2c2ec39
β€’
1 Parent(s): e563844

Dataframe now updates on background (#5)

Browse files

- Dataframe now updates on background (ec79be5278aa2322d671aabf69504a90938fde6d)

Files changed (2) hide show
  1. app.py +64 -26
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  from typing import Dict, Tuple
3
  from uuid import UUID
@@ -214,6 +216,7 @@ def kpi_chart() -> alt.Chart:
214
 
215
  return chart
216
 
 
217
  def render_hub_user_link(hub_id):
218
  link = f"https://huggingface.co/{hub_id}"
219
  return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{hub_id}</a>'
@@ -233,13 +236,42 @@ def obtain_top_5_users(user_ids_annotations: Dict[str, int]) -> pd.DataFrame:
233
  dataframe = pd.DataFrame(
234
  user_ids_annotations.items(), columns=["Name", "Submitted Responses"]
235
  )
236
- dataframe['Name'] = dataframe['Name'].apply(render_hub_user_link)
237
  dataframe = dataframe.sort_values(by="Submitted Responses", ascending=False)
238
  return dataframe.head(50)
239
 
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  def main() -> None:
242
 
 
 
 
 
243
  # Connect to the space with rg.init()
244
  rg.init(
245
  api_url=os.getenv("ARGILLA_API_URL"),
@@ -247,19 +279,22 @@ def main() -> None:
247
  extra_headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"},
248
  )
249
 
250
- global source_dataset, target_dataset, user_ids_annotations
251
- source_dataset, target_dataset = obtain_source_target_datasets()
252
- user_ids_annotations = get_user_annotations_dictionary(target_dataset)
253
 
254
- top5_dataframe = obtain_top_5_users(user_ids_annotations)
255
-
256
- annotated = len(target_dataset)
257
- remaining = int(os.getenv("TARGET_RECORDS")) - annotated
258
- percentage_completed = round(
259
- (annotated / int(os.getenv("TARGET_RECORDS"))) * 100, 1
260
  )
 
261
 
262
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
263
  gr.Markdown(
264
  """
265
  # πŸ—£οΈ The Prompt Collective Dashboad
@@ -277,11 +312,12 @@ def main() -> None:
277
  )
278
  with gr.Row():
279
 
280
- plot2 = gr.Plot(label="Plot")
281
  demo.load(
282
  donut_chart_target,
283
  inputs=[],
284
- outputs=[plot2],
 
285
  )
286
 
287
  gr.Markdown(
@@ -299,25 +335,28 @@ def main() -> None:
299
  )
300
  with gr.Row():
301
 
302
- plot = gr.Plot(label="Plot")
303
  demo.load(
304
  kpi_chart_submitted,
305
  inputs=[],
306
- outputs=[plot],
 
307
  )
308
 
309
- plot = gr.Plot(label="Plot")
310
  demo.load(
311
  kpi_chart_remaining,
312
  inputs=[],
313
- outputs=[plot],
 
314
  )
315
 
316
- plot2 = gr.Plot(label="Plot")
317
  demo.load(
318
  donut_chart_total,
319
  inputs=[],
320
- outputs=[plot2],
 
321
  )
322
 
323
  gr.Markdown(
@@ -329,15 +368,12 @@ def main() -> None:
329
 
330
  with gr.Row():
331
 
332
- plot2 = gr.Plot(label="Plot")
333
  demo.load(
334
- kpi_chart,
335
- inputs=[],
336
- outputs=[plot2],
337
  )
338
 
339
- gr.Dataframe(
340
- value=top5_dataframe,
341
  headers=["Name", "Submitted Responses"],
342
  datatype=[
343
  "markdown",
@@ -346,7 +382,9 @@ def main() -> None:
346
  row_count=50,
347
  col_count=(2, "fixed"),
348
  interactive=False,
349
- ),
 
 
350
 
351
  # Launch the Gradio interface
352
  demo.launch()
 
1
+ from apscheduler.schedulers.background import BackgroundScheduler
2
+ import datetime
3
  import os
4
  from typing import Dict, Tuple
5
  from uuid import UUID
 
216
 
217
  return chart
218
 
219
+
220
  def render_hub_user_link(hub_id):
221
  link = f"https://huggingface.co/{hub_id}"
222
  return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{hub_id}</a>'
 
236
  dataframe = pd.DataFrame(
237
  user_ids_annotations.items(), columns=["Name", "Submitted Responses"]
238
  )
239
+ dataframe["Name"] = dataframe["Name"].apply(render_hub_user_link)
240
  dataframe = dataframe.sort_values(by="Submitted Responses", ascending=False)
241
  return dataframe.head(50)
242
 
243
 
244
+ def fetch_data() -> None:
245
+ """
246
+ This function fetches the data from the source and target datasets and updates the global variables.
247
+ """
248
+
249
+ print(f"Starting to fetch data: {datetime.datetime.now()}")
250
+
251
+ global source_dataset, target_dataset, user_ids_annotations, annotated, remaining, percentage_completed, top5_dataframe
252
+ source_dataset, target_dataset = obtain_source_target_datasets()
253
+ user_ids_annotations = get_user_annotations_dictionary(target_dataset)
254
+
255
+ annotated = len(target_dataset)
256
+ remaining = int(os.getenv("TARGET_RECORDS")) - annotated
257
+ percentage_completed = round(
258
+ (annotated / int(os.getenv("TARGET_RECORDS"))) * 100, 1
259
+ )
260
+
261
+ # Print the current date and time
262
+ print(f"Data fetched: {datetime.datetime.now()}")
263
+
264
+
265
+ def get_top5() -> pd.DataFrame:
266
+ return obtain_top_5_users(user_ids_annotations)
267
+
268
+
269
  def main() -> None:
270
 
271
+ # Set the update interval
272
+ update_interval = 300 # seconds
273
+ update_interval_charts = 30 # seconds
274
+
275
  # Connect to the space with rg.init()
276
  rg.init(
277
  api_url=os.getenv("ARGILLA_API_URL"),
 
279
  extra_headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"},
280
  )
281
 
282
+ fetch_data()
 
 
283
 
284
+ scheduler = BackgroundScheduler()
285
+ scheduler.add_job(
286
+ func=fetch_data, trigger="interval", seconds=update_interval, max_instances=1
 
 
 
287
  )
288
+ scheduler.start()
289
 
290
+ # To avoid the orange border for the Gradio elements that are in constant loading
291
+ css = """
292
+ .generating {
293
+ border: none;
294
+ }
295
+ """
296
+
297
+ with gr.Blocks(css=css) as demo:
298
  gr.Markdown(
299
  """
300
  # πŸ—£οΈ The Prompt Collective Dashboad
 
312
  )
313
  with gr.Row():
314
 
315
+ donut_target_plot = gr.Plot(label="Plot")
316
  demo.load(
317
  donut_chart_target,
318
  inputs=[],
319
+ outputs=[donut_target_plot],
320
+ every=update_interval_charts,
321
  )
322
 
323
  gr.Markdown(
 
335
  )
336
  with gr.Row():
337
 
338
+ kpi_submitted_plot = gr.Plot(label="Plot")
339
  demo.load(
340
  kpi_chart_submitted,
341
  inputs=[],
342
+ outputs=[kpi_submitted_plot],
343
+ every=update_interval_charts,
344
  )
345
 
346
+ kpi_remaining_plot = gr.Plot(label="Plot")
347
  demo.load(
348
  kpi_chart_remaining,
349
  inputs=[],
350
+ outputs=[kpi_remaining_plot],
351
+ every=update_interval_charts,
352
  )
353
 
354
+ donut_total_plot = gr.Plot(label="Plot")
355
  demo.load(
356
  donut_chart_total,
357
  inputs=[],
358
+ outputs=[donut_total_plot],
359
+ every=update_interval_charts,
360
  )
361
 
362
  gr.Markdown(
 
368
 
369
  with gr.Row():
370
 
371
+ kpi_hall_plot = gr.Plot(label="Plot")
372
  demo.load(
373
+ kpi_chart, inputs=[], outputs=[kpi_hall_plot], every=update_interval_charts
 
 
374
  )
375
 
376
+ top5_df_plot = gr.Dataframe(
 
377
  headers=["Name", "Submitted Responses"],
378
  datatype=[
379
  "markdown",
 
382
  row_count=50,
383
  col_count=(2, "fixed"),
384
  interactive=False,
385
+ every=update_interval,
386
+ )
387
+ demo.load(get_top5, None, [top5_df_plot], every=update_interval_charts)
388
 
389
  # Launch the Gradio interface
390
  demo.launch()
requirements.txt CHANGED
@@ -2,6 +2,7 @@ aiofiles==23.2.1
2
  altair==5.2.0
3
  annotated-types==0.6.0
4
  anyio==4.2.0
 
5
  argilla==1.23.0
6
  attrs==23.2.0
7
  backoff==2.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