Spaces:
Runtime error
Runtime error
Francisco Aranda
commited on
Commit
·
f039650
1
Parent(s):
7000191
Add application files
Browse files- Dockerfile +17 -0
- README.md +41 -10
- main.py +76 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.10
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
COPY --chown=user . /main.py main.py
|
| 14 |
+
|
| 15 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 16 |
+
|
| 17 |
+
CMD ["uvicorn", "main:server", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,41 @@
|
|
| 1 |
-
|
| 2 |
-
title: Argilla Webhooks
|
| 3 |
-
emoji: 🐠
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: indigo
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
----
|
| 2 |
+
-title: Argilla Webhooks
|
| 3 |
+
-emoji: 🐠
|
| 4 |
+
-colorFrom: indigo
|
| 5 |
+
-colorTo: indigo
|
| 6 |
+
-sdk: docker
|
| 7 |
+
-pinned: false
|
| 8 |
+
----
|
| 9 |
+
-
|
| 10 |
+
-Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 11 |
+
## Description
|
| 12 |
+
|
| 13 |
+
This is a basic webhook example to show how to setup webhook listeners using the argilla SDK
|
| 14 |
+
|
| 15 |
+
## Running the app
|
| 16 |
+
|
| 17 |
+
1. Start argilla server and argilla worker
|
| 18 |
+
```bash
|
| 19 |
+
pdm server start
|
| 20 |
+
pdm worker
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
2. Add the `localhost.org` alias in the `/etc/hosts` file to comply with the Top Level Domain URL requirement.
|
| 24 |
+
```
|
| 25 |
+
##
|
| 26 |
+
# Host Database
|
| 27 |
+
#
|
| 28 |
+
# localhost is used to configure the loopback interface
|
| 29 |
+
# when the system is booting. Do not change this entry.
|
| 30 |
+
##
|
| 31 |
+
127.0.0.1 localhost localhost.org
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
2. Start the app
|
| 35 |
+
```bash
|
| 36 |
+
uvicorn main:server
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
## Testing the app
|
| 40 |
+
|
| 41 |
+
You can see in se server logs traces when working with dataset, records and responses in the argilla server
|
main.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
argilla @ git+https://github.com/argilla-io/argilla.git@feat/argilla/working-with-webhooks#subdirectory=argilla
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn[standard]
|