ouhenio commited on
Commit
61186ad
·
verified ·
1 Parent(s): db908e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -64
app.py CHANGED
@@ -1,115 +1,100 @@
1
  import os
2
- from datetime import datetime
3
  from queue import Queue
4
- from argilla.webhooks import webhook_listener
5
-
6
- import argilla as rg
7
  import gradio as gr
 
 
 
8
 
 
 
9
 
10
- client = rg.Argilla()
 
 
 
 
11
 
 
12
  server = rg.get_webhook_server()
13
- incoming_events = Queue()
14
-
15
- # Set up the webhook listeners
16
 
17
- # Delete all existing webhooks
18
- for webhook in client.webhooks:
19
- print(f"Deleting webhook: {webhook.url}")
20
- webhook.delete()
21
 
22
- @webhook_listener(events="record.completed")
23
- async def update_validation_space_on_answer(record: rg.Record, type: str):
 
24
  """
25
  Webhook listener that triggers when a new response is added to an answering space.
26
  It will automatically update the corresponding validation space with the new response.
27
-
28
- Args:
29
- event: The webhook event containing the user response data
30
  """
 
 
31
 
 
 
 
 
32
  dataset_name = record.dataset.name
33
  if not dataset_name.endswith("_responder_preguntas"):
 
34
  return # Not an answering space, ignore
35
 
36
  # Extract the country from the dataset name
37
  country = dataset_name.replace("_responder_preguntas", "")
 
38
 
 
39
  validation_dataset_name = f"{country}_validar_respuestas"
40
  try:
41
  validation_dataset = client.datasets(validation_dataset_name)
 
42
  except Exception as e:
43
  print(f"Error connecting to validation dataset: {e}")
 
 
44
  validation_dataset = create_validation_space(country)
45
 
 
46
  validation_record = {
47
  "question": record.fields["question"],
48
  "answer": response.value,
49
  }
50
 
 
51
  validation_dataset.records.log(records=[validation_record])
52
-
53
- incoming_events.put({"event": type, "data": record})
54
  print(f"Added new response to validation space for {country}")
55
 
56
-
57
- # Create a webhook for record events
58
- # @rg.webhook_listener(events=["record.deleted", "record.completed"])
59
- # async def record_events(record: rg.Record, type: str, timestamp: datetime):
60
- # print(f"Received event type {type} at {timestamp}: ", record)
61
-
62
- # incoming_events.put({"event": type, "data": record})
63
-
64
-
65
- # Create a webhook for dataset events
66
- # @rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.deleted", "dataset.published"])
67
- # async def dataset_events(dataset: rg.Dataset, type: str, timestamp: datetime):
68
- # print(f"Received event type {type} at {timestamp}: ", dataset)
69
-
70
- # incoming_events.put({"event": type, "data": dataset})
71
-
72
-
73
- # Create a webhook for response events
74
- # @rg.webhook_listener(events=["response.created", "response.updated"])
75
- # async def response_events(response: rg.UserResponse, type: str, timestamp: datetime):
76
- # print(f"Received event type {type} at {timestamp}: ", response)
77
-
78
- # incoming_events.put({"event": type, "data": response})
79
-
80
-
81
  def read_next_event():
82
- event = incoming_events.get()
83
-
84
- return event
85
-
86
 
 
87
  with gr.Blocks() as demo:
88
  argilla_server = client.http_client.base_url
89
- gr.Markdown("## Argilla Webhooks")
90
  gr.Markdown(f"""
91
- This demo shows the incoming events from the [Argilla Server]({argilla_server}).
 
92
 
93
- The application defines three webhook listeners for the following events:
94
-
95
- - Record events: `record.deleted`, `record.completed`
96
- - Dataset events: `dataset.created`, `dataset.updated`, `dataset.deleted`, `dataset.published`
97
- - Response events: `response.created`, `response.updated`
98
-
99
- The events are stored in a queue and displayed in the JSON component and the incoming events is updated every second.
100
 
101
- You can view the incoming events in the JSON component below.
 
102
 
103
- This application is just a demonstration of how to use the Argilla webhook listeners.
 
104
 
105
- You can visit the [Argilla documentation](https://docs.argilla.io/dev/how_to_guides/webhooks) for more information.
106
  """)
