From b388b6375e71d1cc4486d414383458d7013c6d8e Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Wed, 19 Nov 2025 12:21:30 +0000 Subject: [PATCH] basic AI analytics --- assets/js/main.js | 50 ++++++++++++++--------- upload.php | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 18 deletions(-) create mode 100644 upload.php diff --git a/assets/js/main.js b/assets/js/main.js index e2f8e2e..015b706 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -55,26 +55,40 @@ document.addEventListener('DOMContentLoaded', function () { spinner.classList.remove('d-none'); submitButton.disabled = true; - // --- FAKE LOADER FOR DEMO --- - // In the next step, we will replace this with a real AJAX call to the backend. - setTimeout(() => { + const formData = new FormData(uploadForm); + + // Show loading state + resultsSection.style.display = 'block'; + analysisOutput.innerHTML = ` +
+
+ Loading... +
+

AI is analyzing your data, this may take a moment...

+
+ `; + resultsSection.scrollIntoView({ behavior: 'smooth' }); + + fetch('upload.php', { + method: 'POST', + body: formData + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + analysisOutput.innerHTML = data.analysis; + } else { + analysisOutput.innerHTML = `
${data.error || 'An unknown error occurred.'}
`; + } + }) + .catch(error => { + console.error('Error:', error); + analysisOutput.innerHTML = `
An error occurred while communicating with the server.
`; + }) + .finally(() => { // Hide spinner and re-enable button spinner.classList.add('d-none'); submitButton.disabled = false; - - // Show results section with a placeholder message - resultsSection.style.display = 'block'; - analysisOutput.innerHTML = ` -
-
- Loading... -
-

AI is analyzing your data... This will be implemented in the next step.

-
- `; - // Scroll to results - resultsSection.scrollIntoView({ behavior: 'smooth' }); - - }, 2000); + }); }); }); diff --git a/upload.php b/upload.php new file mode 100644 index 0000000..3554623 --- /dev/null +++ b/upload.php @@ -0,0 +1,100 @@ + false, 'error' => $message]); + exit; +} + +// 1. Handle File Upload +if (!isset($_FILES['csv_file']) || $_FILES['csv_file']['error'] !== UPLOAD_ERR_OK) { + return_error('File upload error. Please try again. Code: ' . ($_FILES['csv_file']['error'] ?? 'Unknown')); +} + +$file = $_FILES['csv_file']; +$tmpPath = $file['tmp_name']; + +// 2. Validate File Type (Basic Check) +$fileType = mime_content_type($tmpPath); +$fileName = $file['name']; +$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); + +if ($fileType !== 'text/csv' && $fileType !== 'text/plain' && $fileExtension !== 'csv') { + return_error('Invalid file type. Please upload a valid CSV file.'); +} + +// 3. Read CSV Header and First 2 Rows +$dataSample = ''; +$handle = fopen($tmpPath, 'r'); +if (!$handle) { + return_error('Failed to open the uploaded file.'); +} + +$lineCount = 0; +while (($row = fgetcsv($handle)) !== false && $lineCount < 3) { + $dataSample .= implode(',', array_map(function($cell) { + return '"' . addslashes($cell) . '"'; + }, $row)) . "\n"; + $lineCount++; +} +fclose($handle); + +if (empty($dataSample)) { + return_error('The CSV file appears to be empty or could not be read.'); +} + +// 4. Prepare Prompt for AI +$prompt = <<` and `
  • ` tags. +3. **Suggest Key Statistics:** In a bulleted list, suggest 2-3 key statistical calculations (e.g., average, sum, distribution) that would be meaningful for this dataset. Use `
      ` and `
    • ` tags. + +Structure your response using HTML tags like `

      `, `

      `, and `

        `. +PROMPT; + + +// 5. Call AI API +try { + $resp = LocalAIApi::createResponse( + [ + 'input' => [ + ['role' => 'system', 'content' => 'You are a helpful data analysis assistant.'], + ['role' => 'user', 'content' => $prompt], + ], + ] + ); + + if (empty($resp['success'])) { + $errorMsg = 'AI API Error: ' . ($resp['error'] ?? 'Unknown error'); + error_log($errorMsg); + return_error('Failed to get a response from the AI. Please try again later.'); + } + + $aiText = LocalAIApi::extractText($resp); + + if (empty($aiText)) { + $decoded = LocalAIApi::decodeJsonFromResponse($resp); + $aiText = $decoded ? '
        ' . htmlspecialchars(json_encode($decoded, JSON_PRETTY_PRINT)) . '
        ' : 'The AI returned an empty response.'; + } + + echo json_encode(['success' => true, 'analysis' => $aiText]); + +} catch (Exception $e) { + error_log('Exception in AI call: ' . $e->getMessage()); + return_error('An unexpected error occurred while processing the file with AI.'); +} + +