File size: 3,519 Bytes
bec5145
 
 
 
 
 
70277bf
 
bec5145
 
 
70277bf
 
 
 
 
 
 
 
 
 
 
 
1073a7b
70277bf
 
1073a7b
70277bf
 
 
 
1073a7b
 
 
 
 
 
29ec487
1073a7b
 
 
 
 
29ec487
1073a7b
 
 
29ec487
1073a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70277bf
1073a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!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 textInput = document.getElementById('text-input');
        const analyzeTextButton = document.getElementById('analyze-text');
        const textOutput = document.getElementById('text-output');
    
        // Verifique se os elementos HTML foram encontrados
        if (!textInput || !analyzeTextButton || !textOutput) {
            console.error('Elementos HTML necessários não foram encontrados.');
        } else {
            let nerModel;
            try {
                textOutput.textContent = 'Carregando modelo de NER...';
                nerModel = await pipeline('ner', 'Xenova/distilbert-base-multilingual-cased-ner-hrl', {
                    dtype: 'q4',
                });
                textOutput.textContent = 'Modelo de NER pronto!';
            } catch (error) {
                console.error('Erro ao carregar o modelo:', error);
                textOutput.textContent = 'Erro ao carregar o modelo. Tente novamente mais tarde.';
                return; // Saia se o modelo não puder ser carregado
            }
    
            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...';
                try {
                    const nerOutput = await nerModel(inputText);
                    renderEntities(nerOutput);
                } catch (error) {
                    console.error('Erro durante a análise:', error);
                    textOutput.textContent = 'Erro durante a análise. Verifique o texto inserido.';
                }
            });
    
            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>