-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini-api.php
More file actions
231 lines (196 loc) · 6.74 KB
/
gemini-api.php
File metadata and controls
231 lines (196 loc) · 6.74 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Gemini AI API Integration
* Main endpoint for processing text and file uploads with Google's Gemini AI
*
* @version 2.1
* @author Gemini PHP Bridge Team
* @license MIT
*/
// ========================================
// CONFIGURATION CONSTANTS
// ========================================
define('GOOGLE_API_KEY', 'your-google-api-key-here'); // Update with your actual API key
define('BASE_API_URL', 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent');
define('MAX_FILE_SIZE', '10M');
define('MAX_INPUT_LENGTH', 100000);
define('RATE_LIMIT_PER_MINUTE', 60);
// Validate critical configuration
if (GOOGLE_API_KEY === 'your-google-api-key-here' || empty(GOOGLE_API_KEY)) {
http_response_code(500);
die(json_encode(['error' => 'Google API key not configured. Please edit gemini-api.php line 14.']));
}
// Configuration array
$config = [
'api' => [
'key' => GOOGLE_API_KEY,
'endpoint' => BASE_API_URL,
'model' => 'gemini-2.0-flash-exp'
],
'generation' => [
'temperature' => 1,
'topK' => 40,
'topP' => 0.95,
'maxOutputTokens' => 8192,
'responseMimeType' => 'text/plain'
],
'upload' => [
'temp_dir' => 'temp_files',
'max_file_size' => MAX_FILE_SIZE,
'allowed_extensions' => ['txt', 'pdf', 'docx', 'md'],
'cleanup_after_hours' => 24
],
'logging' => [
'enabled' => true,
'level' => 'DEBUG',
'file' => 'logs/gemini_api.log',
'max_size' => '50M'
],
'security' => [
'ssl_verify' => true,
'rate_limit_requests_per_minute' => RATE_LIMIT_PER_MINUTE,
'max_input_length' => MAX_INPUT_LENGTH
]
];
// Initialize logging
class Logger {
private string $logFile;
private bool $enabled;
public function __construct(array $config) {
$this->logFile = $config['logging']['file'];
$this->enabled = $config['logging']['enabled'];
// Ensure log directory exists
$logDir = dirname($this->logFile);
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
}
public function log(string $level, string $message): void {
if (!$this->enabled) return;
$timestamp = date('Y-m-d H:i:s');
$logMessage = "[{$timestamp}] [{$level}] {$message}\n";
file_put_contents($this->logFile, $logMessage, FILE_APPEND | LOCK_EX);
}
public function debug(string $message): void { $this->log('DEBUG', $message); }
public function info(string $message): void { $this->log('INFO', $message); }
public function warning(string $message): void { $this->log('WARNING', $message); }
public function error(string $message): void { $this->log('ERROR', $message); }
}
$logger = new Logger($config);
$logger->info("Request received: " . $_SERVER['REQUEST_METHOD'] . " " . $_SERVER['REQUEST_URI']);
/**
* API Response Helper
*/
function sendResponse(array $data, int $statusCode = 200): void {
http_response_code($statusCode);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
/**
* Error Handler
*/
function handleError(string $message, int $statusCode = 400): void {
global $logger;
$logger->error($message);
sendResponse(['error' => $message], $statusCode);
}
// Handle test requests
if (isset($_POST['test']) && $_POST['test'] === 'true') {
$logger->info("Test request received");
sendResponse(['status' => 'OK', 'message' => 'API is working']);
}
// Initialize input processing
$inputText = '';
// Handle file uploads
if (isset($_FILES['textFile']) && $_FILES['textFile']['error'] === UPLOAD_ERR_OK) {
$logger->debug("Text file upload detected");
$inputText = file_get_contents($_FILES['textFile']['tmp_name']);
$logger->info("Text file processed, length: " . strlen($inputText));
}
// Handle PDF uploads
if (isset($_FILES['pdfFile']) && $_FILES['pdfFile']['error'] === UPLOAD_ERR_OK) {
$logger->debug("PDF file upload detected");
$originalFileName = $_FILES['pdfFile']['name'];
$fileExtension = strtolower(pathinfo($originalFileName, PATHINFO_EXTENSION));
if ($fileExtension !== 'pdf') {
handleError("Invalid file type. Only PDF files are accepted.");
}
// Create temp directory if it doesn't exist
$tempDir = $config['upload']['temp_dir'];
if (!is_dir($tempDir)) {
mkdir($tempDir, 0755, true);
}
// Generate unique filename
$uniqueFileName = uniqid() . '.pdf';
$tempFilePath = $tempDir . '/' . $uniqueFileName;
if (!move_uploaded_file($_FILES['pdfFile']['tmp_name'], $tempFilePath)) {
handleError("Failed to save uploaded file", 500);
}
$fileUrl = './' . $tempFilePath;
$inputText .= " Uploaded PDF: " . $fileUrl;
$logger->info("PDF file saved: " . $fileUrl);
}
// Handle raw input
if (empty($inputText)) {
$rawInput = file_get_contents('php://input');
if (!empty($rawInput)) {
$inputText = trim($rawInput);
$logger->debug("Raw input received, length: " . strlen($inputText));
}
}
// Validate input
if (empty($inputText)) {
handleError("No input text or file provided");
}
// Security check - limit input length
if (strlen($inputText) > $config['security']['max_input_length']) {
handleError("Input text too long. Maximum length: " . $config['security']['max_input_length']);
}
$logger->info("Processing input, length: " . strlen($inputText));
// Prepare API payload
$payload = [
'contents' => [
[
'role' => 'user',
'parts' => [
['text' => $inputText]
]
]
],
'generationConfig' => $config['generation']
];
$jsonPayload = json_encode($payload);
$apiUrl = $config['api']['endpoint'] . '?key=' . $config['api']['key'];
$logger->debug("Calling Gemini API");
// Initialize cURL
$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonPayload,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_SSL_VERIFYPEER => $config['security']['ssl_verify'],
CURLOPT_TIMEOUT => 30
]);
// Execute API call
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Handle cURL errors
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
handleError("API connection failed: " . $error, 500);
}
curl_close($ch);
// Process response
if ($httpCode === 200) {
$logger->info("API call successful");
// Return raw response for client processing
header('Content-Type: application/json');
echo $response;
} else {
$logger->error("API call failed with HTTP code: " . $httpCode);
handleError("API request failed", $httpCode);
}
?>