Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -20,42 +20,54 @@ async def update_validation_space_on_answer(response, type, timestamp):
|
|
20 |
Webhook listener that triggers when a new response is added to an answering space.
|
21 |
It will automatically update the corresponding validation space with the new response.
|
22 |
"""
|
23 |
-
# Store the event for display in the UI
|
24 |
-
incoming_events.put({"event": type, "data": str(response)})
|
25 |
-
|
26 |
-
# Get the record from the response
|
27 |
-
record = response.record
|
28 |
-
|
29 |
-
# Check if this is from an answering space
|
30 |
-
dataset_name = record.dataset.name
|
31 |
-
if not dataset_name.endswith("_responder_preguntas"):
|
32 |
-
print(f"Ignoring event from non-answering dataset: {dataset_name}")
|
33 |
-
return # Not an answering space, ignore
|
34 |
-
|
35 |
-
# Extract the country from the dataset name
|
36 |
-
country = dataset_name.replace("_responder_preguntas", "")
|
37 |
-
print(f"Processing response for country: {country}")
|
38 |
-
|
39 |
-
# Connect to the validation space
|
40 |
-
validation_dataset_name = f"{country}_validar_respuestas"
|
41 |
try:
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
except Exception as e:
|
45 |
-
print(f"Error
|
46 |
-
#
|
47 |
-
|
48 |
-
validation_dataset = create_validation_space(country)
|
49 |
-
|
50 |
-
# Create a validation record
|
51 |
-
validation_record = {
|
52 |
-
"question": record.fields["question"],
|
53 |
-
"answer": response.value,
|
54 |
-
}
|
55 |
-
|
56 |
-
# Add the record to the validation space
|
57 |
-
validation_dataset.records.log(records=[validation_record])
|
58 |
-
print(f"Added new response to validation space for {country}")
|
59 |
|
60 |
# Function to read the next event from the queue
|
61 |
def read_next_event():
|
@@ -76,12 +88,9 @@ with gr.Blocks() as demo:
|
|
76 |
The webhook listens for:
|
77 |
- `response.created` events from datasets ending with `_responder_preguntas`
|
78 |
|
79 |
-
|
80 |
-
named `[country]_validar_respuestas`.
|
81 |
-
|
82 |
-
You can view the incoming events in the JSON component below.
|
83 |
""")
|
84 |
-
json_component = gr.JSON(label="Incoming
|
85 |
gr.Timer(1, active=True).tick(read_next_event, outputs=json_component)
|
86 |
|
87 |
# Mount the Gradio app to the FastAPI server
|
|
|
20 |
Webhook listener that triggers when a new response is added to an answering space.
|
21 |
It will automatically update the corresponding validation space with the new response.
|
22 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
try:
|
24 |
+
# Store the event for display in the UI
|
25 |
+
incoming_events.put({"event": type, "timestamp": str(timestamp), "response_id": str(response.id)})
|
26 |
+
|
27 |
+
# Print the response object structure to understand its properties
|
28 |
+
print(f"Response object: {dir(response)}")
|
29 |
+
|
30 |
+
# Get the record from the response
|
31 |
+
record = response.record
|
32 |
+
|
33 |
+
# Check if this is from an answering space
|
34 |
+
dataset_name = record.dataset.name
|
35 |
+
if not dataset_name.endswith("_responder_preguntas"):
|
36 |
+
print(f"Ignoring event from non-answering dataset: {dataset_name}")
|
37 |
+
return # Not an answering space, ignore
|
38 |
+
|
39 |
+
# Extract the country from the dataset name
|
40 |
+
country = dataset_name.replace("_responder_preguntas", "")
|
41 |
+
print(f"Processing response for country: {country}")
|
42 |
+
|
43 |
+
# Connect to the validation space
|
44 |
+
validation_dataset_name = f"{country}_validar_respuestas"
|
45 |
+
try:
|
46 |
+
validation_dataset = client.datasets(validation_dataset_name)
|
47 |
+
print(f"Found validation dataset: {validation_dataset_name}")
|
48 |
+
except Exception as e:
|
49 |
+
print(f"Error connecting to validation dataset: {e}")
|
50 |
+
# You would need to import the create_validation_space function
|
51 |
+
from build_space import create_validation_space
|
52 |
+
validation_dataset = create_validation_space(country)
|
53 |
+
|
54 |
+
# Create a validation record with the correct attribute
|
55 |
+
# Instead of response.value, we need to find the correct attribute
|
56 |
+
# Based on the error, let's try to get the value differently
|
57 |
+
validation_record = {
|
58 |
+
"question": record.fields["question"],
|
59 |
+
# Use the first available response value - adjust this based on your actual response structure
|
60 |
+
"answer": response.values[0] if hasattr(response, 'values') and response.values else str(response),
|
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 |
except Exception as e:
|
68 |
+
print(f"Error in webhook handler: {e}")
|
69 |
+
# Store the error in the queue for display
|
70 |
+
incoming_events.put({"event": "error", "error": str(e)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Function to read the next event from the queue
|
73 |
def read_next_event():
|
|
|
88 |
The webhook listens for:
|
89 |
- `response.created` events from datasets ending with `_responder_preguntas`
|
90 |
|
91 |
+
You can view the incoming events and any errors in the JSON component below.
|
|
|
|
|
|
|
92 |
""")
|
93 |
+
json_component = gr.JSON(label="Incoming events and errors:", value={})
|
94 |
gr.Timer(1, active=True).tick(read_next_event, outputs=json_component)
|
95 |
|
96 |
# Mount the Gradio app to the FastAPI server
|