Create functions.py
Browse files- functions.py +90 -0
functions.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
# Cargar modalidades y tareas desde un archivo JSON
|
| 7 |
+
with open("modalidades_tareas.json", "r") as file:
|
| 8 |
+
MODALIDAD_TAREAS = json.load(file)
|
| 9 |
+
|
| 10 |
+
def validar_modalidades_tareas(modalidades_tareas):
|
| 11 |
+
for modalidad, tareas in modalidades_tareas.items():
|
| 12 |
+
if not isinstance(modalidad, str) or not isinstance(tareas, list):
|
| 13 |
+
raise ValueError(f"Formato incorrecto para la modalidad: {modalidad}")
|
| 14 |
+
for tarea in tareas:
|
| 15 |
+
if not isinstance(tarea, str):
|
| 16 |
+
raise ValueError(f"Formato incorrecto para la tarea: {tarea} en la modalidad {modalidad}")
|
| 17 |
+
|
| 18 |
+
# Validar el diccionario
|
| 19 |
+
validar_modalidades_tareas(MODALIDAD_TAREAS)
|
| 20 |
+
|
| 21 |
+
# Funci贸n para generar la gr谩fica de barras
|
| 22 |
+
def generar_grafica_barras(tareas_seleccionadas):
|
| 23 |
+
# Contar la cantidad de tareas seleccionadas por modalidad
|
| 24 |
+
conteo_modalidades = {}
|
| 25 |
+
for modalidad, tareas in MODALIDAD_TAREAS.items():
|
| 26 |
+
conteo_modalidades[modalidad] = len([tarea for tarea in tareas if tarea in tareas_seleccionadas])
|
| 27 |
+
|
| 28 |
+
modalidades = list(conteo_modalidades.keys())
|
| 29 |
+
cantidades = [conteo_modalidades[modalidad] for modalidad in modalidades]
|
| 30 |
+
|
| 31 |
+
# Crear la gr谩fica de barras horizontal
|
| 32 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 33 |
+
ax.barh(modalidades, cantidades, color='skyblue', edgecolor='black')
|
| 34 |
+
ax.set_xlabel('Cantidad de Tareas Seleccionadas')
|
| 35 |
+
ax.set_ylabel('Modalidades')
|
| 36 |
+
ax.set_title('Distribuci贸n de Tareas Seleccionadas por Modalidad')
|
| 37 |
+
ax.invert_yaxis() # Invertir el eje Y para que las modalidades aparezcan de arriba hacia abajo
|
| 38 |
+
|
| 39 |
+
return fig
|
| 40 |
+
|
| 41 |
+
# Funci贸n para generar el encabezado del CSV
|
| 42 |
+
def generar_encabezado(tareas_seleccionadas):
|
| 43 |
+
if not tareas_seleccionadas:
|
| 44 |
+
raise ValueError("Debes seleccionar al menos una tarea.")
|
| 45 |
+
|
| 46 |
+
columnas = ["id"] # A帽adimos 'id' como primer elemento
|
| 47 |
+
for tarea in tareas_seleccionadas:
|
| 48 |
+
columnas.append(f"{tarea.lower().replace(' ', '_')}_label")
|
| 49 |
+
return ",".join(columnas)
|
| 50 |
+
|
| 51 |
+
# Funci贸n para buscar datasets compatibles en HuggingFace
|
| 52 |
+
def buscar_datasets(tareas_seleccionadas, filtro_tama帽o=None, filtro_licencia=None):
|
| 53 |
+
query = "+".join(tareas_seleccionadas)
|
| 54 |
+
url = f"https://huggingface.co/api/datasets?search={query}"
|
| 55 |
+
response = requests.get(url)
|
| 56 |
+
datasets = response.json()
|
| 57 |
+
|
| 58 |
+
resultados = []
|
| 59 |
+
for dataset in datasets:
|
| 60 |
+
# Aplicar filtros adicionales
|
| 61 |
+
if filtro_tama帽o and dataset.get("size_categories") != filtro_tama帽o:
|
| 62 |
+
continue
|
| 63 |
+
if filtro_licencia and dataset.get("license") != filtro_licencia:
|
| 64 |
+
continue
|
| 65 |
+
resultados.append(f"- {dataset['id']}: {dataset['description']}")
|
| 66 |
+
return "\n".join(resultados)
|
| 67 |
+
|
| 68 |
+
# Funci贸n para generar el dataset
|
| 69 |
+
def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_por_pagina=5):
|
| 70 |
+
if not datasets_seleccionados:
|
| 71 |
+
raise ValueError("Debes seleccionar al menos un dataset.")
|
| 72 |
+
|
| 73 |
+
columnas = encabezado.split(",")
|
| 74 |
+
filas = []
|
| 75 |
+
|
| 76 |
+
# Cargar datos reales desde los datasets seleccionados
|
| 77 |
+
for dataset_id in datasets_seleccionados.split("\n"):
|
| 78 |
+
dataset_id = dataset_id.strip("- ").split(":")[0] # Extraer ID del dataset
|
| 79 |
+
try:
|
| 80 |
+
dataset = load_dataset(dataset_id, split="train")
|
| 81 |
+
inicio = (pagina_actual - 1) * filas_por_pagina
|
| 82 |
+
fin = pagina_actual * filas_por_pagina
|
| 83 |
+
for i, fila in enumerate(dataset[inicio:fin]):
|
| 84 |
+
valores = [str(fila.get(col, "valor_default")) for col in columnas[1:]] # Ignorar 'id'
|
| 85 |
+
filas.append(f"id_{inicio + i}," + ",".join(valores))
|
| 86 |
+
except Exception as e:
|
| 87 |
+
filas.append(f"Error cargando dataset {dataset_id}: {str(e)}")
|
| 88 |
+
|
| 89 |
+
contenido_csv = "\n".join([encabezado] + filas)
|
| 90 |
+
return contenido_csv
|