Update functions.py
Browse files- functions.py +40 -26
functions.py
CHANGED
|
@@ -64,49 +64,66 @@ def generar_encabezado(tareas_seleccionadas):
|
|
| 64 |
|
| 65 |
return ",".join(columnas_ordenadas)
|
| 66 |
|
| 67 |
-
# Buscar datasets en HuggingFace con validación
|
| 68 |
def buscar_datasets(tareas_seleccionadas, filtro_tamaño, filtro_licencia):
|
| 69 |
try:
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
response = requests.get(url)
|
| 74 |
response.raise_for_status()
|
| 75 |
datasets = response.json()
|
| 76 |
|
| 77 |
-
# Filtrar datasets útiles
|
| 78 |
datasets_utiles = []
|
| 79 |
for dataset in datasets:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
if "features" in dataset_info:
|
| 89 |
-
datasets_utiles.append(
|
| 90 |
-
(dataset['id'], f"{dataset['id']}: {dataset['description']}") # Formato (valor, etiqueta)
|
| 91 |
-
)
|
| 92 |
|
| 93 |
-
return datasets_utiles
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
raise ValueError(f"Error al buscar datasets: {str(e)}")
|
| 97 |
|
| 98 |
-
|
|
|
|
| 99 |
def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_por_pagina=5):
|
| 100 |
try:
|
| 101 |
columnas = encabezado.split(",")
|
| 102 |
filas = []
|
| 103 |
|
| 104 |
-
for dataset_id in datasets_seleccionados:
|
| 105 |
try:
|
| 106 |
-
# Cargar dataset y verificar columnas
|
| 107 |
dataset = load_dataset(dataset_id, split="train")
|
| 108 |
columnas_dataset = dataset.features.keys()
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
inicio = (pagina_actual - 1) * filas_por_pagina
|
| 111 |
fin = pagina_actual * filas_por_pagina
|
| 112 |
|
|
@@ -114,18 +131,15 @@ def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_p
|
|
| 114 |
valores = []
|
| 115 |
for col in columnas:
|
| 116 |
if col == "id":
|
| 117 |
-
valores.append(
|
| 118 |
-
elif col in columnas_dataset:
|
| 119 |
-
valores.append(str(fila[col]))
|
| 120 |
else:
|
| 121 |
-
valores.append(
|
| 122 |
filas.append(",".join(valores))
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
filas.append(f"Error en {dataset_id}: {str(e)}")
|
| 126 |
|
| 127 |
-
|
| 128 |
-
return contenido_csv
|
| 129 |
|
| 130 |
except Exception as e:
|
| 131 |
raise ValueError(f"Error al generar el dataset: {str(e)}")
|
|
|
|
| 64 |
|
| 65 |
return ",".join(columnas_ordenadas)
|
| 66 |
|
| 67 |
+
# Buscar datasets en HuggingFace con validación avanzada
|
| 68 |
def buscar_datasets(tareas_seleccionadas, filtro_tamaño, filtro_licencia):
|
| 69 |
try:
|
| 70 |
+
# Traducir tareas al inglés para mejorar la búsqueda <button class="citation-flag" data-index="1">
|
| 71 |
+
tareas_ingles = [tarea.lower().replace(" ", "_") for tarea in tareas_seleccionadas]
|
| 72 |
+
query = "+".join(tareas_ingles)
|
| 73 |
+
url = f"https://huggingface.co/api/datasets?search={query}&full=True" # Parámetro 'full' para más detalles <button class="citation-flag" data-index="1">
|
| 74 |
response = requests.get(url)
|
| 75 |
response.raise_for_status()
|
| 76 |
datasets = response.json()
|
| 77 |
|
|
|
|
| 78 |
datasets_utiles = []
|
| 79 |
for dataset in datasets:
|
| 80 |
+
try:
|
| 81 |
+
# Verificar filtros
|
| 82 |
+
if filtro_tamaño and dataset.get("size_categories", "").lower() != filtro_tamaño:
|
| 83 |
+
continue
|
| 84 |
+
if filtro_licencia and dataset.get("license", "").lower() != filtro_licencia:
|
| 85 |
+
continue
|
| 86 |
+
|
| 87 |
+
# Verificar columnas relevantes
|
| 88 |
+
dataset_info = requests.get(f"https://huggingface.co/api/datasets/{dataset['id']}").json()
|
| 89 |
+
if "features" in dataset_info:
|
| 90 |
+
columnas_dataset = list(dataset_info["features"].keys())
|
| 91 |
+
# Priorizar datasets con columnas útiles (ej. 'text' para NLP) <button class="citation-flag" data-index="1">
|
| 92 |
+
if any(col in columnas_dataset for col in ["text", "image", "audio", "code"]):
|
| 93 |
+
datasets_utiles.append(
|
| 94 |
+
(dataset['id'], f"{dataset['id']} ({dataset['tags']}) - {dataset['description']}")
|
| 95 |
+
)
|
| 96 |
|
| 97 |
+
except requests.exceptions.RequestException:
|
| 98 |
+
continue # Ignorar datasets con información inaccesible
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
return datasets_utiles
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
raise ValueError(f"Error al buscar datasets: {str(e)}")
|
| 104 |
|
| 105 |
+
|
| 106 |
+
# Generar dataset combinado con validación de columnas
|
| 107 |
def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_por_pagina=5):
|
| 108 |
try:
|
| 109 |
columnas = encabezado.split(",")
|
| 110 |
filas = []
|
| 111 |
|
| 112 |
+
for dataset_id in datasets_seleccionados:
|
| 113 |
try:
|
|
|
|
| 114 |
dataset = load_dataset(dataset_id, split="train")
|
| 115 |
columnas_dataset = dataset.features.keys()
|
| 116 |
|
| 117 |
+
# Mapear columnas del dataset a las del encabezado
|
| 118 |
+
mapeo = {}
|
| 119 |
+
for col in columnas:
|
| 120 |
+
if col == "id":
|
| 121 |
+
mapeo[col] = lambda idx: f"id_{idx}"
|
| 122 |
+
elif col in columnas_dataset:
|
| 123 |
+
mapeo[col] = lambda fila, c=col: str(fila[c])
|
| 124 |
+
else:
|
| 125 |
+
mapeo[col] = lambda _: "valor_default" # Valor por defecto <button class="citation-flag" data-index="4">
|
| 126 |
+
|
| 127 |
inicio = (pagina_actual - 1) * filas_por_pagina
|
| 128 |
fin = pagina_actual * filas_por_pagina
|
| 129 |
|
|
|
|
| 131 |
valores = []
|
| 132 |
for col in columnas:
|
| 133 |
if col == "id":
|
| 134 |
+
valores.append(mapeo[col](i))
|
|
|
|
|
|
|
| 135 |
else:
|
| 136 |
+
valores.append(mapeo[col](fila))
|
| 137 |
filas.append(",".join(valores))
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
filas.append(f"Error en {dataset_id}: {str(e)}")
|
| 141 |
|
| 142 |
+
return "\n".join([encabezado] + filas)
|
|
|
|
| 143 |
|
| 144 |
except Exception as e:
|
| 145 |
raise ValueError(f"Error al generar el dataset: {str(e)}")
|