File size: 2,573 Bytes
bec5145
 
 
 
 
 
70277bf
 
bec5145
 
 
70277bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bec5145
 
70277bf
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
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Transformers.js - Named Entity Recognition</title>
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <h1>Named Entity Recognition (NER) w/ 🤗 Transformers.js</h1>

    <!-- Seção de reconhecimento de entidades nomeadas (NER) -->
    <section>
        <h2>Named Entity Recognition (NER)</h2>
        <textarea id="text-input" rows="6" placeholder="Digite ou cole o texto aqui..."></textarea>
        <button id="analyze-text">Analisar Texto</button>
        <div id="text-output"></div>
    </section>

    <script type="module">
        import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';

        // Configuração para desativar modelos locais
        env.allowLocalModels = false;

        // Reconhecimento de Entidades Nomeadas (NER)
        const nerStatus = document.getElementById('status');
        const textInput = document.getElementById('text-input');
        const analyzeTextButton = document.getElementById('analyze-text');
        const textOutput = document.getElementById('text-output');

        nerStatus.textContent = 'Carregando modelo de NER...';
        const nerModel = await pipeline('ner', 'Xenova/distilbert-base-multilingual-cased-ner-hrl');
        nerStatus.textContent = 'Modelo de NER pronto!';

        analyzeTextButton.addEventListener('click', async () => {
            const inputText = textInput.value.trim();
            if (!inputText) {
                textOutput.textContent = 'Por favor, insira um texto para análise.';
                return;
            }

            textOutput.textContent = 'Analisando...';
            const nerOutput = await nerModel(inputText);
            renderEntities(nerOutput);
        });

        function renderEntities(entities) {
            textOutput.innerHTML = '';
            entities.forEach(entity => {
                const { word, entity_group, score } = entity;

                const entityElement = document.createElement('div');
                entityElement.className = 'entity';
                entityElement.innerHTML = `
                    <strong>Palavra:</strong> ${word} <br>
                    <strong>Entidade:</strong> ${entity_group} <br>
                    <strong>Confiança:</strong> ${(score * 100).toFixed(2)}%
                `;
                textOutput.appendChild(entityElement);
            });
        }
    </script>
</body>

</html>