Update functions.py
Browse files- functions.py +45 -31
functions.py
CHANGED
|
@@ -28,26 +28,6 @@ def generar_grafica_barras(tareas_seleccionadas, MODALIDAD_TAREAS):
|
|
| 28 |
except Exception as e:
|
| 29 |
raise ValueError(f"Error al generar gráfica: {str(e)}")
|
| 30 |
|
| 31 |
-
# Buscar datasets en HuggingFace
|
| 32 |
-
def buscar_datasets(tareas_seleccionadas, filtro_tamaño, filtro_licencia):
|
| 33 |
-
try:
|
| 34 |
-
query = "+".join(tareas_seleccionadas)
|
| 35 |
-
url = f"https://huggingface.co/api/datasets?search={query}"
|
| 36 |
-
response = requests.get(url)
|
| 37 |
-
response.raise_for_status()
|
| 38 |
-
datasets = response.json()
|
| 39 |
-
|
| 40 |
-
resultados = []
|
| 41 |
-
for dataset in datasets:
|
| 42 |
-
if filtro_tamaño and dataset.get("size_categories") != filtro_tamaño:
|
| 43 |
-
continue
|
| 44 |
-
if filtro_licencia and dataset.get("license") != filtro_licencia:
|
| 45 |
-
continue
|
| 46 |
-
resultados.append(f"- {dataset['id']}: {dataset['description']}")
|
| 47 |
-
return "\n".join(resultados)
|
| 48 |
-
except requests.exceptions.RequestException as e:
|
| 49 |
-
raise ValueError(f"Error al buscar datasets: {str(e)}")
|
| 50 |
-
|
| 51 |
# Generar encabezado
|
| 52 |
def generar_encabezado(tareas_seleccionadas):
|
| 53 |
MODALIDAD_TAREAS = cargar_modalidades_tareas()
|
|
@@ -84,31 +64,65 @@ def generar_encabezado(tareas_seleccionadas):
|
|
| 84 |
|
| 85 |
return ",".join(columnas_ordenadas)
|
| 86 |
|
| 87 |
-
|
|
|
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
columnas = encabezado.split(",")
|
| 93 |
filas = []
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
for dataset_linea in datasets_seleccionados.split("\n"):
|
| 97 |
-
dataset_id = dataset_linea.strip("- ").split(":")[0] # Extraer ID
|
| 98 |
-
|
| 99 |
try:
|
|
|
|
| 100 |
dataset = load_dataset(dataset_id, split="train")
|
|
|
|
|
|
|
| 101 |
inicio = (pagina_actual - 1) * filas_por_pagina
|
| 102 |
fin = pagina_actual * filas_por_pagina
|
| 103 |
|
| 104 |
for i, fila in enumerate(dataset[inicio:fin]):
|
| 105 |
valores = []
|
| 106 |
-
for col in columnas
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
-
filas.append(f"Error en
|
| 112 |
|
| 113 |
contenido_csv = "\n".join([encabezado] + filas)
|
| 114 |
return contenido_csv
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
raise ValueError(f"Error al generar gráfica: {str(e)}")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Generar encabezado
|
| 32 |
def generar_encabezado(tareas_seleccionadas):
|
| 33 |
MODALIDAD_TAREAS = cargar_modalidades_tareas()
|
|
|
|
| 64 |
|
| 65 |
return ",".join(columnas_ordenadas)
|
| 66 |
|
| 67 |
+
# Buscar datasets en HuggingFace con validación de columnas
|
| 68 |
+
def buscar_datasets(tareas_seleccionadas, filtro_tamaño, filtro_licencia):
|
| 69 |
try:
|
| 70 |
+
# Construir query con tareas y modalidades
|
| 71 |
+
query = "+".join(tareas_seleccionadas)
|
| 72 |
+
url = f"https://huggingface.co/api/datasets?search={query}"
|
| 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 |
+
# Verificar filtros básicos
|
| 81 |
+
if filtro_tamaño and dataset.get("size_categories") != filtro_tamaño:
|
| 82 |
+
continue
|
| 83 |
+
if filtro_licencia and dataset.get("license") != filtro_licencia:
|
| 84 |
+
continue
|
| 85 |
+
|
| 86 |
+
# Verificar que el dataset tenga columnas relevantes (ej. 'text' para NLP) <button class="citation-flag" data-index="1">
|
| 87 |
+
dataset_info = requests.get(f"https://huggingface.co/api/datasets/{dataset['id']}").json()
|
| 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 # Devolver lista de tuples para CheckboxGroup
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
raise ValueError(f"Error al buscar datasets: {str(e)}")
|
| 97 |
+
|
| 98 |
+
# Generar dataset combinado
|
| 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: # Ahora es una lista de IDs 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 |
|
| 113 |
for i, fila in enumerate(dataset[inicio:fin]):
|
| 114 |
valores = []
|
| 115 |
+
for col in columnas:
|
| 116 |
+
if col == "id":
|
| 117 |
+
valores.append(f"id_{inicio + i}")
|
| 118 |
+
elif col in columnas_dataset:
|
| 119 |
+
valores.append(str(fila[col]))
|
| 120 |
+
else:
|
| 121 |
+
valores.append("valor_default") # Rellenar con default si falta la columna <button class="citation-flag" data-index="4">
|
| 122 |
+
filas.append(",".join(valores))
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
+
filas.append(f"Error en {dataset_id}: {str(e)}")
|
| 126 |
|
| 127 |
contenido_csv = "\n".join([encabezado] + filas)
|
| 128 |
return contenido_csv
|