Improve training code 2
This commit is contained in:
@@ -622,7 +622,7 @@ fi
|
||||
echo ""
|
||||
|
||||
# =================================================================
|
||||
# STEP 4: MODEL TRAINING (ONBOARDING PAGE STEP 4)
|
||||
# STEP 4: MODEL TRAINING (ONBOARDING PAGE STEP 4) - FIXED
|
||||
# =================================================================
|
||||
|
||||
echo -e "${STEP_ICONS[3]} ${PURPLE}STEP 4: AI MODEL TRAINING${NC}"
|
||||
@@ -633,22 +633,28 @@ log_step "4.1. Starting model training process with real data products"
|
||||
|
||||
# Get unique products from the imported data for training
|
||||
# Extract some real product names from the CSV for training
|
||||
REAL_PRODUCTS=$(tail -n +2 "$PREPARED_CSV" | cut -d',' -f2 | sort | uniq | head -3 | tr '\n' ',' | sed 's/,$//')
|
||||
REAL_PRODUCTS_RAW=$(tail -n +2 "$PREPARED_CSV" | cut -d',' -f2 | sort | uniq | head -3 | tr '\n' ',' | sed 's/,$//')
|
||||
|
||||
if [ -z "$REAL_PRODUCTS" ]; then
|
||||
if [ -z "$REAL_PRODUCTS_RAW" ]; then
|
||||
# Fallback to default products if extraction fails
|
||||
REAL_PRODUCTS='"Pan de molde","Croissants","Magdalenas"'
|
||||
REAL_PRODUCTS_ARRAY='["Pan de molde","Croissants","Magdalenas"]'
|
||||
log_warning "Could not extract real product names, using defaults"
|
||||
else
|
||||
# Format for JSON array
|
||||
REAL_PRODUCTS=$(echo "$REAL_PRODUCTS" | sed 's/,/","/g' | sed 's/^/"/' | sed 's/$/"/')
|
||||
log_success "Extracted real products for training: $REAL_PRODUCTS"
|
||||
# Format for JSON array properly
|
||||
REAL_PRODUCTS_ARRAY='['$(echo "$REAL_PRODUCTS_RAW" | sed 's/,/","/g' | sed 's/^/"/' | sed 's/$/"/')']'
|
||||
log_success "Extracted real products for training: $REAL_PRODUCTS_ARRAY"
|
||||
fi
|
||||
|
||||
# Training request with real products
|
||||
# ✅ FIXED: Training request with correct data types matching TrainingJobRequest schema
|
||||
TRAINING_DATA="{
|
||||
\"tenant_id\": \"$TENANT_ID\"
|
||||
}
|
||||
\"products\": $REAL_PRODUCTS_ARRAY,
|
||||
\"max_workers\": 4,
|
||||
\"seasonality_mode\": \"additive\",
|
||||
\"daily_seasonality\": true,
|
||||
\"weekly_seasonality\": true,
|
||||
\"yearly_seasonality\": true,
|
||||
\"force_retrain\": false,
|
||||
\"parallel_training\": true
|
||||
}"
|
||||
|
||||
echo "Training Request:"
|
||||
@@ -668,15 +674,54 @@ echo "Training HTTP Status Code: $HTTP_CODE"
|
||||
echo "Training Response:"
|
||||
echo "$TRAINING_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$TRAINING_RESPONSE"
|
||||
|
||||
TRAINING_TASK_ID=$(extract_json_field "$TRAINING_RESPONSE" "job_id")
|
||||
if [ -z "$TRAINING_TASK_ID" ]; then
|
||||
TRAINING_TASK_ID=$(extract_json_field "$TRAINING_RESPONSE" "id")
|
||||
fi
|
||||
|
||||
if [ -n "$TRAINING_TASK_ID" ]; then
|
||||
log_success "Training started successfully - Task ID: $TRAINING_TASK_ID"
|
||||
# ✅ FIXED: Better error handling for 422 responses
|
||||
if [ "$HTTP_CODE" = "422" ]; then
|
||||
log_error "Training request failed with validation error (HTTP 422)"
|
||||
echo "This usually means the request doesn't match the expected schema."
|
||||
echo "Common causes:"
|
||||
echo " - Wrong data types (string instead of integer)"
|
||||
echo " - Invalid field values (seasonality_mode must be 'additive' or 'multiplicative')"
|
||||
echo " - Missing required headers"
|
||||
echo ""
|
||||
echo "Response details:"
|
||||
echo "$TRAINING_RESPONSE"
|
||||
|
||||
# Try a minimal request that should work
|
||||
log_step "4.2. Attempting minimal training request as fallback"
|
||||
|
||||
MINIMAL_TRAINING_DATA='{"seasonality_mode": "additive"}'
|
||||
|
||||
FALLBACK_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "$API_BASE/api/v1/tenants/$TENANT_ID/training/jobs" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $ACCESS_TOKEN" \
|
||||
-H "X-Tenant-ID: $TENANT_ID" \
|
||||
-d "$MINIMAL_TRAINING_DATA")
|
||||
|
||||
FALLBACK_HTTP_CODE=$(echo "$FALLBACK_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
|
||||
FALLBACK_RESPONSE=$(echo "$FALLBACK_RESPONSE" | sed '/HTTP_CODE:/d')
|
||||
|
||||
echo "Fallback HTTP Status Code: $FALLBACK_HTTP_CODE"
|
||||
echo "Fallback Response:"
|
||||
echo "$FALLBACK_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$FALLBACK_RESPONSE"
|
||||
|
||||
if [ "$FALLBACK_HTTP_CODE" = "200" ] || [ "$FALLBACK_HTTP_CODE" = "201" ]; then
|
||||
log_success "Minimal training request succeeded"
|
||||
TRAINING_TASK_ID=$(extract_json_field "$FALLBACK_RESPONSE" "job_id")
|
||||
else
|
||||
log_error "Both training requests failed"
|
||||
fi
|
||||
else
|
||||
log_warning "Could not start training - task ID not found"
|
||||
# Original success handling
|
||||
TRAINING_TASK_ID=$(extract_json_field "$TRAINING_RESPONSE" "job_id")
|
||||
if [ -z "$TRAINING_TASK_ID" ]; then
|
||||
TRAINING_TASK_ID=$(extract_json_field "$TRAINING_RESPONSE" "id")
|
||||
fi
|
||||
|
||||
if [ -n "$TRAINING_TASK_ID" ]; then
|
||||
log_success "Training started successfully - Task ID: $TRAINING_TASK_ID"
|
||||
else
|
||||
log_warning "Could not start training - task ID not found"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
Reference in New Issue
Block a user