Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,30 @@
|
|
1 |
-
import
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
"text-classification",
|
7 |
-
model="finiteautomata/beto-emotion-analysis",
|
8 |
-
tokenizer="finiteautomata/beto-emotion-analysis",
|
9 |
-
return_all_scores=True
|
10 |
-
)
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
'others': 'otras',
|
15 |
-
'joy': 'alegr铆a',
|
16 |
-
'sadness': 'tristeza',
|
17 |
-
'anger': 'ira',
|
18 |
-
'surprise': 'sorpresa',
|
19 |
-
'disgust': 'asco',
|
20 |
-
'fear': 'miedo'
|
21 |
-
}
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
# Lanzar la interfaz
|
34 |
-
iface.launch()
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Inicializamos Flask y el pipeline de Hugging Face
|
6 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Cargar el modelo de Hugging Face para clasificaci贸n de emociones
|
9 |
+
emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
@app.route("/predict", methods=["POST"])
|
12 |
+
def predict_emotion():
|
13 |
+
try:
|
14 |
+
# Obtener el texto del cuerpo de la solicitud
|
15 |
+
data = request.get_json()
|
16 |
+
text = data["text"]
|
17 |
+
|
18 |
+
# Hacer la predicci贸n
|
19 |
+
result = emotion_classifier(text)
|
20 |
+
|
21 |
+
# Retornar la emoci贸n predicha
|
22 |
+
return jsonify({"emotion": result[0]['label'], "confidence": result[0]['score']})
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return jsonify({"error": str(e)}), 400
|
26 |
|
27 |
+
if __name__ == "__main__":
|
28 |
+
# Iniciar la aplicaci贸n Flask
|
29 |
+
app.run(host="0.0.0.0", port=5000)
|
30 |
|
|
|
|