From a20866eec89e3af9329f9b30980e7c11263e70e1 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Fri, 28 Aug 2026 16:30:12 +0300 Subject: [PATCH] Update Saqel Platform: 2026-08-28 16:30:12 --- backend/scripts/curriculum_worker.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/backend/scripts/curriculum_worker.php b/backend/scripts/curriculum_worker.php index e2872c9..42981ff 100644 --- a/backend/scripts/curriculum_worker.php +++ b/backend/scripts/curriculum_worker.php @@ -33,7 +33,23 @@ $state = json_decode(file_get_contents($stateFile), true); $origName = $state['orig_name'] ?? 'Unknown.pdf'; Env::load(__DIR__ . '/../.env'); -$geminiKey = Env::get('GEMINI_API_KEY') ?: getenv('GEMINI_API_KEY'); +$rawKeys = Env::get('GEMINI_API_KEY') ?: getenv('GEMINI_API_KEY'); +$apiKeys = array_values(array_filter(array_map('trim', explode(',', $rawKeys)))); + +if (empty($apiKeys)) { + updateState($stateFile, 'error', 0, '⚠️ لم يتم العثور على مفتاح الذكاء الاصطناعي (GEMINI_API_KEY) في ملف .env.'); + exit; +} + +// Round Robin Logic +$rrFile = sys_get_temp_dir() . '/saqel_gemini_rr.txt'; +$currentIndex = file_exists($rrFile) ? (int)file_get_contents($rrFile) : 0; +$geminiKey = $apiKeys[$currentIndex % count($apiKeys)]; +file_put_contents($rrFile, ($currentIndex + 1) % count($apiKeys)); + +// Add info about which key is used (masked for security) +$maskedKey = substr($geminiKey, 0, 6) . '...' . substr($geminiKey, -4); +updateState($stateFile, 'uploading_to_ai', 15, "تم تفعيل Round Robin. جاري استخدام المفتاح: {$maskedKey}"); if (empty($geminiKey)) { updateState($stateFile, 'error', 0, '⚠️ لم يتم العثور على مفتاح الذكاء الاصطناعي (GEMINI_API_KEY) في ملف .env. النظام يحتاج إلى الـ AI لقراءة وتفريغ الكتب (بما فيها الكتب المصورة والرياضيات) بشكل حقيقي.'); @@ -144,8 +160,12 @@ $payload = [ '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; +// Delay 4 seconds to respect free tier rate limits +updateState($stateFile, 'analyzing', 70, 'جاري تطبيقหน่วงزمني (4 ثوانٍ) لتفادي حدود الاستخدام (Rate Limit)...'); +sleep(4); + +// Using gemini-2.0-flash as requested by the user +$url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=" . $geminiKey; $ch = curl_init($url); curl_setopt_array($ch, [