zvl commited on
Commit
1bbc1f1
·
verified ·
1 Parent(s): a36c5e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -9,45 +9,54 @@ from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def buscar_datos_gob(term: str, category: str = None, format: str = "json", page_size: int = 10, page: int = 0, sort: str = "title") -> dict:
13
  """
14
- A tool that searches for datasets in the datos.gob.es API.
15
 
16
  Args:
17
- term: The search term to filter the datasets.
18
- category: The category/topic to filter datasets (e.g., "hacienda").
19
- format: The format of the datasets (json, xml, csv, rdf). Default is json.
20
- page_size: The number of results per page (maximum: 50).
21
- page: The page number for pagination of results.
22
- sort: The field by which to sort the results.
23
 
24
  Returns:
25
- A dictionary containing the search results from the datos.gob.es API.
26
  """
27
  import requests
28
 
29
  base_url = "https://datos.gob.es/apidata/catalog/dataset"
30
-
31
- if category:
32
- base_url += f"/theme/{category}"
33
-
34
  params = {
35
  "q": term,
36
  "_pageSize": page_size,
37
- "_page": page,
38
- "_sort": sort
39
  }
40
-
41
  headers = {
42
- "Accept": f"application/{format}"
 
43
  }
44
 
45
  try:
46
  response = requests.get(base_url, params=params, headers=headers)
47
  response.raise_for_status()
48
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  except requests.exceptions.RequestException as e:
50
- return {"error": f"Error in the request: {str(e)}"}
 
51
 
52
 
53
  final_answer = FinalAnswerTool()
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ def buscar_datos_gob(term: str, page_size: int = 10, page: int = 0) -> dict:
13
  """
14
+ Search for datasets in the datos.gob.es API.
15
 
16
  Args:
17
+ term: The search term for datasets.
18
+ page_size: Number of results per page (max 50).
19
+ page: The page number for pagination.
 
 
 
20
 
21
  Returns:
22
+ A dictionary with the relevant datasets, showing the title, description, publisher, and access URL.
23
  """
24
  import requests
25
 
26
  base_url = "https://datos.gob.es/apidata/catalog/dataset"
 
 
 
 
27
  params = {
28
  "q": term,
29
  "_pageSize": page_size,
30
+ "_page": page
 
31
  }
 
32
  headers = {
33
+ "Accept": "application/json",
34
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
35
  }
36
 
37
  try:
38
  response = requests.get(base_url, params=params, headers=headers)
39
  response.raise_for_status()
40
+ data = response.json()
41
+
42
+ # Extraer los datasets de la respuesta
43
+ datasets = []
44
+ for item in data.get("result", {}).get("items", []):
45
+ dataset_info = {
46
+ "title": item.get("title", "No Title"),
47
+ "description": item.get("description", {}).get("text", "No Description"),
48
+ "publisher": item.get("publisher", "Unknown Publisher"),
49
+ "accessURL": item.get("distribution", {}).get("accessURL", "No URL Available"),
50
+ "modified": item.get("modified", "No Date Available"),
51
+ "license": item.get("license", "No License Available")
52
+ }
53
+ datasets.append(dataset_info)
54
+
55
+ return {"datasets": datasets}
56
+
57
  except requests.exceptions.RequestException as e:
58
+ return {"error": f"Request Error: {str(e)}"}
59
+
60
 
61
 
62
  final_answer = FinalAnswerTool()