File size: 5,108 Bytes
c3560a4 | 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 | <!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tradutor IA com T5 (JS)</title>
<style>
:root {
--bg-color: #f4f6f9;
--card-bg: #ffffff;
--primary: #2563eb;
--primary-hover: #1d4ed8;
--text: #1f2937;
--border: #e5e7eb;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--bg-color);
color: var(--text);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
}
.container {
background-color: var(--card-bg);
border-radius: 12px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
width: 100%;
max-width: 650px;
padding: 30px;
box-sizing: border-box;
}
h1 {
margin-top: 0;
font-size: 1.5rem;
text-align: center;
}
p.description {
text-align: center;
color: #6b7280;
font-size: 0.9rem;
margin-bottom: 24px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
font-weight: 600;
margin-bottom: 8px;
font-size: 0.9rem;
}
textarea {
width: 100%;
height: 120px;
padding: 12px;
border: 1px solid var(--border);
border-radius: 8px;
resize: vertical;
font-family: inherit;
font-size: 1rem;
box-sizing: border-box;
}
textarea:focus {
outline: none;
border-color: var(--primary);
}
button {
width: 100%;
background-color: var(--primary);
color: white;
border: none;
padding: 12px;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: var(--primary-hover);
}
button:disabled {
background-color: #9ca3af;
cursor: not-allowed;
}
.status {
margin-top: 15px;
font-size: 0.85rem;
color: #4b5563;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>Tradutor IA com T5</h1>
<p class="description">Insira uma frase em inglês para gerar a tradução em alemão rodando direto no seu navegador.</p>
<div class="form-group">
<label for="inputText">Inglês:</label>
<textarea id="inputText" placeholder="Digite o texto em inglês aqui..."></textarea>
</div>
<button id="translateBtn">Carregando Modelo...</button>
<div class="form-group" style="margin-top: 20px;">
<label for="outputText">Tradução em Alemão:</label>
<textarea id="outputText" readonly placeholder="A tradução aparecerá aqui..."></textarea>
</div>
<div id="status" class="status">Baixando bibliotecas...</div>
</div>
<script type="module">
// Importa a biblioteca Transformers.js via CDN
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2';
const inputText = document.getElementById('inputText');
const outputText = document.getElementById('outputText');
const translateBtn = document.getElementById('translateBtn');
const statusDiv = document.getElementById('status');
let generator = null;
// Inicializa o modelo no navegador
async function initModel() {
try {
statusDiv.innerText = "Carregando modelo T5 na memória do navegador (isso pode demorar no primeiro acesso)...";
translateBtn.disabled = true;
// Carrega o pipeline text2text-generation usando o modelo ONNX
generator = await pipeline('text2text-generation', 'Xenova/t5-base');
statusDiv.innerText = "Modelo pronto!";
translateBtn.innerText = "Traduzir";
translateBtn.disabled = false;
} catch (error) {
statusDiv.innerText = "Erro ao carregar o modelo: " + error.message;
console.error(error);
}
}
// Função para traduzir o texto
async function traduzirTexto() {
const text = inputText.value.trim();
if (!text) return;
translateBtn.disabled = true;
translateBtn.innerText = "Traduzindo...";
statusDiv.innerText = "Processando entrada...";
try {
const prompt = `translate English to German: ${text}`;
const output = await generator(prompt, { max_new_tokens: 512 });
outputText.value = output[0].generated_text;
statusDiv.innerText = "Tradução concluída com sucesso!";
} catch (error) {
statusDiv.innerText = "Erro durante a tradução: " + error.message;
console.error(error);
} finally {
translateBtn.disabled = false;
translateBtn.innerText = "Traduzir";
}
}
// Event Listeners
translateBtn.addEventListener('click', traduzirTexto);
// Inicia o carregamento do modelo ao abrir a página
initModel();
</script>
</body>
</html> |