Alexa17 commited on
Commit
feabdbe
verified
1 Parent(s): 18d7cad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -1,34 +1,30 @@
1
- import gradio as gr
 
2
  from transformers import pipeline
3
 
4
- # Cargamos el modelo de clasificaci贸n de emociones
5
- emotion_classifier = pipeline(
6
- "text-classification",
7
- model="finiteautomata/beto-emotion-analysis",
8
- tokenizer="finiteautomata/beto-emotion-analysis",
9
- return_all_scores=True
10
- )
11
 
12
- # Diccionario de traducci贸n de emociones en ingl茅s a espa帽ol
13
- emociones_traducidas = {
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
- def analizar_emocion(texto):
24
- # Detectar emociones
25
- resultado = emotion_classifier(texto)
26
- emocion_principal = max(resultado[0], key=lambda x: x['score'])
27
- etiqueta_traducida = emociones_traducidas[emocion_principal['label']]
28
- return f"Emoci贸n principal: {etiqueta_traducida} (confianza: {emocion_principal['score']:.2f})"
 
 
 
 
 
 
 
 
 
29
 
30
- # Crear la interfaz Gradio
31
- iface = gr.Interface(fn=analizar_emocion, inputs="text", outputs="text")
 
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