Spaces:
Running
Running
File size: 4,162 Bytes
a7c2cd2 32c2587 61186ad 4a7815d 61186ad 28d870e f039650 61186ad 32c2587 61186ad 16eaf19 61186ad 17145bf 9c8bc16 6d086e2 9c8bc16 17145bf 9c8bc16 17145bf 61186ad 30c85ad 61186ad 30c85ad 61186ad 32c2587 a7c2cd2 61186ad b0bc07c 61186ad b0bc07c 61186ad b0bc07c 61186ad b0bc07c 9c8bc16 b0bc07c 9c8bc16 30c85ad 32c2587 61186ad 6020a54 16eaf19 f40fa43 61186ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import os
from queue import Queue
import gradio as gr
import argilla as rg
from argilla.webhooks import webhook_listener
# Initialize Argilla client
client = rg.Argilla()
# Get the webhook server
server = rg.get_webhook_server()
# Queue to store events for display
incoming_events = Queue()
# Set up the webhook listener for response creation
@webhook_listener(events=["response.created"])
async def update_validation_space_on_answer(response, type, timestamp):
"""
Webhook listener that triggers when a new response is added to an answering space.
It will automatically update the corresponding validation space with the new response.
"""
try:
# Store the event for display in the UI
incoming_events.put({"event": type, "timestamp": str(timestamp)})
# Print the response object structure to understand its properties
print(f"Response object: {dir(response)}")
# Get the record from the response
record = response.record
# Check if this is from an answering space
dataset_name = record.dataset.name
if not dataset_name.endswith("_responder_preguntas"):
print(f"Ignoring event from non-answering dataset: {dataset_name}")
return # Not an answering space, ignore
# Extract the country from the dataset name
country = dataset_name.replace("_responder_preguntas", "")
print(f"Processing response for country: {country}")
# Connect to the validation space
validation_dataset_name = f"{country}_validar_respuestas"
try:
validation_dataset = client.datasets(validation_dataset_name)
print(f"Found validation dataset: {validation_dataset_name}")
except Exception as e:
print(f"Error connecting to validation dataset: {e}")
# You would need to import the create_validation_space function
from build_space import create_validation_space
validation_dataset = create_validation_space(country)
# Create a validation record with the correct attribute
# Instead of response.value, we need to find the correct attribute
# Based on the error, let's try to get the value differently
validation_record = {
"question": record.fields["question"],
# Use the first available response value - adjust this based on your actual response structure
"answer": response.values[0] if hasattr(response, 'values') and response.values else str(response),
}
# Add the record to the validation space
validation_dataset.records.log(records=[validation_record])
print(f"Added new response to validation space for {country}")
except Exception as e:
print(f"Error in webhook handler: {e}")
# Store the error in the queue for display
incoming_events.put({"event": "error", "error": str(e)})
# Function to read the next event from the queue
def read_next_event():
if not incoming_events.empty():
return incoming_events.get()
return {}
# Create Gradio interface
with gr.Blocks() as demo:
argilla_server = client.http_client.base_url
gr.Markdown("## Argilla Webhooks - Validation Space Updater")
gr.Markdown(f"""
This application listens for new responses in Argilla answering spaces and automatically
adds them to the corresponding validation spaces.
Connected to Argilla server: {argilla_server}
The webhook listens for:
- `response.created` events from datasets ending with `_responder_preguntas`
You can view the incoming events and any errors in the JSON component below.
""")
json_component = gr.JSON(label="Incoming events and errors:", value={})
gr.Timer(1, active=True).tick(read_next_event, outputs=json_component)
# Mount the Gradio app to the FastAPI server
gr.mount_gradio_app(server, demo, path="/")
# Start the FastAPI server
if __name__ == "__main__":
import uvicorn
uvicorn.run(server, host="0.0.0.0", port=7860) |