Update index.js
Browse files
index.js
CHANGED
|
@@ -3,77 +3,44 @@ import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers
|
|
| 3 |
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
|
| 4 |
env.allowLocalModels = false;
|
| 5 |
|
| 6 |
-
//
|
| 7 |
-
const
|
| 8 |
-
const
|
| 9 |
-
const
|
| 10 |
-
const
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
detect(EXAMPLE_URL);
|
| 22 |
-
});
|
| 23 |
-
|
| 24 |
-
fileUpload.addEventListener('change', function (e) {
|
| 25 |
-
const file = e.target.files[0];
|
| 26 |
-
if (!file) {
|
| 27 |
return;
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
// Set up a callback when the file is loaded
|
| 33 |
-
reader.onload = e2 => detect(e2.target.result);
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
});
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
});
|
| 49 |
-
status.textContent = '';
|
| 50 |
-
output.forEach(renderBox);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
// Render a bounding box and label on the image
|
| 54 |
-
function renderBox({ box, label }) {
|
| 55 |
-
const { xmax, xmin, ymax, ymin } = box;
|
| 56 |
-
|
| 57 |
-
// Generate a random color for the box
|
| 58 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 59 |
-
|
| 60 |
-
// Draw the box
|
| 61 |
-
const boxElement = document.createElement('div');
|
| 62 |
-
boxElement.className = 'bounding-box';
|
| 63 |
-
Object.assign(boxElement.style, {
|
| 64 |
-
borderColor: color,
|
| 65 |
-
left: 100 * xmin + '%',
|
| 66 |
-
top: 100 * ymin + '%',
|
| 67 |
-
width: 100 * (xmax - xmin) + '%',
|
| 68 |
-
height: 100 * (ymax - ymin) + '%',
|
| 69 |
-
})
|
| 70 |
-
|
| 71 |
-
// Draw label
|
| 72 |
-
const labelElement = document.createElement('span');
|
| 73 |
-
labelElement.textContent = label;
|
| 74 |
-
labelElement.className = 'bounding-box-label';
|
| 75 |
-
labelElement.style.backgroundColor = color;
|
| 76 |
-
|
| 77 |
-
boxElement.appendChild(labelElement);
|
| 78 |
-
imageContainer.appendChild(boxElement);
|
| 79 |
}
|
|
|
|
| 3 |
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
|
| 4 |
env.allowLocalModels = false;
|
| 5 |
|
| 6 |
+
// Configuração do modelo de NER
|
| 7 |
+
const nerStatus = document.getElementById('status');
|
| 8 |
+
const textInput = document.getElementById('text-input');
|
| 9 |
+
const analyzeTextButton = document.getElementById('analyze-text');
|
| 10 |
+
const textOutput = document.getElementById('text-output');
|
| 11 |
+
|
| 12 |
+
nerStatus.textContent = 'Carregando modelo de NER...';
|
| 13 |
+
const nerModel = await pipeline('ner', 'Xenova/distilbert-base-multilingual-cased-ner-hrl');
|
| 14 |
+
nerStatus.textContent = 'Modelo de NER pronto!';
|
| 15 |
+
|
| 16 |
+
// Função para análise de texto
|
| 17 |
+
analyzeTextButton.addEventListener('click', async () => {
|
| 18 |
+
const inputText = textInput.value.trim();
|
| 19 |
+
if (!inputText) {
|
| 20 |
+
textOutput.textContent = 'Por favor, insira um texto para análise.';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return;
|
| 22 |
}
|
| 23 |
|
| 24 |
+
textOutput.textContent = 'Analisando...';
|
| 25 |
+
const nerOutput = await nerModel(inputText);
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
// Renderizando as entidades detectadas
|
| 28 |
+
renderEntities(nerOutput);
|
| 29 |
});
|
| 30 |
|
| 31 |
+
// Função para exibir os resultados das entidades detectadas
|
| 32 |
+
function renderEntities(entities) {
|
| 33 |
+
textOutput.innerHTML = '';
|
| 34 |
+
entities.forEach(entity => {
|
| 35 |
+
const { word, entity_group, score } = entity;
|
| 36 |
+
|
| 37 |
+
const entityElement = document.createElement('div');
|
| 38 |
+
entityElement.className = 'entity';
|
| 39 |
+
entityElement.innerHTML = `
|
| 40 |
+
<strong>Palavra:</strong> ${word} <br>
|
| 41 |
+
<strong>Entidade:</strong> ${entity_group} <br>
|
| 42 |
+
<strong>Confiança:</strong> ${(score * 100).toFixed(2)}%
|
| 43 |
+
`;
|
| 44 |
+
textOutput.appendChild(entityElement);
|
| 45 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
}
|