Below is the code: I am upload file from HTML page and sending to PHP file.
I tried several ways, is this the wrong way to do it?
getting error : $upload_response[‘id’] is not set
{ “error”: { “message”: “‘file’ is a required property”, “type”: “invalid_request_error”, “param”: null, “code”: null } } Error: Failed to upload document.
Index HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload MS Word Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h2>Upload MS Word Document</h2>
<input type="file" id="word_document" name="word_document" accept=".docx">
<br><br>
<button id="uploadButton">Upload Document</button>
<div id="summary"></div>
<script>
$(document).ready(function () {
$('#uploadButton').click(function () {
var formData = new FormData();
var file = $('#word_document')[0].files[0];
formData.append('word_document', file);
$.ajax({
url: 'process.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (response) {
$('#summary').html("<strong>Document Summary:</strong> " + response);
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
}
});
});
});
</script>
</body>
</html>
process.php:
<?php
// Function to call OpenAI API
function call_openai_api($endpoint, $data) {
$api_key = 'My API KeY'; // Replace with your OpenAI API key
$url = 'https://api.openai.com/v1/' . $endpoint;
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
return json_decode($response, true);
}
// Function to generate MCQ questions
function generate_mcq_questions($file_path) {
// Upload document
$document_content = file_get_contents($file_path);
$upload_response = call_openai_api('files', array(
'purpose' => 'fine-tune',
'file' => array(
'data' => $document_content,
'filename' => basename($file_path),
'name' => 'word_document' // Specify the name property for the file data
)
));
// Check if upload was successful
if (!isset($upload_response['id'])) {
return "Error: Failed to upload document.";
}
// Generate MCQ questions
$systemPrompt = "Create 2 MCQ questions on the provided document. The resulting JSON object should be in this format:";
$userPrompt = "{'questionArray':[{ 'questionText':'Question text here', 'options':[ {'text':'','isCorrect':true}, {'text':'','isCorrect':false}, {'text':'','isCorrect':false}, {'text':'','isCorrect':false} ], 'correctFeedback':'', 'inCorrectFeedback':'' }]}";
$mcq_response = call_openai_api('completions', array(
'model' => 'gpt-4-turbo-preview',
'temperature' => 0,
'documents' => [$upload_response['id']],
'max_tokens' => 100,
'messages' => array(
array(
'role' => 'user',
'content' => $userPrompt
),
array(
'role' => 'system',
'content' => $systemPrompt
)
)
));
return $mcq_response['choices'][0]['message']['content'];
}
// Validate uploaded file
if (isset($_FILES['word_document']) && $_FILES['word_document']['error'] === UPLOAD_ERR_OK) {
$file_name = $_FILES['word_document']['name'];
$file_size = $_FILES['word_document']['size'];
$file_tmp = $_FILES['word_document']['tmp_name'];
$file_type = $_FILES['word_document']['type'];
// Check file extension
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$allowed_extensions = array('docx');
if (!in_array($file_ext, $allowed_extensions)) {
echo "Error: Only .docx files are allowed.";
exit;
}
// Check file size (max 5 MB)
$max_file_size = 5 * 1024 * 1024; // 5 MB
if ($file_size > $max_file_size) {
echo "Error: File size exceeds maximum limit (5 MB).";
exit;
}
// Upload and summarize the document
$upload_dir = 'uploads/';
$upload_path = $upload_dir . $file_name;
if (move_uploaded_file($file_tmp, $upload_path)) {
$question = 'What is the summary of this document?';
$summary = generate_mcq_questions($upload_path);
echo $summary; // Output the summary
} else {
echo "Error: Failed to upload file.";
}
} else {
echo "Error: No file selected or an error occurred while uploading.";
}
?>
type or paste code here