Alexa17 commited on
Commit
2477995
verified
1 Parent(s): 7dd3216

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -1,29 +1,34 @@
1
  import gradio as gr
2
- from transformers import T5ForConditionalGeneration, T5Tokenizer
3
 
4
- # Cargar el modelo y el tokenizador preentrenado
5
- model_name = "mrm8488/t5-base-finetuned-emotion"
6
- model = T5ForConditionalGeneration.from_pretrained(model_name)
7
- tokenizer = T5Tokenizer.from_pretrained(model_name)
 
 
 
8
 
9
- # Funci贸n para predecir la emoci贸n de un texto
10
- def detect_emotion(text):
11
- # Preprocesar el texto para el modelo T5
12
- input_text = "classify emotion: " + text
13
- input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
 
 
 
 
 
14
 
15
- # Generar la predicci贸n
16
- outputs = model.generate(input_ids, max_length=2, num_beams=4, early_stopping=True)
17
- emotion = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
- return emotion
 
 
19
 
20
- # Crear la interfaz con Gradio
21
- interface = gr.Interface(fn=detect_emotion,
22
- inputs=gr.Textbox(label="Introduce tu texto"),
23
- outputs=gr.Textbox(label="Emoci贸n detectada"),
24
- title="Detector de emociones",
25
- description="Introduce un texto para detectar la emoci贸n que contiene")
26
 
27
  # Lanzar la interfaz
28
- if __name__ == "__main__":
29
- interface.launch()
 
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()