Spaces:
Running
Running
Update app.py
Browse files
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,
|
13 |
"""
|
14 |
-
|
15 |
|
16 |
Args:
|
17 |
-
term: The search term
|
18 |
-
|
19 |
-
|
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
|
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":
|
|
|
43 |
}
|
44 |
|
45 |
try:
|
46 |
response = requests.get(base_url, params=params, headers=headers)
|
47 |
response.raise_for_status()
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
except requests.exceptions.RequestException as e:
|
50 |
-
return {"error": f"Error
|
|
|
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()
|