ouhenio commited on
Commit
738289f
·
verified ·
1 Parent(s): 06378db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -33
app.py CHANGED
@@ -4,19 +4,96 @@ import gradio as gr
4
  import argilla as rg
5
  from argilla.webhooks import webhook_listener
6
 
7
- # Initialize Argilla client
8
  client = rg.Argilla(
9
  api_url=os.getenv("ARGILLA_API_URL"),
10
  api_key=os.getenv("ARGILLA_API_KEY"),
11
  )
12
-
13
- # Get the webhook server
14
  server = rg.get_webhook_server()
15
-
16
- # Queue to store events for display
17
  incoming_events = Queue()
18
 
19
- # Set up the webhook listener for response creation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @webhook_listener(events=["response.created"])
21
  async def update_validation_space_on_answer(response, type, timestamp):
22
  """
@@ -24,53 +101,50 @@ async def update_validation_space_on_answer(response, type, timestamp):
24
  It will automatically update the corresponding validation space with the new response.
25
  """
26
  try:
27
- # Store the event for display in the UI
28
  incoming_events.put({"event": type, "timestamp": str(timestamp)})
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
  response_dict = response.to_dict()
55
- answer = response_dict["values"]['text']['value']
 
 
56
 
57
- # Get the user ID of the original responder
58
  original_user_id = str(response.user_id)
59
-
60
- # Create a validation record with the correct attribute
61
- # Instead of response.value, we need to find the correct attribute
62
- # Based on the error, let's try to get the value differently
63
- validation_record = {
64
- "question": record.fields["question"],
65
- "answer": answer,
66
- "metadata": {
67
- "original_responder_id": original_user_id,
68
- "original_dataset": dataset_name
69
- }
70
- }
 
 
71
 
72
  # Add the record to the validation space
73
- validation_dataset.records.log(records=[validation_record])
74
  print(f"Added new response to validation space for {country}")
75
 
76
  except Exception as e:
 
4
  import argilla as rg
5
  from argilla.webhooks import webhook_listener
6
 
 
7
  client = rg.Argilla(
8
  api_url=os.getenv("ARGILLA_API_URL"),
9
  api_key=os.getenv("ARGILLA_API_KEY"),
10
  )
 
 
11
  server = rg.get_webhook_server()
 
 
12
  incoming_events = Queue()
13
 
14
+ countries = {
15
+ "Argentina": {
16
+ "iso": "ARG",
17
+ "emoji": "🇦🇷"
18
+ },
19
+ "Bolivia": {
20
+ "iso": "BOL",
21
+ "emoji": "🇧🇴"
22
+ },
23
+ "Chile": {
24
+ "iso": "CHL",
25
+ "emoji": "🇨🇱"
26
+ },
27
+ "Colombia": {
28
+ "iso": "COL",
29
+ "emoji": "🇨🇴"
30
+ },
31
+ "Costa Rica": {
32
+ "iso": "CRI",
33
+ "emoji": "🇨🇷"
34
+ },
35
+ "Cuba": {
36
+ "iso": "CUB",
37
+ "emoji": "🇨🇺"
38
+ },
39
+ "Ecuador": {
40
+ "iso": "ECU",
41
+ "emoji": "🇪🇨"
42
+ },
43
+ "El Salvador": {
44
+ "iso": "SLV",
45
+ "emoji": "🇸🇻"
46
+ },
47
+ "España": {
48
+ "iso": "ESP",
49
+ "emoji": "🇪🇸"
50
+ },
51
+ "Guatemala": {
52
+ "iso": "GTM",
53
+ "emoji": "🇬🇹"
54
+ },
55
+ "Honduras": {
56
+ "iso": "HND",
57
+ "emoji": "🇭🇳"
58
+ },
59
+ "México": {
60
+ "iso": "MEX",
61
+ "emoji": "🇲🇽"
62
+ },
63
+ "Nicaragua": {
64
+ "iso": "NIC",
65
+ "emoji": "🇳🇮"
66
+ },
67
+ "Panamá": {
68
+ "iso": "PAN",
69
+ "emoji": "🇵🇦"
70
+ },
71
+ "Paraguay": {
72
+ "iso": "PRY",
73
+ "emoji": "🇵🇾"
74
+ },
75
+ "Perú": {
76
+ "iso": "PER",
77
+ "emoji": "🇵🇪"
78
+ },
79
+ "Puerto Rico": {
80
+ "iso": "PRI",
81
+ "emoji": "🇵🇷"
82
+ },
83
+ "República Dominicana": {
84
+ "iso": "DOM",
85
+ "emoji": "🇩🇴"
86
+ },
87
+ "Uruguay": {
88
+ "iso": "URY",
89
+ "emoji": "🇺🇾"
90
+ },
91
+ "Venezuela": {
92
+ "iso": "VEN",
93
+ "emoji": "🇻🇪"
94
+ }
95
+ }
96
+
97
  @webhook_listener(events=["response.created"])
98
  async def update_validation_space_on_answer(response, type, timestamp):
99
  """
 
101
  It will automatically update the corresponding validation space with the new response.
102
  """
103
  try:
 
104
  incoming_events.put({"event": type, "timestamp": str(timestamp)})
105
 
 
106
  record = response.record
107
 
 
108
  dataset_name = record.dataset.name
109
+ if not dataset_name.endswith("Responder"):
110
  print(f"Ignoring event from non-answering dataset: {dataset_name}")
111
  return # Not an answering space, ignore
112
 
113
+ country = " ".join(dataset_name.split("-").split(" ")[1:])
 
114
  print(f"Processing response for country: {country}")
115
+
116
+ iso = countries[country]["iso"]
117
+ emoji = countries[country]["emoji"]
118
+ validation_dataset_name = f"{emoji} {country} - {iso} - Responder"
119
  try:
120
  validation_dataset = client.datasets(validation_dataset_name)
121
  print(f"Found validation dataset: {validation_dataset_name}")
122
  except Exception as e:
123
  print(f"Error connecting to validation dataset: {e}")
 
 
 
124
 
125
  response_dict = response.to_dict()
126
+ answer_1 = response_dict["values"]['answer_1']['value']
127
+ answer_2 = response_dict["values"]['answer_2']['value']
128
+ answer_3 = response_dict["values"]['answer_3']['value']
129
 
 
130
  original_user_id = str(response.user_id)
131
+
132
+ new_records = []
133
+
134
+ for answer in [answer_1, answer_2, answer_3]:
135
+ if answer:
136
+ validation_record = {
137
+ "question": record.fields["question"],
138
+ "answer": answer,
139
+ "metadata": {
140
+ "original_responder_id": original_user_id,
141
+ "original_dataset": dataset_name
142
+ }
143
+ }
144
+ new_records.append(answer)
145
 
146
  # Add the record to the validation space
147
+ validation_dataset.records.log(records=new_records)
148
  print(f"Added new response to validation space for {country}")
149
 
150
  except Exception as e: