Francisco Aranda commited on
Commit
30c85ad
1 Parent(s): bcf795e

chore: prepare space example

Browse files
Files changed (2) hide show
  1. main.py +22 -23
  2. start.sh +2 -1
main.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  from queue import Queue
3
 
4
  import argilla as rg
@@ -9,55 +10,53 @@ client = rg.Argilla()
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, record)
20
 
21
  incoming_events.put(record)
22
 
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, **kwargs):
27
- print(f"Received event", type, dataset)
28
 
29
  incoming_events.put((type, dataset))
30
 
31
 
32
  # Create a webhook for response events
33
  @rg.webhook_listener(events=["response.created", "response.updated"])
34
- async def response_events(type:str, response: rg.UserResponse, **kwargs):
35
- print(f"Received event", type, response)
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):
42
- print(f"Received event {type} for resource {data}")
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="/")
 
1
  import os
2
+ from datetime import datetime
3
  from queue import Queue
4
 
5
  import argilla as rg
 
10
  server = rg.get_webhook_server()
11
  incoming_events = Queue()
12
 
13
+ # The default webhook server URL is set to http://127.0.0.1:8000
14
+ # This value is important since the webhook server URL is used to create the webhook URLs
15
+ # When the SPACE_HOST environment variable is set, the default webhook server URL is set to https://SPACE_HOST value
16
+ # If the FastAPI app is running on a different URL, the WEBHOOK_SERVER_URL environment variable should be set
17
+ default_webhook_server_url = os.getenv("WEBHOOK_SERVER_URL")
18
+ if default_webhook_server_url is None and "SPACE_HOST" in os.environ:
19
+ # We set it to the default gradio server URL
20
+ os.environ["WEBHOOK_SERVER_URL"] = "http://127.0.0.1:7860"
21
+
22
 
23
  # Set up the webhook listeners
24
 
25
  # Create a webhook for record events
26
+ @rg.webhook_listener(events=["record.created", "record.updated", "record.deleted", "record.completed"])
27
+ async def record_events(record: rg.Record, type: str, timestamp: datetime):
28
+ print(f"Received event type {type} at {timestamp}: ", record)
 
29
 
30
  incoming_events.put(record)
31
 
32
 
33
  # Create a webhook for dataset events
34
+ @rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.deleted", "dataset.published"])
35
+ async def dataset_events(dataset: rg.Dataset, type: str, timestamp: datetime):
36
+ print(f"Received event type {type} at {timestamp}: ", dataset)
37
 
38
  incoming_events.put((type, dataset))
39
 
40
 
41
  # Create a webhook for response events
42
  @rg.webhook_listener(events=["response.created", "response.updated"])
43
+ async def response_events(response: rg.UserResponse, type: str, timestamp: datetime):
44
+ print(f"Received event type {type} at {timestamp}: ", response)
45
 
46
  incoming_events.put(response)
47
 
48
 
49
+ def read_next_event():
 
 
 
 
 
 
 
 
 
 
 
50
  event = incoming_events.get()
51
 
52
  return event
53
 
54
+
55
  with gr.Blocks() as demo:
56
  argilla_server = client.http_client.base_url
57
  gr.Markdown("## Argilla Events")
58
  gr.Markdown(f"This demo shows the incoming events from the [Argilla Server]({argilla_server}).")
59
+ json_component = gr.JSON(label="Incoming argilla events:", value={})
60
+ gr.Timer(1, active=True).tick(read_next_event, outputs=json_component)
61
 
62
  gr.mount_gradio_app(server, demo, path="/")
start.sh CHANGED
@@ -1 +1,2 @@
1
- WEBHOOK_SERVER_URL="https://$SPACE_HOST" uvicorn main:server --host 0.0.0.0 --port 7860
 
 
1
+ uvicorn main:server --host 0.0.0.0 --port 7860
2
+