36487-vm/analyze.php
2025-11-30 05:59:02 +00:00

71 lines
2.2 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/ai/LocalAIApi.php';
if (isset($_FILES['resume']) && $_FILES['resume']['error'] === UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['resume']['tmp_name'];
// Read the file content
$resumeContent = file_get_contents($fileTmpPath);
if ($resumeContent === false) {
echo json_encode(['success' => false, 'message' => 'Failed to read resume file.']);
exit;
}
// Define the AI prompt
$prompt = <<<PROMPT
Act as an expert Applicant Tracking System (ATS) resume analyzer. Analyze the following resume content and return a JSON object with your analysis.
The JSON object must have the following structure:
{
"score": <an integer score from 0 to 100 representing ATS compatibility>,
"status": "<a short status like 'Strong Candidate', 'Needs Improvement', or 'Poor Fit'>",
"strengths": [
"<a brief description of a positive aspect of the resume>",
"<another positive aspect>"
],
"weaknesses": [
"<a brief description of a negative aspect or area for improvement>",
"<another area for improvement>"
]
}
Here is the resume content:
---
{$resumeContent}
---
PROMPT;
// Call the AI
$response = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are an expert ATS resume analyzer that only responds with JSON.'],
['role' => 'user', 'content' => $prompt],
],
]);
if (empty($response['success'])) {
error_log('AI API Error: ' . ($response['error'] ?? 'Unknown error'));
echo json_encode(['success' => false, 'message' => 'AI analysis failed. Please try again later.']);
exit;
}
$analysisJson = LocalAIApi::decodeJsonFromResponse($response);
if ($analysisJson === null) {
error_log('AI API JSON Decode Error: ' . LocalAIApi::extractText($response));
echo json_encode(['success' => false, 'message' => 'Failed to parse AI response. The response may not be valid JSON.']);
exit;
}
echo json_encode(['success' => true, 'data' => $analysisJson]);
} else {
echo json_encode([
'success' => false,
'message' => 'File upload failed or no file was uploaded.'
]);
}
?>