'Genre and topic are required.']); exit; } // Prepare the prompt for the AI $prompt = "You are a creative assistant for social media influencers. Generate a short video script (around 150-200 words) for the genre: '{$genre}'. The script should be about: '{$topic}'. The script should be engaging, concise, and suitable for platforms like TikTok, Instagram Reels, or YouTube Shorts. Include suggestions for visuals or on-screen text where appropriate. Format the output as plain text."; try { $resp = LocalAIApi::createResponse( [ 'input' => [ ['role' => 'system', 'content' => 'You are a helpful scriptwriting assistant for social media creators.'], ['role' => 'user', 'content' => $prompt], ], ], [ 'poll_interval' => 5, 'poll_timeout' => 300 ] ); if (!empty($resp['success'])) { $text = LocalAIApi::extractText($resp); if ($text === '') { throw new Exception('AI returned an empty response.'); } echo json_encode(['script' => nl2br(htmlspecialchars($text))]); } else { throw new Exception($resp['error'] ?? 'Unknown AI API error.'); } } catch (Exception $e) { error_log('AI script generation error: ' . $e->getMessage()); http_response_code(500); echo json_encode(['error' => 'Sorry, we couldn\'t generate a script at this time. Please try again later.']); }