Update functions.py
Browse files- functions.py +17 -26
functions.py
CHANGED
|
@@ -4,18 +4,18 @@ import requests
|
|
| 4 |
from datasets import load_dataset
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
|
| 7 |
-
# Funci贸n para cargar modalidades y tareas
|
| 8 |
def cargar_modalidades_tareas():
|
| 9 |
try:
|
| 10 |
with open("modalidades_tareas.json", "r") as file:
|
| 11 |
MODALIDAD_TAREAS = json.load(file)
|
| 12 |
return MODALIDAD_TAREAS
|
| 13 |
except FileNotFoundError:
|
| 14 |
-
raise ValueError("El archivo 'modalidades_tareas.json' no se encontr贸.
|
| 15 |
except json.JSONDecodeError:
|
| 16 |
-
raise ValueError("El archivo 'modalidades_tareas.json' tiene un formato incorrecto.
|
| 17 |
|
| 18 |
-
# Validar
|
| 19 |
def validar_modalidades_tareas(modalidades_tareas):
|
| 20 |
try:
|
| 21 |
for modalidad, tareas in modalidades_tareas.items():
|
|
@@ -27,41 +27,34 @@ def validar_modalidades_tareas(modalidades_tareas):
|
|
| 27 |
except AttributeError:
|
| 28 |
raise ValueError("El objeto proporcionado no es un diccionario v谩lido.")
|
| 29 |
|
| 30 |
-
#
|
| 31 |
def generar_grafica_barras(tareas_seleccionadas, MODALIDAD_TAREAS):
|
| 32 |
try:
|
| 33 |
-
# Contar la cantidad de tareas seleccionadas por modalidad
|
| 34 |
conteo_modalidades = {}
|
| 35 |
for modalidad, tareas in MODALIDAD_TAREAS.items():
|
| 36 |
-
conteo_modalidades[modalidad] = len([
|
| 37 |
|
| 38 |
-
modalidades = list(conteo_modalidades.keys())
|
| 39 |
-
cantidades = [conteo_modalidades[modalidad] for modalidad in modalidades]
|
| 40 |
-
|
| 41 |
-
# Crear la gr谩fica de barras horizontal
|
| 42 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 43 |
-
ax.barh(
|
| 44 |
ax.set_xlabel('Cantidad de Tareas Seleccionadas')
|
| 45 |
ax.set_ylabel('Modalidades')
|
| 46 |
-
ax.set_title('Distribuci贸n de Tareas
|
| 47 |
-
ax.invert_yaxis()
|
| 48 |
-
|
| 49 |
return fig
|
| 50 |
except Exception as e:
|
| 51 |
-
raise ValueError(f"Error al generar la gr谩fica
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
def buscar_datasets(tareas_seleccionadas, filtro_tama帽o
|
| 55 |
try:
|
| 56 |
query = "+".join(tareas_seleccionadas)
|
| 57 |
url = f"https://huggingface.co/api/datasets?search={query}"
|
| 58 |
response = requests.get(url)
|
| 59 |
-
response.raise_for_status()
|
| 60 |
datasets = response.json()
|
| 61 |
|
| 62 |
resultados = []
|
| 63 |
for dataset in datasets:
|
| 64 |
-
# Aplicar filtros adicionales
|
| 65 |
if filtro_tama帽o and dataset.get("size_categories") != filtro_tama帽o:
|
| 66 |
continue
|
| 67 |
if filtro_licencia and dataset.get("license") != filtro_licencia:
|
|
@@ -69,9 +62,7 @@ def buscar_datasets(tareas_seleccionadas, filtro_tama帽o=None, filtro_licencia=N
|
|
| 69 |
resultados.append(f"- {dataset['id']}: {dataset['description']}")
|
| 70 |
return "\n".join(resultados)
|
| 71 |
except requests.exceptions.RequestException as e:
|
| 72 |
-
raise ValueError(f"Error al buscar datasets: {str(e)}") <button class="citation-flag" data-index="8">
|
| 73 |
-
except Exception as e:
|
| 74 |
-
raise ValueError(f"Error inesperado al buscar datasets: {str(e)}")
|
| 75 |
|
| 76 |
# Funci贸n para generar el dataset
|
| 77 |
def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_por_pagina=5):
|
|
@@ -98,7 +89,7 @@ def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_p
|
|
| 98 |
contenido_csv = "\n".join([encabezado] + filas)
|
| 99 |
return contenido_csv
|
| 100 |
except Exception as e:
|
| 101 |
-
raise ValueError(f"Error al generar el dataset: {str(e)}")
|
| 102 |
|
| 103 |
# Funci贸n inteligente para generar el encabezado usando IA de HuggingFace
|
| 104 |
def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
@@ -130,8 +121,8 @@ def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
| 130 |
else:
|
| 131 |
raise ValueError(f"Error al generar el encabezado: {response.text}")
|
| 132 |
except requests.exceptions.RequestException as e:
|
| 133 |
-
raise ValueError(f"Error en la solicitud a la API de HuggingFace: {str(e)}")
|
| 134 |
except KeyError:
|
| 135 |
-
raise ValueError("La respuesta de la API no contiene los datos esperados.")
|
| 136 |
except Exception as e:
|
| 137 |
raise ValueError(f"Error inesperado al generar el encabezado: {str(e)}")
|
|
|
|
| 4 |
from datasets import load_dataset
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
|
| 7 |
+
# Funci贸n para cargar modalidades y tareas
|
| 8 |
def cargar_modalidades_tareas():
|
| 9 |
try:
|
| 10 |
with open("modalidades_tareas.json", "r") as file:
|
| 11 |
MODALIDAD_TAREAS = json.load(file)
|
| 12 |
return MODALIDAD_TAREAS
|
| 13 |
except FileNotFoundError:
|
| 14 |
+
raise ValueError("El archivo 'modalidades_tareas.json' no se encontr贸.")
|
| 15 |
except json.JSONDecodeError:
|
| 16 |
+
raise ValueError("El archivo 'modalidades_tareas.json' tiene un formato incorrecto.")
|
| 17 |
|
| 18 |
+
# Validar modalidades y tareas
|
| 19 |
def validar_modalidades_tareas(modalidades_tareas):
|
| 20 |
try:
|
| 21 |
for modalidad, tareas in modalidades_tareas.items():
|
|
|
|
| 27 |
except AttributeError:
|
| 28 |
raise ValueError("El objeto proporcionado no es un diccionario v谩lido.")
|
| 29 |
|
| 30 |
+
# Generar gr谩fica de barras
|
| 31 |
def generar_grafica_barras(tareas_seleccionadas, MODALIDAD_TAREAS):
|
| 32 |
try:
|
|
|
|
| 33 |
conteo_modalidades = {}
|
| 34 |
for modalidad, tareas in MODALIDAD_TAREAS.items():
|
| 35 |
+
conteo_modalidades[modalidad] = len([t for t in tareas if t in tareas_seleccionadas])
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 38 |
+
ax.barh(list(conteo_modalidades.keys()), list(conteo_modalidades.values()), color='skyblue')
|
| 39 |
ax.set_xlabel('Cantidad de Tareas Seleccionadas')
|
| 40 |
ax.set_ylabel('Modalidades')
|
| 41 |
+
ax.set_title('Distribuci贸n de Tareas por Modalidad')
|
| 42 |
+
ax.invert_yaxis()
|
|
|
|
| 43 |
return fig
|
| 44 |
except Exception as e:
|
| 45 |
+
raise ValueError(f"Error al generar la gr谩fica: {str(e)}")
|
| 46 |
|
| 47 |
+
# Buscar datasets en HuggingFace
|
| 48 |
+
def buscar_datasets(tareas_seleccionadas, filtro_tama帽o, filtro_licencia):
|
| 49 |
try:
|
| 50 |
query = "+".join(tareas_seleccionadas)
|
| 51 |
url = f"https://huggingface.co/api/datasets?search={query}"
|
| 52 |
response = requests.get(url)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
datasets = response.json()
|
| 55 |
|
| 56 |
resultados = []
|
| 57 |
for dataset in datasets:
|
|
|
|
| 58 |
if filtro_tama帽o and dataset.get("size_categories") != filtro_tama帽o:
|
| 59 |
continue
|
| 60 |
if filtro_licencia and dataset.get("license") != filtro_licencia:
|
|
|
|
| 62 |
resultados.append(f"- {dataset['id']}: {dataset['description']}")
|
| 63 |
return "\n".join(resultados)
|
| 64 |
except requests.exceptions.RequestException as e:
|
| 65 |
+
raise ValueError(f"Error al buscar datasets: {str(e)}") # Eliminado el HTML <button class="citation-flag" data-index="8">
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Funci贸n para generar el dataset
|
| 68 |
def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_por_pagina=5):
|
|
|
|
| 89 |
contenido_csv = "\n".join([encabezado] + filas)
|
| 90 |
return contenido_csv
|
| 91 |
except Exception as e:
|
| 92 |
+
raise ValueError(f"Error al generar el dataset: {str(e)}")
|
| 93 |
|
| 94 |
# Funci贸n inteligente para generar el encabezado usando IA de HuggingFace
|
| 95 |
def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
|
|
| 121 |
else:
|
| 122 |
raise ValueError(f"Error al generar el encabezado: {response.text}")
|
| 123 |
except requests.exceptions.RequestException as e:
|
| 124 |
+
raise ValueError(f"Error en la solicitud a la API de HuggingFace: {str(e)}")
|
| 125 |
except KeyError:
|
| 126 |
+
raise ValueError("La respuesta de la API no contiene los datos esperados.")
|
| 127 |
except Exception as e:
|
| 128 |
raise ValueError(f"Error inesperado al generar el encabezado: {str(e)}")
|