-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathama.php
322 lines (272 loc) · 8.99 KB
/
ama.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
// Handle POST requests
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Decode JSON data from the request body
$data = json_decode(file_get_contents("php://input"));
if (isset($data->userInput)) {
// Handle user input scenario
// Sanitize user input (optional)
$userInput = htmlspecialchars(strip_tags($data->userInput));
// Execute ama.py script with user input
$escapedInput = escapeshellarg($userInput);
$output = shell_exec('python3 ama.py ' . $escapedInput);
// Check if output is valid JSON
$response = json_decode($output);
if (json_last_error() !== JSON_ERROR_NONE) {
echo json_encode(array("answer" => "Sorry, I don't understand that question."));
exit;
}
// Prepare JSON response for user input
$response_data = array(
"answer" => $response->answer
);
// Send JSON response
echo json_encode($response_data);
exit;
} else {
// Handle invalid request
echo json_encode(array("error" => "Sorry, I don't understand that question."));
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ask Me Anything</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
/* CSS styles for the virtual assistant widget */
.widget-container {
display: none;
position: fixed;
bottom: 20px;
right: 20px;
width: 350px; /* Increased width for the widget container */
background-color: #f0f0f0;
border: 1px solid #ccc;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
font-family: Arial, sans-serif;
border-radius: 8px;
overflow: hidden;
z-index: 1000;
}
.widget-header {
background-color: #4CAF50;
color: white;
padding: 10px;
text-align: center;
font-weight: bold;
}
.widget-body {
padding: 10px;
width: 95%; /* Increased width of the widget body */
max-width: 100%; /* Ensure it doesn't exceed 100% */
margin: 0 auto;
}
.chat-messages {
list-style-type: none;
padding: 0;
margin: 0;
max-height: 300px;
overflow-y: scroll;
color: black;
}
.user-message {
text-align: right;
margin-bottom: 10px;
}
.assistant-message {
text-align: left;
margin-bottom: 10px;
}
.message {
background-color: #e5e5ea;
padding: 8px;
border-radius: 5px;
display: inline-block;
max-width: 100%;
}
.user-message .message {
background-color: #4CAF50;
color: white;
align-self: flex-end;
max-width: 100%; /* Adjust the maximum width of user messages */
}
.assistant-message .message {
background-color: #f0f0f0;
color: #333;
align-self: flex-start;
max-width: 100%; /* Adjust the maximum width of assistant messages */
}
.input-container {
display: flex;
margin-top: 10px;
}
.input-container input[type="text"] {
flex: 1;
padding: 8px;
border: 1px solid #ccc;
border-radius: 5px 0 0 5px;
font-size: 14px;
color: black;
max-width: calc(100% - 70px); /* Adjust the input width to leave space for the button */
}
.input-container button {
padding: 8px 12px;
background-color: #4CAF50;
border: none;
color: white;
border-radius: 0 5px 5px 0;
cursor: pointer;
font-size: 14px;
max-width: 70px; /* Adjust the button width */
}
.widget-icon {
position: fixed;
bottom: 20px;
right: 20px;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #4CAF50;
color: white;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
cursor: pointer;
z-index: 1000;
}
.close-button {
position: absolute;
top: 10px;
right: 10px;
cursor: pointer;
color: #666;
}
/* Typing animation */
@keyframes typing {
from { width: 0 }
}
.typing-indicator {
overflow: hidden;
white-space: nowrap;
animation: typing 2s steps(20, end);
}
.typing-indicator::after {
content: '|';
overflow: hidden;
display: inline-block;
vertical-align: bottom;
animation: caret 1s steps(1, end) infinite;
}
@keyframes caret {
50% { border-color: transparent }
}
/* Blue spinner */
.blue-spinner {
border: 2px solid #f3f3f3;
border-radius: 50%;
border-top: 2px solid #3498db;
width: 20px;
height: 20px;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="widget-icon" onclick="toggleWidget()">💬</div>
<div class="widget-container" id="widget-container" aria-hidden="true" aria-labelledby="widget-header">
<div class="widget-header" id="widget-header">
AMA
<span class="close-button" onclick="toggleWidget()" aria-label="Close">×</span>
</div>
<div class="widget-body">
<ul class="chat-messages" id="chat-messages" role="log" aria-live="polite">
<li class="assistant-message">
<div class="message" id="greetingMessage"></div>
</li>
</ul>
<div class="input-container">
<input type="text" id="user-input" placeholder="Type your message..." onkeypress="handleKeyPress(event)">
<button onclick="sendMessage()" aria-label="Send">Send</button>
</div>
</div>
</div>
<script>
function toggleWidget() {
const widgetContainer = document.getElementById('widget-container');
const isVisible = widgetContainer.style.display !== 'none';
widgetContainer.style.display = isVisible ? 'none' : 'block';
document.getElementById('user-input').focus(); // Focus on input field when widget opens
}
function sendMessage() {
const userInput = document.getElementById('user-input').value.trim();
if (userInput === '') return; // If input is empty, do nothing
const chatMessages = document.getElementById('chat-messages');
const userMessage = document.createElement('li');
userMessage.className = 'user-message';
userMessage.innerHTML = `<div class="message">${userInput}</div>`;
chatMessages.appendChild(userMessage);
// Clear input after sending message
document.getElementById('user-input').value = '';
// Simulate typing effect
const typingIndicator = document.createElement('li');
typingIndicator.className = 'assistant-message';
typingIndicator.innerHTML = `<div class="blue-spinner"></div>`;
chatMessages.appendChild(typingIndicator);
// Scroll to the bottom of the chat messages
chatMessages.scrollTop = chatMessages.scrollHeight;
// Send user input to server via AJAX
fetch('ama.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ userInput: userInput }),
})
.then(response => response.json())
.then(data => {
// Simulate typing response
const responseText = data.answer;
typingIndicator.innerHTML = ''; // Clear typing indicator
for (let i = 0; i < responseText.length; i++) {
setTimeout(() => {
typingIndicator.innerHTML += responseText.charAt(i);
chatMessages.scrollTop = chatMessages.scrollHeight; // Scroll to bottom after each character
}, 50 * i); // Adjust typing speed (50 milliseconds per character here)
}
})
.catch(error => {
console.error('Error:', error);
});
}
function handleKeyPress(event) {
if (event.key === 'Enter') {
sendMessage();
}
}
// Get the current hour of the day (0-23)
var currentHour = new Date().getHours();
// Define greetings based on the time of day
var greeting;
if (currentHour >= 0 && currentHour < 12) {
greeting = "Good morning!";
} else if (currentHour >= 12 && currentHour < 16) {
greeting = "Good afternoon!";
} else {
greeting = "Good evening!";
}
// Update the greeting message in the HTML
var greetingMessage = document.getElementById('greetingMessage');
greetingMessage.textContent = greeting + " I am AMA your virtual assistant. How can I help you today?";
</script>
</body>
</html>