[ [ 'parts' => [ ['fileData' => ['mimeType' => 'application/pdf', 'fileUri' => $fileUri]], ['text' => $prompt] ] ] ], 'generationConfig' => ['responseMimeType' => 'application/json', 'temperature' => 0.1] ]; // Using gemini-1.5-pro since it is powerful for large PDFs and math formulas $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent?key=" . $geminiKey; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 300 // AI might take 2 minutes ]); $res = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code !== 200 || empty($res)) { updateState($stateFile, 'error', 0, 'فشل توليد المحتوى من الذكاء الاصطناعي. قد يكون الملف ضخماً جداً أو الـ API Key غير صالح.'); exit; } $jsonResponse = json_decode($res, true); $text = $jsonResponse['candidates'][0]['content']['parts'][0]['text'] ?? ''; $parsedStructure = json_decode($text, true); if (empty($parsedStructure) || empty($parsedStructure['units'])) { updateState($stateFile, 'error', 0, 'فشل الذكاء الاصطناعي في إرجاع بنية JSON صالحة.'); exit; } updateState($stateFile, 'generating', 80, 'الذكاء الاصطناعي انتهى من القراءة! جاري بناء الملفات وحفظها على السيرفر...'); // Save to disk $updatedTree = CurriculumService::mergeExtractedCurriculum($parsedStructure); $firstLessonFile = ''; if (!empty($parsedStructure['units'][0]['lessons'][0])) { $firstLes = $parsedStructure['units'][0]['lessons'][0]; $firstLessonFile = "{$parsedStructure['grade_key']}/{$parsedStructure['subject_key']}/{$parsedStructure['semester_key']}/{$parsedStructure['units'][0]['unit_key']}/{$firstLes['lesson_id']}.md"; } updateState($stateFile, 'completed', 100, 'تم الاستخراج المعتمد على الـ AI بالكامل بنجاح!', [ 'active_file' => $firstLessonFile, 'tree' => $updatedTree, 'extracted_data' => $parsedStructure ]);