Update functions.py
Browse files- functions.py +19 -12
functions.py
CHANGED
|
@@ -101,27 +101,32 @@ def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
| 101 |
if not api_key:
|
| 102 |
raise ValueError("API Key no proporcionada. Ingresa una clave válida.")
|
| 103 |
|
| 104 |
-
# Prompt mejorado
|
| 105 |
prompt = f"""
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
Genera un encabezado de CSV con las siguientes reglas:
|
| 109 |
1. El primer campo debe ser 'id'.
|
| 110 |
-
2.
|
| 111 |
-
3.
|
| 112 |
-
Ejemplo
|
| 113 |
-
id,clasificación_de_texto_label,detección_de_objetos_label
|
| 114 |
"""
|
| 115 |
|
| 116 |
-
# Usar un modelo
|
| 117 |
-
API_URL = "https://api-inference.huggingface.co/models/
|
| 118 |
headers = {
|
| 119 |
"Authorization": f"Bearer {api_key}",
|
| 120 |
"Content-Type": "application/json"
|
| 121 |
}
|
| 122 |
|
| 123 |
# Enviar solicitud a la API
|
| 124 |
-
response = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
response.raise_for_status()
|
| 126 |
|
| 127 |
# Procesar respuesta
|
|
@@ -130,7 +135,7 @@ def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
| 130 |
|
| 131 |
# Validar formato básico
|
| 132 |
if not encabezado.startswith("id,"):
|
| 133 |
-
raise ValueError("
|
| 134 |
|
| 135 |
return encabezado
|
| 136 |
else:
|
|
@@ -141,6 +146,8 @@ def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
| 141 |
raise ValueError("API Key inválida o sin permisos.")
|
| 142 |
elif e.response.status_code == 403:
|
| 143 |
raise ValueError("Acceso denegado al modelo. Prueba con otro modelo público.")
|
|
|
|
|
|
|
| 144 |
else:
|
| 145 |
raise ValueError(f"Error HTTP: {str(e)}")
|
| 146 |
except KeyError:
|
|
|
|
| 101 |
if not api_key:
|
| 102 |
raise ValueError("API Key no proporcionada. Ingresa una clave válida.")
|
| 103 |
|
| 104 |
+
# Prompt mejorado
|
| 105 |
prompt = f"""
|
| 106 |
+
Genera un encabezado de CSV para las siguientes tareas: {', '.join(tareas_seleccionadas)}.
|
| 107 |
+
Reglas:
|
|
|
|
| 108 |
1. El primer campo debe ser 'id'.
|
| 109 |
+
2. Las columnas deben seguir el formato 'tarea_label' (ej. 'clasificación_de_texto_label').
|
| 110 |
+
3. No incluir espacios ni texto adicional.
|
| 111 |
+
Ejemplo: id,traducción_label,clasificación_de_imágenes_label
|
|
|
|
| 112 |
"""
|
| 113 |
|
| 114 |
+
# Usar un modelo de generación de texto compatible (ej. GPT-2) <button class="citation-flag" data-index="2">
|
| 115 |
+
API_URL = "https://api-inference.huggingface.co/models/gpt2"
|
| 116 |
headers = {
|
| 117 |
"Authorization": f"Bearer {api_key}",
|
| 118 |
"Content-Type": "application/json"
|
| 119 |
}
|
| 120 |
|
| 121 |
# Enviar solicitud a la API
|
| 122 |
+
response = requests.post(
|
| 123 |
+
API_URL,
|
| 124 |
+
headers=headers,
|
| 125 |
+
json={
|
| 126 |
+
"inputs": prompt,
|
| 127 |
+
"parameters": {"max_length": 256, "temperature": 0.5} # Ajustar parámetros <button class="citation-flag" data-index="2">
|
| 128 |
+
}
|
| 129 |
+
)
|
| 130 |
response.raise_for_status()
|
| 131 |
|
| 132 |
# Procesar respuesta
|
|
|
|
| 135 |
|
| 136 |
# Validar formato básico
|
| 137 |
if not encabezado.startswith("id,"):
|
| 138 |
+
raise ValueError(f"Formato inválido. Respuesta del modelo: {encabezado}")
|
| 139 |
|
| 140 |
return encabezado
|
| 141 |
else:
|
|
|
|
| 146 |
raise ValueError("API Key inválida o sin permisos.")
|
| 147 |
elif e.response.status_code == 403:
|
| 148 |
raise ValueError("Acceso denegado al modelo. Prueba con otro modelo público.")
|
| 149 |
+
elif e.response.status_code == 503:
|
| 150 |
+
raise ValueError("El modelo está temporalmente no disponible. Intenta más tarde o prueba con otro modelo.") <button class="citation-flag" data-index="1">
|
| 151 |
else:
|
| 152 |
raise ValueError(f"Error HTTP: {str(e)}")
|
| 153 |
except KeyError:
|