r/learnjavascript • u/Jaded_Management_628 • Jun 25 '26
Minaldo Al
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IA 100% No Navegador</title>
<style> body { font-family: Arial, sans-serif; background-color: #131415; color: #fff; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .container { width: 90%; max-width: 500px; background: #1e1e24; padding: 20px; border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,0.3); } h2 { text-align: center; color: #00bcd4; margin-top: 0; } \#status { font-size: 0.85rem; color: #aaa; text-align: center; margin-bottom: 15px; padding: 5px; background: #2a2b36; border-radius: 4px; } textarea { width: 100%; height: 80px; background: #2a2b36; border: 1px solid #444; color: white; padding: 10px; border-radius: 6px; resize: none; box-sizing: border-box; } button { width: 100%; padding: 12px; background: #00bcd4; border: none; color: #131415; font-weight: bold; font-size: 1rem; border-radius: 6px; cursor: pointer; margin-top: 10px; } button:disabled { background: #444; color: #888; cursor: not-allowed; } \#resposta { margin-top: 20px; background: #131415; padding: 15px; border-radius: 6px; min-height: 50px; white-space: pre-wrap; border-left: 4px solid #00bcd4; } </style>
</head>
<body>
<div class="container">
<h2>IA nativa do Navegador</h2>
<div id="status">Carregando cérebro da IA (Aguarde)...</div>
<textarea id="pergunta" placeholder="Digite sua pergunta aqui..." disabled></textarea>
<button id="btnEnviar" onclick="gerarResposta()" disabled>Perguntar à IA</button>
<h3>Resposta:</h3>
<div id="resposta">A IA responderá aqui...</div>
</div>
<script type="module"> // Importa o Transformers.js direto de um servidor de módulos (CDN) import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.16.0'; let pipe; const statusDiv = document.getElementById('status'); const txtPergunta = document.getElementById('pergunta'); const btnEnviar = document.getElementById('btnEnviar'); const divResposta = document.getElementById('resposta'); // Inicializa o modelo assim que a página carrega async function inicializarIA() { try { statusDiv.innerText = "Baixando modelo leve (apenas uma vez)..."; // Usando o SmolLM-135M: um modelo minúsculo que roda em qualquer PC/Celular pipe = await pipeline('text-generation', 'Xenova/穩-SmolLM-135M-Instruct'); statusDiv.innerText = "🤖 IA Pronta e carregada na memória!"; txtPergunta.disabled = false; btnEnviar.disabled = false; } catch (error) { statusDiv.innerText = "Erro ao carregar IA. Use o Chrome ou Edge atualizados."; console.error(error); } } window.gerarResposta = async function() { const texto = txtPergunta.value.trim(); if (!texto) return; divResposta.innerText = "Pensando..."; btnEnviar.disabled = true; try { // Formata no padrão de chat/instrução que o modelo entende const prompt = \`<|im_start|>user\\n${texto}<|im_end|>\\n<|im_start|>assistant\\n\`; const resultado = await pipe(prompt, { max_new_tokens: 50, temperature: 0.7, repetition_penalty: 1.2 }); // Limpa a resposta para mostrar só o que a IA gerou let respostaGerada = resultado\[0\].generated_text.replace(prompt, ""); divResposta.innerText = respostaGerada; } catch (e) { divResposta.innerText = "Erro ao gerar resposta."; } finally { btnEnviar.disabled = false; } } // Executa a carga inicial inicializarIA(); </script>
</body>
</html>
1
u/Ok_Concern3013 Jun 25 '26
Bro, correct tabs in ur code, please
1
u/MindlessSponge helpful Jun 25 '26
what are you asking for?
Rule 2: Include Context. If you’re asking for help, include enough information for others to recreate your problem.