35853-vm/index.php
2025-11-19 16:08:07 +00:00

111 lines
5.7 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI CSV Analyzer</title>
<meta name="description" content="Upload a CSV and get an AI-powered analysis with a dataset description and chart suggestions.">
<meta name="keywords" content="csv analysis, ai data analysis, data visualization, business intelligence, data insights, csv uploader, ai-powered charts, data analytics, data processing, flatlogic generator">
<meta property="og:title" content="AI CSV Analyzer">
<meta property="og:description" content="Upload a CSV and get an AI-powered analysis with a dataset description and chart suggestions.">
<meta property="og:image" content="">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">AI CSV Analyzer</a>
</div>
</nav>
<main class="container">
<div class="hero-section text-center p-5 rounded-lg my-4">
<h1 class="display-4">Analyze Your CSV Data with AI</h1>
<p class="lead">Upload your CSV file, and our AI will provide a description of the dataset and suggest relevant charts.</p>
<form action="index.php" method="post" enctype="multipart/form-data" class="mt-4">
<div class="input-group mb-3">
<input type="file" class="form-control" name="csv_file" id="csv_file" accept=".csv" required>
<button class="btn btn-primary" type="submit">Analyze CSV</button>
</div>
</form>
<?php
$analysis_result = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
if ($_FILES['csv_file']['error'] === UPLOAD_ERR_OK) {
$file_tmp_path = $_FILES['csv_file']['tmp_name'];
$file_name = htmlspecialchars($_FILES['csv_file']['name']);
// --- CSV Processing ---
$csv_data = [];
$row_limit = 6; // Header + 5 rows
if (($handle = fopen($file_tmp_path, "r")) !== FALSE) {
$i = 0;
while (($data = fgetcsv($handle)) !== FALSE && $i < $row_limit) {
$csv_data[] = $data;
$i++;
}
fclose($handle);
}
if (!empty($csv_data)) {
// --- AI Analysis ---
require_once __DIR__ . '/ai/LocalAIApi.php';
$header = array_shift($csv_data);
$prompt = "Analyze the following CSV data. The first line is the header, followed by a few sample rows.\n\n";
$prompt .= "Header: " . implode(', ', $header) . "\n";
$prompt .= "Sample Rows:\n";
foreach ($csv_data as $row) {
$prompt .= implode(', ', $row) . "\n";
}
$prompt .= "\nBased on this data, provide:
1. A brief, insightful description of the dataset. What kind of information does it seem to contain?
2. Suggestions for 2-3 useful charts that could be created from this data. For each suggestion, specify the chart type (e.g., Bar Chart, Line Chart, Scatter Plot) and explain what insights it would provide.";
$resp = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are a data analysis expert.'],
['role' => 'user', 'content' => $prompt],
],
]);
if (!empty($resp['success'])) {
$ai_text = LocalAIApi::extractText($resp);
// Use nl2br to preserve line breaks from the AI response
$analysis_result = "<div class='alert alert-success mt-3 text-start'><h4>Analysis for ".htmlspecialchars($file_name)."</h4><p>" . nl2br(htmlspecialchars($ai_text)) . "</p></div>";
} else {
$error_message = $resp['error'] ?? 'An unknown error occurred with the AI API.';
$analysis_result = "<div class='alert alert-danger mt-3'>Error during AI analysis: " . htmlspecialchars($error_message) . "</div>";
}
} else {
$analysis_result = "<div class='alert alert-warning mt-3'>Could not read data from the CSV file.</div>";
}
} else {
$analysis_result = "<div class='alert alert-danger mt-3'>Error uploading file.</div>";
}
echo $analysis_result;
}
?>
</div>
</main>
<footer class="footer mt-auto py-3 bg-light">
<div class="container text-center">
<span class="text-muted">Built with Flatlogic</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>