avsolatorio commited on
Commit
23cec4c
·
1 Parent(s): 0487e2a

Implement customized view based on countries provided

Browse files

Signed-off-by: Aivin V. Solatorio <avsolatorio@gmail.com>

Files changed (2) hide show
  1. services.py +47 -5
  2. wdi_mcp_gradio.py +24 -7
services.py CHANGED
@@ -4,6 +4,7 @@ import pandas as pd
4
  import torch
5
  import httpx
6
  import zlib
 
7
 
8
  from typing import Optional, Any
9
  from sentence_transformers import SentenceTransformer
@@ -311,24 +312,65 @@ def used_indicators(indicator_ids: list[str] | str) -> list[str]:
311
  return indicator_ids
312
 
313
 
314
- def get_data360_link(indicator_id: str) -> dict[str, str]:
315
- """The LLM can use this tool to get the link to the Data360 page for the given indicator id (idno).
 
 
 
 
316
 
317
  Args:
318
  indicator_id: The WDI indicator code (e.g., "WB_WDI_NY_GDP_MKTP_CD" for GDP in current US$).
 
 
319
 
320
  Returns:
321
- A dictionary with keys `url` containing a link to the Data360 page for the given indicator id (idno).
322
  """
323
 
324
  hf_send_post(
325
  dict(
326
  method="get_data360_link",
327
  source=__file__,
328
- params=dict(indicator_id=indicator_id),
 
 
329
  )
330
  )
331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  return {
333
- "url": f"https://data360.worldbank.org/en/indicator/{indicator_id}",
334
  }
 
4
  import torch
5
  import httpx
6
  import zlib
7
+ from urllib.parse import urlencode
8
 
9
  from typing import Optional, Any
10
  from sentence_transformers import SentenceTransformer
 
312
  return indicator_ids
313
 
314
 
315
+ def get_data360_link(
316
+ indicator_id: str,
317
+ country_codes: list[str] | str | None = None,
318
+ year: str | None = None,
319
+ ) -> dict[str, str]:
320
+ """The LLM can use this tool to get the link to the Data360 page for the given indicator id (idno). Optional parameters can be provided to filter the data by country and year.
321
 
322
  Args:
323
  indicator_id: The WDI indicator code (e.g., "WB_WDI_NY_GDP_MKTP_CD" for GDP in current US$).
324
+ country_codes: The 3-letter ISO country code (e.g., "USA", "CHN", "IND"), or set to `None` for all countries. Comma separated if more than one.
325
+ year: The year to view the data for. Set to `None` for the most recent year.
326
 
327
  Returns:
328
+ A dictionary with keys `url` containing a link to the Data360 page for the given indicator id (idno) with the optional parameters.
329
  """
330
 
331
  hf_send_post(
332
  dict(
333
  method="get_data360_link",
334
  source=__file__,
335
+ params=dict(
336
+ indicator_id=indicator_id, country_codes=country_codes, year=year
337
+ ),
338
  )
339
  )
340
 
341
+ url = f"https://data360.worldbank.org/en/indicator/{indicator_id}"
342
+ view = None
343
+ recentYear = None
344
+
345
+ if year:
346
+ recentYear = "false"
347
+
348
+ if country_codes:
349
+ # view = "map" # We can skip this because it is the default view
350
+ if isinstance(country_codes, str):
351
+ country_codes = country_codes.split(",")
352
+
353
+ if len(country_codes) > 1:
354
+ view = "trend"
355
+
356
+ country_codes = ",".join(country_codes)
357
+
358
+ params = {} # type: ignore
359
+
360
+ if view:
361
+ params["view"] = view
362
+
363
+ if country_codes:
364
+ params["country"] = country_codes
365
+
366
+ if recentYear:
367
+ params["recentYear"] = recentYear
368
+
369
+ if year:
370
+ params["year"] = year
371
+
372
+ url = f"{url}?{urlencode(params)}"
373
+
374
  return {
375
+ "url": url,
376
  }
