87 lines
2.5 KiB
PHP
87 lines
2.5 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Gemini Chat</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
margin: 40px;
|
|
}
|
|
#chat-box {
|
|
border: 1px solid #ccc;
|
|
padding: 15px;
|
|
height: 300px;
|
|
overflow-y: scroll;
|
|
margin-bottom: 10px;
|
|
}
|
|
.user, .bot {
|
|
margin-bottom: 10px;
|
|
}
|
|
.user {
|
|
text-align: right;
|
|
color: blue;
|
|
}
|
|
.bot {
|
|
text-align: left;
|
|
color: green;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<h2>Gemini Chat</h2>
|
|
<div id="chat-box"></div>
|
|
<textarea id="prompt" rows="3" cols="60" placeholder="Type your message here..."></textarea><br>
|
|
<button onclick="sendPrompt()">Send</button>
|
|
|
|
<script>
|
|
async function getGeminiResponse(prompt) {
|
|
try {
|
|
const res = await fetch('http://172.31.31.51:8000/api/chatbot/message', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ message: prompt })
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const errorText = await res.text();
|
|
console.error(`Server returned ${res.status}:`, errorText);
|
|
throw new Error(`HTTP error! Status: ${res.status}`);
|
|
}
|
|
|
|
const data = await res.json();
|
|
return data.reply || "Sorry, no response.";
|
|
} catch (e) {
|
|
console.error('Fetch error:', e);
|
|
return "Error fetching response.";
|
|
}
|
|
}
|
|
|
|
async function sendPrompt() {
|
|
const promptInput = document.getElementById('prompt');
|
|
const prompt = promptInput.value.trim();
|
|
if (!prompt) return;
|
|
|
|
appendMessage('user', prompt);
|
|
promptInput.value = '';
|
|
|
|
const reply = await getGeminiResponse(prompt);
|
|
appendMessage('bot', reply);
|
|
}
|
|
|
|
function appendMessage(sender, text) {
|
|
const chatBox = document.getElementById('chat-box');
|
|
const message = document.createElement('div');
|
|
message.className = sender;
|
|
message.textContent = text;
|
|
chatBox.appendChild(message);
|
|
chatBox.scrollTop = chatBox.scrollHeight;
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|