-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_sampling_request.php
More file actions
85 lines (69 loc) · 2.54 KB
/
Copy pathprocess_sampling_request.php
File metadata and controls
85 lines (69 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
session_start();
include('db_connect.php');
require 'vendor/autoload.php'; // Load Composer dependencies
use Dotenv\Dotenv;
use OpenAI\Client;
// Load environment variables
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Retrieve API key from .env file
$apiKey = $_ENV['OPENAI_API_KEY'] ?? null;
if (!$apiKey) {
die("Fatal Error: OpenAI API Key is missing. Please check your .env file.");
}
// Create OpenAI client
$client = OpenAI::client($apiKey);
if (!isset($_GET['file'])) {
die("No file specified.");
}
$file_path = urldecode($_GET['file']);
// Convert Image/PDF to Text (Use OCR)
$text = ocr_extract_text($file_path);
// Translate Text to English
$translated_text = translate_to_english($text);
// Extract Event Information Using AI
$event_details = extract_event_details($translated_text);
if ($event_details) {
// Insert event into the database
$stmt = $conn->prepare("INSERT INTO events (event_name, event_date, event_time, event_duration, sampling_count, contact_person, contact_phone) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssiss",
$event_details['event_name'],
$event_details['event_date'],
$event_details['event_time'],
$event_details['event_duration'],
$event_details['sampling_count'],
$event_details['contact_person'],
$event_details['contact_phone']
);
$stmt->execute();
$stmt->close();
echo "Event Created Successfully!";
} else {
echo "Failed to extract event details.";
}
// OCR Function to Extract Text from PDF/Image
function ocr_extract_text($file_path) {
return "Sample OCR Extracted Text"; // Placeholder, integrate with OCR library
}
// Translate Text to English Using OpenAI API
function translate_to_english($text) {
global $client;
$response = $client->completions()->create([
'model' => 'gpt-3.5-turbo', // Use updated model
'prompt' => "Translate this to English: $text",
'max_tokens' => 100,
]);
return trim($response['choices'][0]['text']);
}
// Extract Event Details Using OpenAI API
function extract_event_details($text) {
global $client;
$response = $client->completions()->create([
'model' => 'gpt-3.5-turbo', // Use updated model
'prompt' => "Extract event details from this text:\n\n$text\n\nProvide output as JSON with fields: event_name, event_date, event_time, event_duration, sampling_count, contact_person, contact_phone.",
'max_tokens' => 150,
]);
return json_decode($response['choices'][0]['text'], true);
}
?>