Update functions.py
Browse files- functions.py +23 -27
functions.py
CHANGED
|
@@ -92,11 +92,25 @@ def generar_dataset(encabezado, datasets_seleccionados, pagina_actual=1, filas_p
|
|
| 92 |
except Exception as e:
|
| 93 |
raise ValueError(f"Error al generar el dataset: {str(e)}")
|
| 94 |
|
| 95 |
-
# Funci贸n inteligente para generar el encabezado usando IA de HuggingFace
|
| 96 |
def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
| 97 |
max_reintentos = 3
|
| 98 |
espera_entre_reintentos = 2
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
for intento in range(max_reintentos + 1):
|
| 101 |
try:
|
| 102 |
if not tareas_seleccionadas:
|
|
@@ -105,33 +119,17 @@ def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
| 105 |
if not api_key:
|
| 106 |
raise ValueError("API Key no proporcionada. Ingresa una clave v谩lida.")
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
prompt = f"""
|
| 110 |
-
Eres un experto en datasets. Genera un encabezado CSV para las tareas: {', '.join(tareas_seleccionadas)}.
|
| 111 |
-
Ejemplo de salida para 'Clasificaci贸n de texto' y 'Detecci贸n de objetos':
|
| 112 |
-
id,clasificaci贸n_de_texto_label,detecci贸n_de_objetos_label
|
| 113 |
-
|
| 114 |
-
Reglas:
|
| 115 |
-
- Solo responde con el encabezado, sin texto adicional.
|
| 116 |
-
- El primer campo debe ser 'id'.
|
| 117 |
-
- Las columnas deben seguir el formato 'tarea_label' en min煤sculas y sin espacios.
|
| 118 |
-
- Separa las columnas con comas.
|
| 119 |
-
"""
|
| 120 |
-
|
| 121 |
-
# Usar un modelo alternativo (distilgpt2) <button class="citation-flag" data-index="2">
|
| 122 |
-
API_URL = "https://api-inference.huggingface.co/models/distilgpt2"
|
| 123 |
-
headers = {
|
| 124 |
-
"Authorization": f"Bearer {api_key}",
|
| 125 |
-
"Content-Type": "application/json"
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
# Enviar solicitud a la API
|
| 129 |
response = requests.post(
|
| 130 |
API_URL,
|
| 131 |
headers=headers,
|
| 132 |
json={
|
| 133 |
"inputs": prompt,
|
| 134 |
-
"parameters": {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
)
|
| 137 |
response.raise_for_status()
|
|
@@ -140,14 +138,12 @@ def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
|
| 140 |
if response.status_code == 200:
|
| 141 |
encabezado = response.json()[0]["generated_text"].strip()
|
| 142 |
|
| 143 |
-
# Validar formato
|
| 144 |
if not encabezado.startswith("id,"):
|
| 145 |
raise ValueError("Formato inv谩lido. Respuesta: " + encabezado)
|
| 146 |
|
| 147 |
return encabezado
|
| 148 |
-
|
| 149 |
-
raise ValueError(f"Error en la API: {response.text}")
|
| 150 |
-
|
| 151 |
except requests.exceptions.HTTPError as e:
|
| 152 |
if e.response.status_code == 503 and intento < max_reintentos:
|
| 153 |
time.sleep(espera_entre_reintentos)
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
raise ValueError(f"Error al generar el dataset: {str(e)}")
|
| 94 |
|
|
|
|
| 95 |
def generar_encabezado_inteligente(tareas_seleccionadas, api_key):
|
| 96 |
max_reintentos = 3
|
| 97 |
espera_entre_reintentos = 2
|
| 98 |
|
| 99 |
+
# Prompt mejorado con instrucciones estrictas <button class="citation-flag" data-index="3">
|
| 100 |
+
prompt = f"""
|
| 101 |
+
Genera SOLO el encabezado CSV para las tareas: {', '.join(tareas_seleccionadas)}.
|
| 102 |
+
Formato: id,tarea1_label,tarea2_label,...
|
| 103 |
+
Ejemplo para 'Clasificaci贸n de texto' y 'Segmentaci贸n':
|
| 104 |
+
id,clasificaci贸n_de_texto_label,segmentaci贸n_label
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
# Usar modelo especializado en generaci贸n de texto estructurado <button class="citation-flag" data-index="2">
|
| 108 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-code-3b"
|
| 109 |
+
headers = {
|
| 110 |
+
"Authorization": f"Bearer {api_key}",
|
| 111 |
+
"Content-Type": "application/json"
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
for intento in range(max_reintentos + 1):
|
| 115 |
try:
|
| 116 |
if not tareas_seleccionadas:
|
|
|
|
| 119 |
if not api_key:
|
| 120 |
raise ValueError("API Key no proporcionada. Ingresa una clave v谩lida.")
|
| 121 |
|
| 122 |
+
# Enviar solicitud con par谩metros ajustados <button class="citation-flag" data-index="2">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
response = requests.post(
|
| 124 |
API_URL,
|
| 125 |
headers=headers,
|
| 126 |
json={
|
| 127 |
"inputs": prompt,
|
| 128 |
+
"parameters": {
|
| 129 |
+
"max_length": 64,
|
| 130 |
+
"temperature": 0.1,
|
| 131 |
+
"stop": ["\n\n"] # Detener la generaci贸n al finalizar el encabezado <button class="citation-flag" data-index="3">
|
| 132 |
+
}
|
| 133 |
}
|
| 134 |
)
|
| 135 |
response.raise_for_status()
|
|
|
|
| 138 |
if response.status_code == 200:
|
| 139 |
encabezado = response.json()[0]["generated_text"].strip()
|
| 140 |
|
| 141 |
+
# Validar formato estricto
|
| 142 |
if not encabezado.startswith("id,"):
|
| 143 |
raise ValueError("Formato inv谩lido. Respuesta: " + encabezado)
|
| 144 |
|
| 145 |
return encabezado
|
| 146 |
+
|
|
|
|
|
|
|
| 147 |
except requests.exceptions.HTTPError as e:
|
| 148 |
if e.response.status_code == 503 and intento < max_reintentos:
|
| 149 |
time.sleep(espera_entre_reintentos)
|