Repository navigation
Expand file tree
/
Copy pathvingadevs-cli.js
More file actions
209 lines (179 loc) · 5.75 KB
/
Copy pathvingadevs-cli.js
File metadata and controls
209 lines (179 loc) · 5.75 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env node
import 'dotenv/config';
import chalk from 'chalk';
import readline from 'readline';
// ========================================
// CONFIGURAÇÕES E CONSTANTES - VINGADEVS
// ========================================
const CONFIG = {
API_URL: 'https://api-inference.huggingface.co/models/nlptown/bert-base-multilingual-uncased-sentiment',
API_KEY: process.env.HUGGINGFACE_API_KEY
};
const EMOJIS_SENTIMENTO = {
'Muito Negativo': '😡',
'Negativo': '😠',
'Neutro': '😐',
'Positivo': '😄',
'Muito Positivo': '🤩',
'Indefinido': '❓'
};
const MESSAGES = {
WELCOME: '🤖 VingaDevs - Análise de Sentimento com IA',
SUBTITLE: 'Bem-vindo ao nosso analisador inteligente!',
INSTRUCTIONS: 'Digite "sair" para encerrar',
ANALYZING: 'Analisando sentimento com IA... 🧠',
RESULT: 'Resultado da Análise:',
ERROR: 'Erro ao analisar sentimento:',
GOODBYE: 'Obrigado por usar o VingaDevs CLI! Até logo! 👋',
PROMPT: 'Digite o texto para análise: ',
EMPTY_TEXT: 'Você precisa digitar um texto.',
NO_API_KEY: 'ERRO: Chave da API do Hugging Face não encontrada. Configure HUGGINGFACE_API_KEY no arquivo .env'
};
// ========================================
// FUNÇÕES DE ANÁLISE (API HUGGING FACE)
// ========================================
/**
* Mapeia os labels da API para sentimentos em português
* @param {string} label - Label retornado pela API (ex: "1 star", "5 stars")
* @returns {string} - Sentimento em português
*/
function mapearSentimento(label) {
const mapeamento = {
'1 star': 'Muito Negativo',
'2 stars': 'Negativo',
'3 stars': 'Neutro',
'4 stars': 'Positivo',
'5 stars': 'Muito Positivo'
};
return mapeamento[label] || 'Indefinido';
}
/**
* Faz requisição para a API do Hugging Face
* @param {string} texto - Texto a ser analisado
* @returns {Promise<Object>} - Resposta da API
*/
async function chamarApiHuggingFace(texto) {
const response = await fetch(CONFIG.API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${CONFIG.API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ inputs: texto })
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(`API Error: ${errorData.error || response.statusText}`);
}
return await response.json();
}
/**
* Analisa o sentimento de um texto usando IA
* @param {string} texto - Texto a ser analisado
* @returns {Promise<string>} - Sentimento identificado
*/
async function analisarSentimento(texto) {
if (!CONFIG.API_KEY) {
throw new Error(MESSAGES.NO_API_KEY);
}
if (!texto || !texto.trim()) {
throw new Error('Texto não pode estar vazio');
}
try {
const data = await chamarApiHuggingFace(texto);
const resultado = data[0][0]; // Pega o resultado com maior confiança
return mapearSentimento(resultado.label);
} catch (error) {
throw new Error(`${MESSAGES.ERROR} ${error.message}`);
}
}
// ========================================
// FUNÇÕES DE INTERFACE - VINGADEVS UX
// ========================================
/**
* Exibe o cabeçalho de boas-vindas da VingaDevs
*/
function exibirBoasVindas() {
console.log('\n' + '='.repeat(60));
console.log(chalk.cyan.bold(MESSAGES.WELCOME));
console.log(chalk.gray(MESSAGES.SUBTITLE));
console.log(chalk.yellow(MESSAGES.INSTRUCTIONS));
console.log('='.repeat(60) + '\n');
}
/**
* Exibe o resultado da análise de sentimento
* @param {string} sentimento - Sentimento identificado
* @param {string} textoOriginal - Texto que foi analisado
*/
function exibirResultado(sentimento, textoOriginal) {
console.log('\n' + '='.repeat(50));
console.log(chalk.cyan('📝 Texto analisado:'), chalk.white(`"${textoOriginal}"`));
console.log(chalk.green('🎯 ' + MESSAGES.RESULT), chalk.bold.yellow(sentimento));
console.log(chalk.green('😊 Emoji:'), EMOJIS_SENTIMENTO[sentimento]);
console.log('='.repeat(50) + '\n');
}
/**
* Trata e exibe erros de forma amigável
* @param {Error} error - Erro a ser tratado
*/
function exibirErro(error) {
console.log('\n' + '❌'.repeat(20));
console.error(chalk.red('💥 ' + error.message));
if (error.message.includes('API_KEY')) {
console.log(chalk.yellow('\n💡 Dica VingaDevs: Crie um arquivo .env com:'));
console.log(chalk.gray('HUGGINGFACE_API_KEY=seu_token_aqui'));
console.log(chalk.gray('Obtenha o token em: https://huggingface.co/settings/tokens'));
}
console.log('❌'.repeat(20) + '\n');
}
// ========================================
// FUNÇÃO PRINCIPAL - MODO INTERATIVO
// ========================================
/**
* Executa o modo interativo VingaDevs
*/
async function executarModoInterativo() {
// Verificar API key antes de iniciar
if (!CONFIG.API_KEY) {
exibirErro(new Error(MESSAGES.NO_API_KEY));
process.exit(1);
}
exibirBoasVindas();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const perguntarTexto = () => {
rl.question(chalk.blue('💬 ' + MESSAGES.PROMPT), async (texto) => {
// Verificar se o usuário quer sair
if (texto.toLowerCase() === 'sair') {
console.log(chalk.green('\n' + MESSAGES.GOODBYE));
rl.close();
return;
}
// Verificar se o texto não está vazio
if (!texto.trim()) {
console.log(chalk.red('⚠️ ' + MESSAGES.EMPTY_TEXT));
perguntarTexto();
return;
}
try {
console.log(chalk.yellow('🔄 ' + MESSAGES.ANALYZING));
const sentimento = await analisarSentimento(texto);
exibirResultado(sentimento, texto);
} catch (error) {
exibirErro(error);
}
perguntarTexto();
});
};
perguntarTexto();
}
// ========================================
// PONTO DE ENTRADA
// ========================================
// Iniciar o modo interativo VingaDevs
executarModoInterativo().catch((error) => {
exibirErro(error);
process.exit(1);
});