-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (41 loc) · 1.66 KB
/
main.py
File metadata and controls
52 lines (41 loc) · 1.66 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
from flask import Flask, jsonify, request
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from deep_translator import GoogleTranslator
from dotenv import load_dotenv
import os
app = Flask(__name__)
# Cargar variables de entorno desde .env
load_dotenv()
# Inicializar el analizador de sentimientos
analyzer = SentimentIntensityAnalyzer()
# Obtener el token de autenticación desde .env
AUTH_TOKEN = os.getenv('AUTH_TOKEN')
# Ruta para la raíz
@app.route('/')
def home():
return "Bienvenido al análisis de sentimientos. Usa GET /sentiment/<texto> para analizar el estado de ánimo."
# Ruta para analizar el estado de ánimo con traducción al inglés y autenticación
@app.route('/sentiment/<text>')
def sentiment(text):
# Obtener el token de la solicitud
token = request.headers.get('Authorization')
# Verificar si el token es correcto
if token != f"Bearer {AUTH_TOKEN}":
return jsonify({'error': 'No autorizado. Token inválido.'}), 401
try:
# Traducir el texto de cualquier idioma a inglés usando GoogleTranslator
translation = GoogleTranslator(source='auto', target='en').translate(text)
# Analizar el estado de ánimo en inglés
sentiment_score = analyzer.polarity_scores(translation)
return jsonify({
'original_text': text,
'translated_text': translation,
'sentiment': sentiment_score
})
except Exception as e:
return jsonify({
'error': str(e),
'message': 'Hubo un problema con la traducción o el análisis de sentimientos.'
}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3000)