-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
105 lines (92 loc) · 3.69 KB
/
Copy pathapi.php
File metadata and controls
105 lines (92 loc) · 3.69 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
<?php
header('Content-Type: application/json');
// Simple Router
$request_uri = $_SERVER['REQUEST_URI'] ?? '';
$path = parse_url($request_uri, PHP_URL_PATH);
if ($path === '/' || $path === '/index.php') {
include 'index.php';
exit;
} elseif ($path === '/generate') {
handleGenerate();
} elseif ($path === '/health') {
echo json_encode(['status' => 'ok', 'model' => 'Sheikh-ABF']);
} elseif ($path === '/info') {
$config_file = 'Sheikh-ABF/config.json';
if (file_exists($config_file)) {
$config = json_decode(file_get_contents($config_file), true);
echo json_encode([
'name' => 'Sheikh-ABF',
'version' => '4.5',
'creator' => 'Likhon Sheikh',
'config' => $config
]);
} else {
echo json_encode(['error' => 'Config not found']);
}
} else {
http_response_code(404);
echo json_encode(['error' => 'Not Found', 'path' => $path]);
}
function handleGenerate() {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method Not Allowed']);
return;
}
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if (!$data || !isset($data['prompt'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid input, "prompt" is required']);
return;
}
// Call Python FastAPI server
$url = 'http://127.0.0.1:5000/generate';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
$result = json_decode($response, true);
if ($result) {
if (isset($result['generated_text'])) {
$text = $result['generated_text'];
$prompt = $result['prompt'] ?? '';
// Improved prompt stripping logic
// LLMs often prepend <bos> (token 0) if not present, or it might be in the prompt already.
// We'll try to find the prompt in the text.
$completion = $text;
$prompt_pos = strpos($text, $prompt);
if ($prompt_pos !== false) {
$completion = substr($text, $prompt_pos + strlen($prompt));
}
$result['completion'] = trim($completion);
// Handle thinking blocks
$thinking = '';
$answer = $text;
if (strpos($text, '<think>') !== false && strpos($text, '</think>') !== false) {
$start = strpos($text, '<think>') + strlen('<think>');
$end = strpos($text, '</think>');
$thinking = substr($text, $start, $end - $start);
$answer = substr($text, $end + strlen('</think>'));
} elseif (strpos($text, '<think>') !== false) {
$start = strpos($text, '<think>') + strlen('<think>');
$thinking = substr($text, $start);
$answer = '';
}
$result['thinking'] = trim($thinking);
$result['answer'] = trim($answer);
}
echo json_encode($result);
} else {
http_response_code(500);
echo json_encode(['error' => 'Failed to parse model server response', 'raw' => $response]);
}
} else {
http_response_code($http_code ?: 500);
echo json_encode(['error' => 'Model server error', 'details' => $response]);
}
}