Francisco Aranda commited on
Commit
32c2587
1 Parent(s): 8f24e22

Creating a gradio APP

Browse files
Files changed (2) hide show
  1. main.py +47 -62
  2. requirements.txt +1 -2
main.py CHANGED
@@ -1,76 +1,61 @@
1
- import os
2
- from datetime import datetime
3
 
4
  import argilla as rg
 
5
 
6
- # Environment variables with defaults
7
- API_KEY = os.environ.get("ARGILLA_API_KEY", "argilla.apikey")
8
- API_URL = os.environ.get("ARGILLA_API_URL", "http://localhost:6900")
9
 
10
- # Initialize Argilla client
11
- client = rg.Argilla(api_key=API_KEY, api_url=API_URL)
 
 
12
 
13
- # Show the existing webhooks in the argilla server
14
  for webhook in client.webhooks:
15
- print(webhook.url)
16
-
17
-
18
- # Create a webhook listener using the decorator
19
- # This decorator will :
20
- # 1. Create the webhook in the argilla server
21
- # 2. Create a POST endpoint in the server
22
- # 3. Handle the incoming requests to verify the webhook signature
23
- # 4. Ignoring the events other than the ones specified in the `events` argument
24
- # 5. Parse the incoming request and call the decorated function with the parsed data
25
- #
26
- # Each event will be passed as a keyword argument to the decorated function depending on the event type.
27
- # The event types are:
28
- # - record: created, updated, deleted and completed
29
- # - response: created, updated, deleted
30
- # - dataset: created, updated, published, deleted
31
- # Related resources will be passed as keyword arguments to the decorated function
32
- # (for example the dataset for a record-related event, or the record for a response-related event)
33
- # When a resource is deleted
34
- @rg.webhook_listener(events=["record.created", "record.completed"])
35
- async def listen_record(
36
- record: rg.Record, dataset: rg.Dataset, type: str, timestamp: datetime
37
- ):
38
- print(f"Received record event of type {type} at {timestamp}")
39
-
40
- action = "completed" if type == "record.completed" else "created"
41
- print(f"A record with id {record.id} has been {action} for dataset {dataset.name}!")
42
-
43
-
44
- @rg.webhook_listener(events="response.updated")
45
- async def trigger_something_on_response_updated(response: rg.UserResponse, **kwargs):
46
- print(
47
- f"The user response {response.id} has been updated with the following responses:"
48
- )
49
- print([response.serialize() for response in response.responses])
50
 
 
51
 
 
52
  @rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.published"])
53
- async def with_raw_payload(
54
- type: str,
55
- timestamp: datetime,
56
- dataset: rg.Dataset,
57
- **kwargs,
58
- ):
59
- print(f"Event type {type} at {timestamp}")
60
- print(dataset.settings)
61
 
 
 
 
 
 
 
62
 
63
- @rg.webhook_listener(events="dataset.deleted")
64
- async def on_dataset_deleted(
65
- data: dict,
66
- **kwargs,
67
- ):
68
- print(f"Dataset {data} has been deleted!")
69
 
 
 
 
 
70
 
71
- # Set the webhook server. The server is a FastAPI instance, so you need to expose it in order to run it using uvicorn:
72
- # ```bash
73
- # uvicorn main:webhook_server --reload
74
- # ```
75
 
76
- server = rg.get_webhook_server()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from queue import Queue
 
2
 
3
  import argilla as rg
4
+ import gradio as gr
5
 
6
+ client = rg.Argilla()
 
 
7
 
8
+ server = rg.get_webhook_server()
9
+ incoming_events = Queue()
10
+
11
+ # Set up the webhook listeners
12
 
13
+ # Delete all existing webhooks
14
  for webhook in client.webhooks:
15
+ webhook.delete()
16
+
17
+ # Create a webhook for record events
18
+ @rg.webhook_listener(events=["record.created", "record.updated", "record.completed"])
19
+ async def record_events(record: rg.Record, **kwargs):
20
+ print(f"Received event {kwargs['type']} for record {record.id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ incoming_events.put(record)
23
 
24
+ # Create a webhook for dataset events
25
  @rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.published"])
26
+ async def dataset_events(type: str, dataset: rg.Dataset | None = None, **kwargs):
 
 
 
 
 
 
 
27
 
28
+ if "deleted" in type:
29
+ print(f"Received event {type} for dataset {kwargs['data']}")
30
+ incoming_events.put((type, kwargs["data"]))
31
+ else:
32
+ print(f"Received event {type} for dataset {dataset.id}")
33
+ incoming_events.put((type, dataset))
34
 
 
 
 
 
 
 
35
 
36
+ # Create a webhook for response events
37
+ @rg.webhook_listener(events=["response.created", "response.updated", "response.deleted"])
38
+ async def response_events(response: rg.UserResponse, **kwargs):
39
+ print(f"Received event {kwargs['type']} for response {response.id}")
40
 
41
+ incoming_events.put(response)
 
 
 
42
 
43
+
44
+ def check_incoming_events():
45
+ """
46
+ This function is called every 5 seconds to check if there are any incoming
47
+ events and send data to update the JSON component.
48
+ """
49
+ events = []
50
+ while not incoming_events.empty():
51
+ events.append(incoming_events.get())
52
+
53
+ return {"events": events}
54
+
55
+
56
+ with gr.Blocks() as demo:
57
+ json_component = gr.JSON(label="Incoming argilla events:")
58
+ gr.Timer(5, active=True).tick(check_incoming_events, outputs=json_component)
59
+
60
+
61
+ gr.mount_gradio_app(server, demo, path="/gradio")
requirements.txt CHANGED
@@ -1,3 +1,2 @@
 
1
  argilla @ git+https://github.com/argilla-io/argilla.git@feat/argilla/working-with-webhooks#subdirectory=argilla
2
- fastapi
3
- uvicorn[standard]
 
1
+ gradio
2
  argilla @ git+https://github.com/argilla-io/argilla.git@feat/argilla/working-with-webhooks#subdirectory=argilla