Update functions.py
Browse files- functions.py +71 -58
functions.py
CHANGED
|
@@ -91,67 +91,80 @@ def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_p
|
|
| 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 |
# Funci贸n inteligente para generar el encabezado usando IA de HuggingFace
|
| 96 |
def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
"
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
API_URL,
|
| 127 |
-
headers=headers,
|
| 128 |
-
json={
|
| 129 |
-
"inputs": prompt,
|
| 130 |
-
"parameters": {"max_length": 512, "temperature": 0.1} # Ajustar par谩metros para precisi贸n <button class="citation-flag" data-index="2">
|
| 131 |
}
|
| 132 |
-
)
|
| 133 |
-
response.raise_for_status()
|
| 134 |
-
|
| 135 |
-
# Procesar respuesta
|
| 136 |
-
if response.status_code == 200:
|
| 137 |
-
encabezado = response.json()[0]["generated_text"].strip()
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
| 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):
|
| 96 |
+
max_reintentos = 3 # N煤mero m谩ximo de reintentos <button class="citation-flag" data-index="1">
|
| 97 |
+
espera_entre_reintentos = 2 # Segundos entre reintentos
|
| 98 |
+
|
| 99 |
+
for intento in range(max_reintentos + 1):
|
| 100 |
+
try:
|
| 101 |
+
if not tareas_seleccionadas:
|
| 102 |
+
raise ValueError("Debes seleccionar al menos una tarea.")
|
| 103 |
+
|
| 104 |
+
if not api_key:
|
| 105 |
+
raise ValueError("API Key no proporcionada. Ingresa una clave v谩lida.")
|
| 106 |
+
|
| 107 |
+
# Prompt optimizado
|
| 108 |
+
prompt = f"""
|
| 109 |
+
Eres un experto en datasets. Genera un encabezado CSV para las tareas: {', '.join(tareas_seleccionadas)}.
|
| 110 |
+
Ejemplo de salida para 'Clasificaci贸n de texto' y 'Detecci贸n de objetos':
|
| 111 |
+
id,clasificaci贸n_de_texto_label,detecci贸n_de_objetos_label
|
| 112 |
+
|
| 113 |
+
Reglas:
|
| 114 |
+
- Solo responde con el encabezado, sin texto adicional.
|
| 115 |
+
- El primer campo debe ser 'id'.
|
| 116 |
+
- Las columnas deben seguir el formato 'tarea_label' en min煤sculas y sin espacios.
|
| 117 |
+
- Separa las columnas con comas.
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
# Usar un modelo ligero y estable (t5-small) <button class="citation-flag" data-index="2">
|
| 121 |
+
API_URL = "https://api-inference.huggingface.co/models/t5-small"
|
| 122 |
+
headers = {
|
| 123 |
+
"Authorization": f"Bearer {api_key}",
|
| 124 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Enviar solicitud a la API
|
| 128 |
+
response = requests.post(
|
| 129 |
+
API_URL,
|
| 130 |
+
headers=headers,
|
| 131 |
+
json={
|
| 132 |
+
"inputs": prompt,
|
| 133 |
+
"parameters": {"max_length": 128, "temperature": 0.1} # Par谩metros ajustados <button class="citation-flag" data-index="2">
|
| 134 |
+
}
|
| 135 |
+
)
|
| 136 |
+
response.raise_for_status()
|
| 137 |
|
| 138 |
+
# Procesar respuesta
|
| 139 |
+
if response.status_code == 200:
|
| 140 |
+
encabezado = response.json()[0]["generated_text"].strip()
|
| 141 |
+
|
| 142 |
+
# Validar formato
|
| 143 |
+
if not encabezado.startswith("id,"):
|
| 144 |
+
raise ValueError("Formato inv谩lido. Respuesta: " + encabezado)
|
| 145 |
+
|
| 146 |
+
return encabezado
|
| 147 |
+
else:
|
| 148 |
+
raise ValueError(f"Error en la API: {response.text}")
|
| 149 |
+
|
| 150 |
+
except requests.exceptions.HTTPError as e:
|
| 151 |
+
if e.response.status_code == 503 and intento < max_reintentos:
|
| 152 |
+
time.sleep(espera_entre_reintentos) # Esperar antes de reintentar <button class="citation-flag" data-index="1">
|
| 153 |
+
continue
|
| 154 |
+
elif e.response.status_code == 401:
|
| 155 |
+
raise ValueError("API Key inv谩lida o sin permisos.")
|
| 156 |
+
else:
|
| 157 |
+
raise ValueError(f"Error HTTP: {str(e)}")
|
| 158 |
+
|
| 159 |
+
except Exception as e:
|
| 160 |
+
raise ValueError(f"Error inesperado: {str(e)}")
|
| 161 |
|
| 162 |
+
# Si fallan todos los reintentos, usar un fallback local <button class="citation-flag" data-index="3">
|
| 163 |
+
return generar_encabezado_local(tareas_seleccionadas)
|
| 164 |
+
|
| 165 |
+
# Fallback local en caso de fallo de la API
|
| 166 |
+
def generar_encabezado_local(tareas_seleccionadas):
|
| 167 |
+
columnas = ["id"]
|
| 168 |
+
for tarea in tareas_seleccionadas:
|
| 169 |
+
columnas.append(f"{tarea.lower().replace(' ', '_')}_label")
|
| 170 |
+
return ",".join(columnas)
|