-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrontend.php
277 lines (236 loc) · 9.26 KB
/
frontend.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
// Handling the backend processing for the chatbot
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
header("Content-Type: application/json");
// Read the input JSON data
$input = json_decode(file_get_contents("php://input"), true);
// Check if the input data is properly decoded and the 'message' key exists
if (is_null($input)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid input: JSON data not decoded']);
exit;
}
if (!isset($input['message'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid input: message key missing']);
exit;
}
$userMessage = $input['message'];
// Save the user message to a temporary file
file_put_contents("user_message.txt", $userMessage);
// Call the Python script and capture the output
$command = escapeshellcmd('python chatbot_model.py');
$output = shell_exec($command);
if ($output === null) {
http_response_code(500);
echo json_encode(['error' => 'Failed to generate response']);
exit;
}
echo json_encode(['response' => trim($output)]);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Skytrade Bot</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow: hidden; /* Prevent overflow */
background-image: url('bg.jpg'); /* Background image */
background-size: cover; /* Cover the entire viewport */
background-position: center; /* Center the image */
}
.drone {
position: absolute;
width: 100px; /* Size of the drone */
height: 100px;
background-image: url('drone.png'); /* Your drone image */
background-size: contain;
background-repeat: no-repeat;
}
.chat-container {
width: 67vw; /* 67% of viewport width */
height: 80vh; /* Fixed height */
border: 1px solid #ccc;
border-radius: 20px; /* More curvature */
overflow: hidden;
display: flex;
flex-direction: column;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
background: rgba(255, 255, 255, 0.9); /* Slightly transparent white background */
z-index: 1; /* Ensure chat is above the drones */
}
.chat-header {
background: #ffffff;
text-align: center;
padding: 10px;
border-top-left-radius: 20px; /* Curved corners */
border-top-right-radius: 20px; /* Curved corners */
}
.chat-header img {
max-width: 100%;
height: auto;
}
.chat-box {
flex: 1;
padding: 20px;
overflow-y: auto;
background: hsl(182, 100%, 95%);
display: flex;
flex-direction: column;
gap: 10px; /* Space between messages */
}
.input-box {
display: flex;
border-top: 1px solid #ccc;
border-bottom-left-radius: 20px; /* Curved corners */
border-bottom-right-radius: 20px; /* Curved corners */
}
.input-box input {
flex: 1;
padding: 10px;
border: none;
outline: none;
height: 50px;
border-top-left-radius: 20px; /* Curved corners */
border-bottom-left-radius: 20px; /* Curved corners */
}
.input-box button {
padding: 10px;
background: #007bff;
color: #fff;
border: none;
cursor: pointer;
width: 100px; /* Adjusted width */
border-top-right-radius: 20px; /* Curved corners */
border-bottom-right-radius: 20px; /* Curved corners */
}
.input-box button:hover {
background: #0056b3;
}
.message {
padding: 10px;
border-radius: 15px; /* Curved message corners */
max-width: 75%;
word-wrap: break-word;
display: inline-block; /* Allow wrapping */
}
.message.user {
background: #007bff;
color: white;
align-self: flex-end; /* Align user messages to the right */
}
.message.bot {
background: #333; /* Darker background for better contrast */
color: #ffffff; /* Bot message color */
align-self: flex-start; /* Align bot messages to the left */
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
<img src="logo.jpeg" alt="Skytrade Logo" />
</div>
<div class="chat-box" id="chat-box">
<!-- Messages will be appended here -->
</div>
<div class="input-box">
<input type="text" id="user-input" placeholder="Type a message..." />
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
const drones = [];
function createDrone() {
const drone = document.createElement('div');
drone.classList.add('drone');
// Set random starting position
const startX = Math.random() * (window.innerWidth - 100); // Adjust for drone width
const startY = Math.random() * (window.innerHeight - 100); // Adjust for drone height
drone.style.left = `${startX}px`;
drone.style.top = `${startY}px`;
// Set random duration for flight (speed control)
const duration = Math.random() * 40 + 40; // Random duration between 40s and 80s
drone.style.animation = `fly ${duration}s linear infinite`;
document.body.appendChild(drone);
drones.push(drone);
// Animate the drone with random end position
const endX = Math.random() * (window.innerWidth - 100);
const endY = Math.random() * (window.innerHeight - 100);
drone.animate([
{ transform: `translate(0, 0)` },
{ transform: `translate(${endX - startX}px, ${endY - startY}px)` }
], {
duration: duration * 1000,
iterations: Infinity,
easing: 'linear'
});
// Ensure the drone bounces off the edges
drone.addEventListener('animationiteration', () => {
const newStartX = Math.random() * (window.innerWidth - 100);
const newStartY = Math.random() * (window.innerHeight - 100);
drone.style.left = `${newStartX}px`;
drone.style.top = `${newStartY}px`;
});
}
// Create 3 drones initially
for (let i = 0; i < 3; i++) {
createDrone();
}
async function sendMessage() {
const userInput = document.getElementById("user-input").value;
if (!userInput) return;
const chatBox = document.getElementById("chat-box");
// Display user message
const userMessage = document.createElement("div");
userMessage.classList.add("message", "user");
userMessage.innerText = userInput;
chatBox.appendChild(userMessage);
document.getElementById("user-input").value = "";
try {
// Send user message to backend
const response = await fetch("", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message: userInput }),
});
const result = await response.json();
if (response.ok) {
// Display bot response
const botMessage = document.createElement("div");
botMessage.classList.add("message", "bot");
botMessage.innerText = result.response;
chatBox.appendChild(botMessage);
} else {
console.error('Error:', result.error);
alert('Error: ' + result.error);
}
} catch (error) {
console.error('Error:', error);
alert('Error: ' + error.message);
}
// Scroll to the bottom of the chat
chatBox.scrollTop = chatBox.scrollHeight;
}
// Add event listener for 'Enter' key press
document.getElementById("user-input").addEventListener("keypress", function(event) {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent form submission
sendMessage();
}
});
</script>
</body>
</html>