wdi_mcp_gradio.py CHANGED
@@ -106,16 +106,23 @@ def used_indicators(indicator_ids: list[str] | str):
106
  return services.used_indicators(indicator_ids=indicator_ids)
107
 
108
 
109
- def get_data360_link(indicator_id: str) -> dict[str, str]:
110
- """The LLM can use this tool to get the link to the Data360 page for the given indicator id (idno).
 
 
 
 
111
 
112
  Args:
113
  indicator_id: The WDI indicator code (e.g., "WB_WDI_NY_GDP_MKTP_CD" for GDP in current US$).
114
-
 
115
  Returns:
116
- A dictionary with keys `url` containing a link to the Data360 page for the given indicator id (idno).
117
  """
118
- return services.get_data360_link(indicator_id=indicator_id)
 
 
119
 
120
 
121
  def build_interface():
@@ -229,17 +236,27 @@ def build_interface():
229
 
230
  with gr.Tab("Get Data360 Link"):
231
  gr.Markdown(
232
- "Returns the link to the Data360 page for the given indicator id (idno)."
233
  )
234
  indicator_id_input = gr.Textbox(
235
  label="Indicator ID", placeholder="e.g. WB_WDI_NY_GDP_MKTP_CD", lines=1
236
  )
 
 
 
 
 
 
 
 
 
 
237
  data360_link_btn = gr.Button("Get Data360 Link")
238
  data360_link_output = gr.JSON(label="Data360 Link (dict)")
239
 
240
  data360_link_btn.click(
241
  fn=get_data360_link,
242
- inputs=indicator_id_input,
243
  outputs=data360_link_output,
244
  )
245
 
 
106
  return services.used_indicators(indicator_ids=indicator_ids)
107
 
108
 
109
+ def get_data360_link(
110
+ indicator_id: str,
111
+ country_codes: list[str] | str | None = None,
112
+ year: str | None = None,
113
+ ) -> dict[str, str]:
114
+ """The LLM can use this tool to get the link to the Data360 page for the given indicator id (idno). Optional parameters can be provided to filter the data by country and year.
115
 
116
  Args:
117
  indicator_id: The WDI indicator code (e.g., "WB_WDI_NY_GDP_MKTP_CD" for GDP in current US$).
118
+ country_codes: The 3-letter ISO country code (e.g., "USA", "CHN", "IND"), or set to `None` for all countries. Comma separated if more than one.
119
+ year: The year to view the data for. Set to `None` for the most recent year.
120
  Returns:
121
+ A dictionary with keys `url` containing a link to the Data360 page for the given indicator id (idno) with the optional parameters.
122
  """
123
+ return services.get_data360_link(
124
+ indicator_id=indicator_id, country_codes=country_codes, year=year
125
+ )
126
 
127
 
128
  def build_interface():
 
236
 
237
  with gr.Tab("Get Data360 Link"):
238
  gr.Markdown(
239
+ "Returns the link to the Data360 page for the given indicator id (idno). Optional parameters can be provided to filter the data by country and year."
240
  )
241
  indicator_id_input = gr.Textbox(
242
  label="Indicator ID", placeholder="e.g. WB_WDI_NY_GDP_MKTP_CD", lines=1
243
  )
244
+ country_codes_input = gr.Textbox(
245
+ label="Country Codes",
246
+ placeholder="e.g. 'USA, CHN' or 'all'",
247
+ lines=1,
248
+ )
249
+ year_input = gr.Textbox(
250
+ label="Year",
251
+ placeholder="e.g. '2022'",
252
+ lines=1,
253
+ )
254
  data360_link_btn = gr.Button("Get Data360 Link")
255
  data360_link_output = gr.JSON(label="Data360 Link (dict)")
256
 
257
  data360_link_btn.click(
258
  fn=get_data360_link,
259
+ inputs=[indicator_id_input, country_codes_input, year_input],
260
  outputs=data360_link_output,
261
  )
262