File size: 12,907 Bytes
079552f |
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 |
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat com Gemini AI</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.chat-message.user {
background-color: #3b82f6;
color: white;
border-radius: 18px 18px 0 18px;
}
.chat-message.ai {
background-color: #f3f4f6;
color: #1f2937;
border-radius: 18px 18px 18px 0;
}
.typing-indicator span {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
background-color: #9ca3af;
margin: 0 2px;
animation: bounce 1.5s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) {
animation-delay: 0.2s;
}
.typing-indicator span:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes bounce {
0%,
100% {
transform: translateY(0);
}
50% {
transform: translateY(-5px);
}
}
#chat-container {
height: calc(100vh - 140px);
}
@media (max-width: 640px) {
#chat-container {
height: calc(100vh - 120px);
}
}
</style>
</head>
<body class="bg-gray-100 font-sans">
<div class="container mx-auto max-w-4xl px-4 py-6">
<!-- Cabeçalho -->
<header class="flex items-center justify-between mb-6">
<div class="flex items-center space-x-3">
<div
class="w-10 h-10 rounded-full bg-gradient-to-r from-blue-500 to-purple-600 flex items-center justify-center">
<i class="fas fa-robot text-white"></i>
</div>
<h1 class="text-2xl font-bold text-gray-800">Chat com Gemini AI</h1>
</div>
<div class="relative">
<button id="api-key-btn"
class="px-4 py-2 bg-white border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 transition">
<i class="fas fa-key mr-2"></i>Chave API
</button>
<div id="api-key-modal"
class="hidden absolute right-0 mt-2 w-80 bg-white rounded-lg shadow-xl z-10 p-4 border border-gray-200">
<h3 class="font-medium text-gray-900 mb-2">Insira sua Chave da API Gemini</h3>
<input type="password" id="api-key-input" placeholder="Sua chave API"
class="w-full px-3 py-2 border border-gray-300 rounded-md mb-3">
<div class="flex justify-end space-x-2">
<button id="cancel-api-key"
class="px-3 py-1 text-gray-600 hover:text-gray-800">Cancelar</button>
<button id="save-api-key"
class="px-3 py-1 bg-blue-600 text-white rounded-md hover:bg-blue-700">Salvar</button>
</div>
</div>
</div>
</header>
<!-- Container do Chat -->
<div id="chat-container" class="bg-white rounded-xl shadow-md overflow-hidden flex flex-col">
<!-- Mensagens -->
<div id="chat-messages" class="flex-1 overflow-y-auto p-4 space-y-4">
<div class="chat-message ai max-w-[80%] p-4">
<div class="font-medium flex items-center mb-1">
<div
class="w-6 h-6 rounded-full bg-gradient-to-r from-purple-500 to-pink-500 flex items-center justify-center mr-2">
<i class="fas fa-robot text-white text-xs"></i>
</div>
<span>Gemini AI</span>
</div>
<p>Olá! Eu sou o Gemini, seu assistente de IA. Como posso te ajudar hoje?</p>
</div>
</div>
<!-- Área de Entrada -->
<div class="border-t border-gray-200 p-4 bg-gray-50">
<form id="message-form" class="flex space-x-2">
<input type="text" id="message-input" placeholder="Digite sua mensagem..."
class="flex-1 px-4 py-3 border border-gray-300 rounded-full focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
autocomplete="off">
<button type="submit"
class="w-12 h-12 rounded-full bg-blue-600 text-white flex items-center justify-center hover:bg-blue-700 transition">
<i class="fas fa-paper-plane"></i>
</button>
</form>
<div class="text-xs text-gray-500 mt-2 px-2">
O Gemini pode exibir informações imprecisas, inclusive sobre pessoas, então verifique suas
respostas.
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Elementos DOM
const chatMessages = document.getElementById('chat-messages');
const messageForm = document.getElementById('message-form');
const messageInput = document.getElementById('message-input');
const apiKeyBtn = document.getElementById('api-key-btn');
const apiKeyModal = document.getElementById('api-key-modal');
const apiKeyInput = document.getElementById('api-key-input');
const saveApiKeyBtn = document.getElementById('save-api-key');
const cancelApiKeyBtn = document.getElementById('cancel-api-key');
// Estado
let apiKey = localStorage.getItem('gemini-api-key') || '';
let isWaitingForResponse = false;
// Inicialização
if (apiKey) {
apiKeyInput.value = apiKey;
}
// Event Listeners
apiKeyBtn.addEventListener('click', toggleApiKeyModal);
saveApiKeyBtn.addEventListener('click', saveApiKey);
cancelApiKeyBtn.addEventListener('click', toggleApiKeyModal);
messageForm.addEventListener('submit', handleSubmit);
// Clicar fora do modal para fechar
document.addEventListener('click', function (e) {
if (!apiKeyModal.contains(e.target) && e.target !== apiKeyBtn) {
apiKeyModal.classList.add('hidden');
}
});
// Funções
function toggleApiKeyModal() {
apiKeyModal.classList.toggle('hidden');
}
function saveApiKey() {
apiKey = apiKeyInput.value.trim();
localStorage.setItem('gemini-api-key', apiKey);
toggleApiKeyModal();
addSystemMessage(apiKey ? 'Chave API salva com sucesso!' : 'Chave API removida.');
}
function addSystemMessage(text) {
const messageDiv = document.createElement('div');
messageDiv.className = 'text-center text-sm text-gray-500 my-2';
messageDiv.textContent = text;
chatMessages.appendChild(messageDiv);
scrollToBottom();
}
async function handleSubmit(e) {
e.preventDefault();
const message = messageInput.value.trim();
if (!message) return;
if (!apiKey) {
addSystemMessage('Por favor, configure sua chave da API Gemini primeiro.');
toggleApiKeyModal();
return;
}
if (isWaitingForResponse) {
addSystemMessage('Por favor, aguarde a resposta atual ser concluída.');
return;
}
// Adiciona mensagem do usuário
addMessage(message, 'user');
messageInput.value = '';
// Mostra indicador de digitação
showTypingIndicator();
isWaitingForResponse = true;
try {
const response = await fetchGeminiResponse(message);
removeTypingIndicator();
addMessage(response, 'ai');
} catch (error) {
removeTypingIndicator();
addSystemMessage(`Erro: ${error.message}`);
} finally {
isWaitingForResponse = false;
}
}
function addMessage(text, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `chat-message ${sender} max-w-[80%] p-4`;
const senderName = sender === 'user' ? 'Você' : 'Gemini AI';
const iconClass = sender === 'user' ? 'fas fa-user' : 'fas fa-robot';
const iconBg = sender === 'user'
? 'bg-gradient-to-r from-blue-400 to-blue-600'
: 'bg-gradient-to-r from-purple-500 to-pink-500';
messageDiv.innerHTML = `
<div class="font-medium flex items-center mb-1">
<div class="w-6 h-6 rounded-full ${iconBg} flex items-center justify-center mr-2">
<i class="${iconClass} text-white text-xs"></i>
</div>
<span>${senderName}</span>
</div>
<p>${text}</p>
`;
chatMessages.appendChild(messageDiv);
scrollToBottom();
}
function showTypingIndicator() {
const typingDiv = document.createElement('div');
typingDiv.id = 'typing-indicator';
typingDiv.className = 'chat-message ai max-w-[80%] p-4';
typingDiv.innerHTML = `
<div class="font-medium flex items-center mb-1">
<div class="w-6 h-6 rounded-full bg-gradient-to-r from-purple-500 to-pink-500 flex items-center justify-center mr-2">
<i class="fas fa-robot text-white text-xs"></i>
</div>
<span>Gemini AI</span>
</div>
<div class="typing-indicator">
<span></span>
<span></span>
<span></span>
</div>
`;
chatMessages.appendChild(typingDiv);
scrollToBottom();
}
function removeTypingIndicator() {
const typingIndicator = document.getElementById('typing-indicator');
if (typingIndicator) {
typingIndicator.remove();
}
}
function scrollToBottom() {
chatMessages.scrollTop = chatMessages.scrollHeight;
}
async function fetchGeminiResponse(prompt) {
if (!apiKey) {
throw new Error('Chave API não configurada');
}
const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${apiKey}`;
const requestBody = {
contents: [{
parts: [{
text: prompt
}]
}]
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error?.message || 'Falha ao obter resposta do Gemini');
}
const data = await response.json();
return data.candidates?.[0]?.content?.parts?.[0]?.text || "Desculpe, não consegui gerar uma resposta.";
}
});
</script>
</body>
</html> |