Francisco Aranda commited on
Commit
36e4ada
·
1 Parent(s): c631621

Add application files

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