Francisco Aranda commited on
Commit
ea4e048
·
1 Parent(s): a7c2cd2

refactor: Show queue elements one by one

Browse files
Files changed (1) hide show
  1. main.py +15 -25
main.py CHANGED
@@ -9,22 +9,16 @@ client = rg.Argilla()
9
  server = rg.get_webhook_server()
10
  incoming_events = Queue()
11
 
12
- # Set up the webhook listeners
13
 
14
- # Delete all existing webhooks
15
- for webhook in client.webhooks:
16
- webhook.delete()
17
 
18
  # Create a webhook for record events
19
 
20
- @rg.webhook_listener(
21
- events=["record.created", "record.updated", "record.completed"],
22
- raw_event=True # Using raw events until PR https://github.com/argilla-io/argilla/pull/5500 is merged
23
- )
24
- async def record_events(event:dict):
25
- print("Received event", event)
26
 
27
- incoming_events.put(event)
28
 
29
  # Create a webhook for dataset events
30
  @rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.published"])
@@ -34,15 +28,14 @@ async def dataset_events(type: str, dataset: rg.Dataset | None = None, **kwargs)
34
  incoming_events.put((type, dataset))
35
 
36
 
 
37
  # Create a webhook for response events
38
- @rg.webhook_listener(
39
- events=["response.created", "response.updated"],
40
- raw_event=True # Using raw events until PR https://github.com/argilla-io/argilla/pull/5500 is merged
41
- )
42
- async def response_events(event: dict):
43
- print("Received event", event)
44
 
45
- incoming_events.put(event)
46
 
47
  @rg.webhook_listener(events=["record.deleted", "dataset.deleted", "response.deleted"])
48
  async def deleted_events(type: str, data: dict, **kwargs):
@@ -50,24 +43,21 @@ async def deleted_events(type: str, data: dict, **kwargs):
50
 
51
  incoming_events.put((type, data))
52
 
 
53
  def check_incoming_events():
54
  """
55
  This function is called every 5 seconds to check if there are any incoming
56
  events and send data to update the JSON component.
57
  """
58
- events = []
59
- while not incoming_events.empty():
60
- events.append(incoming_events.get())
61
-
62
- return {"events": events}
63
 
 
64
 
65
  with gr.Blocks() as demo:
66
  argilla_server = client.http_client.base_url
67
  gr.Markdown("## Argilla Events")
68
  gr.Markdown(f"This demo shows the incoming events from the [Argilla Server]({argilla_server}).")
69
  json_component = gr.JSON(label="Incoming argilla events:")
70
- gr.Timer(5, active=True).tick(check_incoming_events, outputs=json_component)
71
-
72
 
73
  gr.mount_gradio_app(server, demo, path="/")
 
9
  server = rg.get_webhook_server()
10
  incoming_events = Queue()
11
 
 
12
 
13
+ # Set up the webhook listeners
 
 
14
 
15
  # Create a webhook for record events
16
 
17
+ @rg.webhook_listener(events=["record.created", "record.updated", "record.completed"])
18
+ async def record_events(record: rg.Record, type: str, **kwargs):
19
+ print("Received event", type)
 
 
 
20
 
21
+ incoming_events.put(record)
22
 
23
  # Create a webhook for dataset events
24
  @rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.published"])
 
28
  incoming_events.put((type, dataset))
29
 
30
 
31
+
32
  # Create a webhook for response events
33
+ @rg.webhook_listener(events=["response.created", "response.updated"])
34
+ async def response_events(response: rg.UserResponse, type:str, **kwargs):
35
+ print("Received event type", type)
36
+
37
+ incoming_events.put(response)
 
38
 
 
39
 
40
  @rg.webhook_listener(events=["record.deleted", "dataset.deleted", "response.deleted"])
41
  async def deleted_events(type: str, data: dict, **kwargs):
 
43
 
44
  incoming_events.put((type, data))
45
 
46
+
47
  def check_incoming_events():
48
  """
49
  This function is called every 5 seconds to check if there are any incoming
50
  events and send data to update the JSON component.
51
  """
52
+ event = incoming_events.get()
 
 
 
 
53
 
54
+ return event
55
 
56
  with gr.Blocks() as demo:
57
  argilla_server = client.http_client.base_url
58
  gr.Markdown("## Argilla Events")
59
  gr.Markdown(f"This demo shows the incoming events from the [Argilla Server]({argilla_server}).")
60
  json_component = gr.JSON(label="Incoming argilla events:")
61
+ gr.Timer(1, active=True).tick(check_incoming_events, outputs=json_component)
 
62
 
63
  gr.mount_gradio_app(server, demo, path="/")