From cfeff636ba0a7da9dfa40d71a8b03f6294638ffe Mon Sep 17 00:00:00 2001 From: velz Date: Thu, 23 Oct 2025 10:46:57 +0530 Subject: [PATCH] TEST_POLICY_DOC_READ --- app/Config/Routes.php | 1 + app/Controllers/Home.php | 139 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 877ba46e..f6746f2d 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -48,6 +48,7 @@ $routes->get("frontend_content", "AppContentManagementController::frontend_conte $routes->get('/test', 'Home::index'); $routes->get('/check_gemini', 'Home::check_Gemini'); $routes->get('/check_gemini2', 'Home::check_gemini_2'); +$routes->get('/checkPolicyDoc', 'Home::checkPolicyDoc'); $routes->get('/login', 'LoginController::index'); ///auth/google $routes->get('/loginPos', 'LoginController::loginPos'); //login POS team $routes->post('/getVerifyPosMobileNo', 'LoginController::getVerifyPosMobileNo'); //Verify POS team mobile no diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 68eb0402..84d0d8c9 100755 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -60,6 +60,8 @@ class Home extends PublicController //print_rr($data); } + + //for insurer statement upload public function check_gemini_2() { @@ -158,6 +160,143 @@ class Home extends PublicController + // The final JSON payload + $data = [ + "contents" => [ + $content_parts + ] + ]; + + // Encode the data to a JSON string + $json_data = json_encode($data); + + // Initialize cURL + $ch = curl_init($url); + + // Set cURL options + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // Set the Content-Type header + curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); // Set the JSON payload + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Optional: to bypass SSL verification if needed (not recommended for production) + + // Execute the cURL request and get the response + $response = curl_exec($ch); + + // Check for cURL errors + if (curl_errno($ch)) { + echo 'cURL Error: ' . curl_error($ch); + } + + // Close the cURL handle + curl_close($ch); + + // Decode the JSON response + $responseData = json_decode($response, true); + echo '
';
+                print_r($responseData);
+            echo '
';
+            // Check if the response contains generated text
+            if (isset($responseData['candidates'][0]['content']['parts'][0]['text'])) {
+                $generatedText = $responseData['candidates'][0]['content']['parts'][0]['text'];
+                echo "Generated Text: " . $generatedText;
+            } else {
+                echo "Error or no text generated. Response: " . $response;
+            }
+
+    }
+
+
+    //for insurer policy doc upload
+    public function checkPolicyDoc()
+    {
+
+            // $this->convertToPdf();die();
+            // Replace with your actual Gemini API key
+           $apiKey = 'AIzaSyBbx-uotRBqkYhqLpmD60420E_a0G0duP8';
+
+            // The model to use and the API endpoint
+            $model = "gemini-pro";
+            $model = "gemini-2.5-flash";
+            // $model = "gemini-1.5-pro";
+            // $model = "gemini-1.5-pro";
+            $url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent?key={$apiKey}";
+
+            $filePath = 'C:\Users\Venba\AppData\Local\Programs\Python\pyenv\pdfreader\bike-0904023124P114268957.pdf';
+            $filePath = 'C:\Users\Venba\AppData\Local\Programs\Python\pyenv\pdfreader\car-insurance-0904023124P114213011.pdf';
+
+            // Check if the file exists
+            if (!file_exists($filePath)) {
+                die("Error: File not found at {$filePath}");
+            }
+
+            // Get the file's MIME type using the finfo extension
+            $finfo = finfo_open(FILEINFO_MIME_TYPE);
+            $mimeType = finfo_file($finfo, $filePath);
+            finfo_close($finfo);
+            // print_r($mimeType);die();
+             // Define supported inline MIME types
+             $supportedInlineMimeTypes = ['application/pdf','text/csv'];
+
+            // Read the file content and encode it to Base64
+            $fileContent = file_get_contents($filePath);
+            $base64Content = base64_encode($fileContent);
+            // The prompt you want to send to the model
+            // $prompt = "give me a json with emp data like name,age,dob,mobile and email. only json not any explanations";
+            $prompt = "Read the following motor policy pdf document and convert into JSON format as sample specified. Give me only JSON,not any explanations.";
+            $prompt .= '"{\"policy\":{\"policy_number\":\"\",\"issue_date\":\"\",\"period\":{\"start\":\"\",\"end\":\"\"},\"insurer\":\"\",\"previous_policy_number\":\"\"},\"insured\":{\"name\":\"\",\"father_name\":\"\",\"address\":[\"\"],\"mobile\":\"\",\"id_proofs\":{\"aadhaar\":\"\",\"pan\":\"\"}},\"vehicle\":{\"reg_no\":\"\",\"engine_no\":\"\",\"chassis_no\":\"\",\"make\":\"\",\"model\":\"\",\"year\":\"\",\"cubic_capacity\":\"\",\"vehicle_type\":\"\"},\"rto\":\"\",\"premium\":{\"tp\":0,\"od\":0,\"pa_od\":0,\"taxes\":{},\"total\":0,\"in_words\":\"\"},\"endorsements\":[{\"code\":\"\",\"desc\":\"\"}]}"';
+
+            $payloadPart = null;
+
+            // The text part of the prompt
+            $text_part = [
+                "text" => $prompt
+            ];
+            // Conditionally handle the file upload based on MIME type
+            if (in_array($mimeType, $supportedInlineMimeTypes)) {
+                 echo "Detected supported format inline MIME type ({$mimeType})";
+                // Handle PDF as inline data
+                $fileContent = file_get_contents($filePath);
+                $base64Content = base64_encode($fileContent);
+                $payloadPart = [
+                    "inlineData" => [
+                        "mimeType" => $mimeType,
+                        "data" => $base64Content
+                    ]
+                ];
+
+                 $content_parts = [
+                        "parts" => [
+                            $text_part,
+                            $payloadPart
+                        ]
+                ];
+            } else {
+                // Handle Excel/CSV using the Files API
+                echo "Detected unsupported inline MIME type ({$mimeType}). Uploading via Files API...\n";
+                $fileInfo = $this->uploadFileToGemini($filePath, $apiKey); // Pass apiKey
+
+                print_r($fileInfo);
+                 // The file part, using the URI from the Files API upload
+                    $file_part = [
+                        "fileData" => [
+                            "fileUri" => $fileInfo['uri'],
+                            "mimeType" => $fileInfo['mimeType']
+                        ]
+                    ];
+
+                    // The full content array, containing both parts
+                    $content_parts = [
+                        "parts" => [
+                            $text_part,
+                            $file_part
+                        ]
+                    ];
+            }
+
+           
+
+          
+
             // The final JSON payload
             $data = [
                 "contents" => [