-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai-search.php
executable file
·144 lines (132 loc) · 4.46 KB
/
ai-search.php
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Interface</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
transition: background-color 0.3s, color 0.3s;
}
body.light {
background-color: #f4f4f4;
color: #333;
}
body.dark {
background-color: #222;
color: #ddd;
}
form {
margin-bottom: 20px;
}
input[type="text"], input[type="submit"] {
padding: 8px;
font-size: 16px;
}
input[type="text"] {
width: 300px;
}
input[type="submit"] {
cursor: pointer;
}
.theme-switcher {
margin-bottom: 20px;
}
.split-view {
display: flex;
gap: 20px;
}
.normal-output, .json-output {
flex: 1;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
max-height: 400px;
overflow-y: auto;
}
.json-output {
background-color: #282c34;
color: #61dafb;
}
pre {
white-space: pre-wrap; /* Allows wrapping */
word-wrap: break-word; /* Break long words */
}
h1, h2, h3 {
margin-top: 0;
}
</style>
</head>
<body class="light">
<h1>Search</h1>
<div class="theme-switcher">
<label for="theme">Choose Theme:</label>
<select id="theme" onchange="toggleTheme()">
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</div>
<form method="POST" action="">
<input type="text" name="query" placeholder="Enter search term" required>
<input type="submit" value="Search">
</form>
<div class="split-view">
<div class="normal-output">
<h2>Search Results</h2>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['query'])) {
$query = $_POST['query'];
$max_results = 10;
$url = 'http://127.0.0.1:8000/faiss/search/';
$data = array(
'query' => $query,
'max_results' => $max_results
);
$json_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_status == 200) {
$results = json_decode($response, true);
if (!empty($results['faiss_results'])) {
foreach ($results['faiss_results'] as $result) {
echo '<div class="result">';
echo '<h3>' . htmlspecialchars($result['thread_title']) . '</h3>';
echo '<p>' . htmlspecialchars($result['message']) . '</p>';
echo '<p class="meta">Post Date: ' . date('Y-m-d H:i:s', $result['post_date']) . '</p>';
echo '</div>';
}
} else {
echo '<p>No results found for "' . htmlspecialchars($query) . '".</p>';
}
} else {
echo '<p>An error occurred while processing the search. Please try again later.</p>';
}
}
?>
</div>
<div class="json-output">
<h2>JSON Output</h2>
<pre><?php
if (!empty($response)) {
// Pretty print the JSON response
echo htmlspecialchars(json_encode(json_decode($response), JSON_PRETTY_PRINT));
}
?></pre>
</div>
</div>
<script>
function toggleTheme() {
const theme = document.getElementById('theme').value;
document.body.className = theme;
}
</script>
</body>
</html>