fine_tune_text( $text ); // Check processing time. $processing_time = time() - $start_time; accessibility_audio_tts_debug_log( 'ChatGPT fine-tuning completed in ' . $processing_time . ' seconds' ); if ( $processing_time > $max_fine_tune_time ) { accessibility_audio_tts_debug_log( 'WARNING: ChatGPT fine-tuning took ' . $processing_time . ' seconds (> ' . $max_fine_tune_time . 's limit)' ); } // Handle new array format from fine_tune_text. if ( is_array( $fine_tune_result ) && isset( $fine_tune_result['success'] ) ) { if ( $fine_tune_result['success'] && $fine_tune_result['optimized'] ) { $result['text'] = $fine_tune_result['text']; $result['optimized'] = true; // Store token usage data. if ( isset( $fine_tune_result['tokens'] ) ) { $result['chatgpt_tokens'] = $fine_tune_result['tokens']; $tokens = $fine_tune_result['tokens']; $result['chatgpt_cost'] = ( new \Accessibility_Audio_TTS\Admin\Cost_Calculator() )->calculate_openai_cost( $tokens['input'], $tokens['output'], $tokens['model'] ?? get_option( 'accessibility_audio_tts_chatgpt_model', 'gpt-5' ), $tokens['cached_input'] ?? 0 ); accessibility_audio_tts_debug_log( 'ChatGPT tokens used - Input: ' . $fine_tune_result['tokens']['input'] . ', Output: ' . $fine_tune_result['tokens']['output'] . ', Cost: $' . $result['chatgpt_cost'] ); } $length_diff = strlen( $result['text'] ) - strlen( $text ); accessibility_audio_tts_debug_log( 'Text fine-tuning applied successfully. Original length: ' . strlen( $text ) . ', Fine-tuned length: ' . strlen( $result['text'] ) . ' (diff: ' . $length_diff . ')' ); // Sanity check - prevent extremely long output. if ( strlen( $result['text'] ) > strlen( $text ) * 2 ) { accessibility_audio_tts_debug_log( 'WARNING: Fine-tuned text is more than 2x original length, using original text for safety' ); $result['text'] = $text; $result['optimized'] = false; } } else { accessibility_audio_tts_debug_log( 'Text fine-tuning returned unchanged text' ); } } return $result; } catch ( \Exception $e ) { $processing_time = time() - $start_time; // Check for specific ChatGPT errors that should stop processing. if ( $e->getMessage() === 'CHATGPT_QUOTA_EXCEEDED' ) { accessibility_audio_tts_debug_log( 'CRITICAL ERROR: ChatGPT quota exceeded after ' . $processing_time . 's', true ); throw new \Exception( esc_html( __( 'ChatGPT API quota exceeded. Please check your OpenAI billing details.', 'accessibility-audio-tts-text-to-speech-for-articles' ) ) ); } if ( $e->getMessage() === 'CHATGPT_INVALID_API_KEY' ) { accessibility_audio_tts_debug_log( 'CRITICAL ERROR: Invalid ChatGPT API key after ' . $processing_time . 's', true ); throw new \Exception( esc_html( __( 'Invalid ChatGPT API key. Please check your API key in settings.', 'accessibility-audio-tts-text-to-speech-for-articles' ) ) ); } // For other errors, log and continue with original text. accessibility_audio_tts_debug_log( 'ERROR in ChatGPT fine-tuning after ' . $processing_time . 's: ' . $e->getMessage() ); accessibility_audio_tts_debug_log( 'Falling back to original text due to ChatGPT error' ); return $result; } } }