ouhenio commited on
Commit
88e9336
·
verified ·
1 Parent(s): 12c6f3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -12
app.py CHANGED
@@ -7,19 +7,93 @@ from collections import defaultdict
7
  from fastapi import FastAPI
8
  from functools import lru_cache
9
 
10
- # Initialize Argilla client with environment variables
11
  client = rg.Argilla(
12
  api_url=os.getenv("ARGILLA_API_URL", ""),
13
  api_key=os.getenv("ARGILLA_API_KEY", "")
14
  )
15
 
16
- # Dataset information - list all the datasets to track
17
- DATASETS = [
18
- "🇪🇸 España - ESP - Responder",
19
- # Add more datasets as needed
20
- ]
21
-
22
- # Cache results to avoid frequent API calls
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  @lru_cache(maxsize=32)
24
  def get_user_contributions_cached(cache_buster: int):
25
  return get_user_contributions()
@@ -30,13 +104,16 @@ def get_user_contributions():
30
  user_id_to_username = {}
31
 
32
  # Process each dataset
33
- for dataset_name in DATASETS:
 
 
 
 
34
  try:
35
  print(f"Processing dataset: {dataset_name}")
36
  dataset = client.datasets(dataset_name)
37
  records = list(dataset.records(with_responses=True))
38
 
39
- # Track contributions per user in this dataset
40
  dataset_contributions = defaultdict(int)
41
 
42
  for record in records:
@@ -47,7 +124,6 @@ def get_user_contributions():
47
  user_id = answer["user_id"]
48
  dataset_contributions[user_id] += 1
49
 
50
- # Get username if not already cached
51
  if user_id not in user_id_to_username:
52
  try:
53
  user = client.users(id=user_id)
@@ -56,7 +132,6 @@ def get_user_contributions():
56
  print(f"Error getting username for {user_id}: {e}")
57
  user_id_to_username[user_id] = f"User-{user_id[:8]}"
58
 
59
- # Add dataset contributions to overall user stats
60
  for user_id, count in dataset_contributions.items():
61
  username = user_id_to_username.get(user_id, f"User-{user_id[:8]}")
62
  user_contributions[user_id]["username"] = username
 
7
  from fastapi import FastAPI
8
  from functools import lru_cache
9
 
 
10
  client = rg.Argilla(
11
  api_url=os.getenv("ARGILLA_API_URL", ""),
12
  api_key=os.getenv("ARGILLA_API_KEY", "")
13
  )
14
 
15
+ countries = {
16
+ "Argentina": {
17
+ "iso": "ARG",
18
+ "emoji": "🇦🇷"
19
+ },
20
+ "Bolivia": {
21
+ "iso": "BOL",
22
+ "emoji": "🇧🇴"
23
+ },
24
+ "Chile": {
25
+ "iso": "CHL",
26
+ "emoji": "🇨🇱"
27
+ },
28
+ "Colombia": {
29
+ "iso": "COL",
30
+ "emoji": "🇨🇴"
31
+ },
32
+ "Costa Rica": {
33
+ "iso": "CRI",
34
+ "emoji": "🇨🇷"
35
+ },
36
+ "Cuba": {
37
+ "iso": "CUB",
38
+ "emoji": "🇨🇺"
39
+ },
40
+ "Ecuador": {
41
+ "iso": "ECU",
42
+ "emoji": "🇪🇨"
43
+ },
44
+ "El Salvador": {
45
+ "iso": "SLV",
46
+ "emoji": "🇸🇻"
47
+ },
48
+ "España": {
49
+ "iso": "ESP",
50
+ "emoji": "🇪🇸"
51
+ },
52
+ "Guatemala": {
53
+ "iso": "GTM",
54
+ "emoji": "🇬🇹"
55
+ },
56
+ "Honduras": {
57
+ "iso": "HND",
58
+ "emoji": "🇭🇳"
59
+ },
60
+ "México": {
61
+ "iso": "MEX",
62
+ "emoji": "🇲🇽"
63
+ },
64
+ "Nicaragua": {
65
+ "iso": "NIC",
66
+ "emoji": "🇳🇮"
67
+ },
68
+ "Panamá": {
69
+ "iso": "PAN",
70
+ "emoji": "🇵🇦"
71
+ },
72
+ "Paraguay": {
73
+ "iso": "PRY",
74
+ "emoji": "🇵🇾"
75
+ },
76
+ "Perú": {
77
+ "iso": "PER",
78
+ "emoji": "🇵🇪"
79
+ },
80
+ "Puerto Rico": {
81
+ "iso": "PRI",
82
+ "emoji": "🇵🇷"
83
+ },
84
+ "República Dominicana": {
85
+ "iso": "DOM",
86
+ "emoji": "🇩🇴"
87
+ },
88
+ "Uruguay": {
89
+ "iso": "URY",
90
+ "emoji": "🇺🇾"
91
+ },
92
+ "Venezuela": {
93
+ "iso": "VEN",
94
+ "emoji": "🇻🇪"
95
+ }
96
+ }
97
  @lru_cache(maxsize=32)
98
  def get_user_contributions_cached(cache_buster: int):
99
  return get_user_contributions()
 
104
  user_id_to_username = {}
105
 
106
  # Process each dataset
107
+ for country in countries.keys():
108
+ iso = countries[country]["iso"]
109
+ emoji = countries[country]["emoji"]
110
+
111
+ dataset_name = f"{emoji} {country} - {iso} - Responder"
112
  try:
113
  print(f"Processing dataset: {dataset_name}")
114
  dataset = client.datasets(dataset_name)
115
  records = list(dataset.records(with_responses=True))
116
 
 
117
  dataset_contributions = defaultdict(int)
118
 
119
  for record in records:
 
124
  user_id = answer["user_id"]
125
  dataset_contributions[user_id] += 1
126
 
 
127
  if user_id not in user_id_to_username:
128
  try:
129
  user = client.users(id=user_id)
 
132
  print(f"Error getting username for {user_id}: {e}")
133
  user_id_to_username[user_id] = f"User-{user_id[:8]}"
134
 
 
135
  for user_id, count in dataset_contributions.items():
136
  username = user_id_to_username.get(user_id, f"User-{user_id[:8]}")
137
  user_contributions[user_id]["username"] = username