teste2 / index.html
AnaDrivia's picture
Update index.html
e64ab21 verified
Raw History Blame Contribute Delete
7.11 kB
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gerador de Legendas de Imagens (BLIP)</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #fdf2f2;
margin: 0;
padding: 40px 20px;
color: #333;
}
.container {
max-width: 700px;
margin: auto;
background: #ffffff;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(183, 28, 28, 0.15);
border-top: 6px solid #b71c1c;
}
h1 {
text-align: center;
color: #b71c1c;
margin-top: 0;
}
p.description {
text-align: center;
color: #666;
margin-bottom: 25px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
font-weight: bold;
margin-bottom: 8px;
color: #b71c1c;
}
input[type="file"] {
width: 100%;
padding: 10px;
box-sizing: border-box;
border: 2px dashed #e57373;
border-radius: 6px;
background: #fff8f8;
cursor: pointer;
}
input[type="text"] {
width: 100%;
padding: 12px;
font-size: 16px;
box-sizing: border-box;
border: 1px solid #e0e0e0;
border-radius: 6px;
outline: none;
}
input[type="text"]:focus {
border-color: #b71c1c;
}
.preview-container {
text-align: center;
margin-top: 15px;
}
#preview {
max-width: 100%;
max-height: 280px;
border-radius: 8px;
display: none;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
margin-top: 10px;
}
button {
width: 100%;
margin-top: 15px;
padding: 14px;
border: none;
border-radius: 6px;
background: #b71c1c;
color: white;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #8e0000;
}
button:disabled {
background: #e0e0e0;
color: #999;
cursor: wait;
}
#status {
margin-top: 25px;
padding: 15px;
background: #ffebee;
border-radius: 8px;
text-align: center;
font-size: 16px;
color: #b71c1c;
font-weight: 500;
}
.resultado-box {
margin-top: 15px;
padding: 15px;
background: #fff8f8;
border-left: 5px solid #b71c1c;
border-radius: 6px;
}
.resultado-title {
font-weight: bold;
color: #b71c1c;
margin-bottom: 5px;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.resultado-text {
font-size: 16px;
color: #222;
}
</style>
</head>
<body>
<div class="container">
<h1>📷 Gerador de Legendas (BLIP)</h1>
<p class="description">
Envie uma imagem para gerar legendas automáticas com e sem contexto inicial.
</p>
<!-- Upload da Imagem -->
<div class="form-group">
<label for="imagem">1. Selecione uma imagem:</label>
<input type="file" id="imagem" accept="image/*">
<div class="preview-container">
<img id="preview" alt="Pré-visualização da imagem">
</div>
</div>
<!-- Texto de contexto inicial -->
<div class="form-group">
<label for="prompt">2. Texto inicial (Condicional):</label>
<input type="text" id="prompt" value="a photography of a" placeholder="Ex: a photography of a">
</div>
<!-- Botão de Ação -->
<button id="botao" onclick="gerarLegendas()">Gerar legendas</button>
<!-- Status do carregamento/inferência -->
<div id="status">Carregando modelo BLIP...</div>
<!-- Resultados -->
<div id="resultados" style="display: none;">
<div class="resultado-box">
<div class="resultado-title">Resultado Condicional</div>
<div class="resultado-text" id="resCondicional">-</div>
</div>
<div class="resultado-box">
<div class="resultado-title">Resultado Incondicional</div>
<div class="resultado-text" id="resIncondicional">-</div>
</div>
</div>
</div>
<script type="module">
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.2";
let captioner = null;
const inputImagem = document.getElementById("imagem");
const imgPreview = document.getElementById("preview");
const statusDiv = document.getElementById("status");
const botao = document.getElementById("botao");
// Exibe a pré-visualização da imagem selecionada
inputImagem.addEventListener("change", (e) => {
const file = e.target.files[0];
if (file) {
const url = URL.createObjectURL(file);
imgPreview.src = url;
imgPreview.style.display = "inline-block";
} else {
imgPreview.style.display = "none";
}
});
// ======================================
// CARREGAMENTO DO MODELO
// ======================================
try {
captioner = await pipeline(
"image-to-text",
"Xenova/blip-image-captioning-base"
);
statusDiv.innerHTML = "✅ Modelo carregado. Selecione uma imagem e clique em Gerar!";
} catch (erro) {
console.error(erro);
statusDiv.innerHTML = "❌ Erro ao carregar o modelo BLIP.";
}
// ======================================
// FUNÇÃO DE GERAÇÃO DE LEGENDAS
// ======================================
window.gerarLegendas = async function () {
if (!captioner) {
alert("O modelo ainda está sendo carregado. Por favor, aguarde.");
return;
}
const file = inputImagem.files[0];
if (!file) {
alert("Por favor, envie uma imagem antes de gerar.");
return;
}
const promptText = document.getElementById("prompt").value.trim();
const imageUrl = imgPreview.src;
botao.disabled = true;
statusDiv.innerHTML = "⏳ Gerando legendas...";
try {
// 1. Legenda Condicional
const resCond = await captioner(imageUrl, {
prompt: promptText,
max_new_tokens: 50
});
// 2. Legenda Incondicional
const resUncond = await captioner(imageUrl, {
max_new_tokens: 50
});
// Extrai as legendas geradas
const condicionalText = resCond[0]?.generated_text || "Não foi possível gerar.";
const incondicionalText = resUncond[0]?.generated_text || "Não foi possível gerar.";
document.getElementById("resCondicional").innerText = condicionalText;
document.getElementById("resIncondicional").innerText = incondicionalText;
document.getElementById("resultados").style.display = "block";
statusDiv.innerHTML = "✨ Legendas geradas com sucesso!";
} catch (erro) {
console.error(erro);
statusDiv.innerHTML = "❌ Erro ao processar a imagem.";
} finally {
botao.disabled = false;
}
};
</script>
</body>
</html>