107
- json_component = gr.JSON(label="Incoming argilla events:", value={})
108
  gr.Timer(1, active=True).tick(read_next_event, outputs=json_component)
109
 
 
110
  gr.mount_gradio_app(server, demo, path="/")
111
 
112
  # Start the FastAPI server
113
- import uvicorn
114
-
115
- uvicorn.run(server, host="0.0.0.0", port=7860)
 
1
  import os
 
2
  from queue import Queue
 
 
 
3
  import gradio as gr
4
+ import argilla as rg
5
+ from argilla.webhooks import webhook_listener
6
+ from dotenv import load_dotenv
7
 
8
+ # Load environment variables
9
+ load_dotenv()
10
 
11
+ # Initialize Argilla client
12
+ client = rg.Argilla(
13
+ api_url=os.getenv("API_URL"),
14
+ api_key=os.getenv("ARGILLA_API_KEY"),
15
+ )
16
 
17
+ # Get the webhook server
18
  server = rg.get_webhook_server()
 
 
 
19
 
20
+ # Queue to store events for display
21
+ incoming_events = Queue()
 
 
22
 
23
+ # Set up the webhook listener for response creation
24
+ @webhook_listener(events=["response.created"])
25
+ async def update_validation_space_on_answer(response, type, timestamp):
26
  """
27
  Webhook listener that triggers when a new response is added to an answering space.
28
  It will automatically update the corresponding validation space with the new response.
 
 
 
29
  """
30
+ # Store the event for display in the UI
31
+ incoming_events.put({"event": type, "data": str(response)})
32
 
33
+ # Get the record from the response
34
+ record = response.record
35
+
36
+ # Check if this is from an answering space
37
  dataset_name = record.dataset.name
38
  if not dataset_name.endswith("_responder_preguntas"):
39
+ print(f"Ignoring event from non-answering dataset: {dataset_name}")
40
  return # Not an answering space, ignore
41
 
42
  # Extract the country from the dataset name
43
  country = dataset_name.replace("_responder_preguntas", "")
44
+ print(f"Processing response for country: {country}")
45
 
46
+ # Connect to the validation space
47
  validation_dataset_name = f"{country}_validar_respuestas"
48
  try:
49
  validation_dataset = client.datasets(validation_dataset_name)
50
+ print(f"Found validation dataset: {validation_dataset_name}")
51
  except Exception as e:
52
  print(f"Error connecting to validation dataset: {e}")
53
+ # You would need to import the create_validation_space function
54
+ from your_module import create_validation_space
55
  validation_dataset = create_validation_space(country)
56
 
57
+ # Create a validation record
58
  validation_record = {
59
  "question": record.fields["question"],
60
  "answer": response.value,
61
  }
62
 
63
+ # Add the record to the validation space
64
  validation_dataset.records.log(records=[validation_record])
 
 
65
  print(f"Added new response to validation space for {country}")
66
 
67
+ # Function to read the next event from the queue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def read_next_event():
69
+ if not incoming_events.empty():
70
+ return incoming_events.get()
71
+ return {}
 
72
 
73
+ # Create Gradio interface
74
  with gr.Blocks() as demo:
75
  argilla_server = client.http_client.base_url
76
+ gr.Markdown("## Argilla Webhooks - Validation Space Updater")
77
  gr.Markdown(f"""
78
+ This application listens for new responses in Argilla answering spaces and automatically
79
+ adds them to the corresponding validation spaces.
80
 
81
+ Connected to Argilla server: {argilla_server}
 
 
 
 
 
 
82
 
83
+ The webhook listens for:
84
+ - `response.created` events from datasets ending with `_responder_preguntas`
85
 
86
+ When a new response is created, it is added to the corresponding validation space
87
+ named `[country]_validar_respuestas`.
88
 
89
+ You can view the incoming events in the JSON component below.
90
  """)
91
+ json_component = gr.JSON(label="Incoming response events:", value={})
92
  gr.Timer(1, active=True).tick(read_next_event, outputs=json_component)
93
 
94
+ # Mount the Gradio app to the FastAPI server
95
  gr.mount_gradio_app(server, demo, path="/")
96
 
97
  # Start the FastAPI server
98
+ if __name__ == "__main__":
99
+ import uvicorn
100
+ uvicorn.run(server, host="0.0.0.0", port=7860)