Jeysshon commited on
Commit
699ec29
1 Parent(s): 58aa8cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -1,14 +1,20 @@
1
- import os
2
- from fastai.vision.all import *
3
- import gradio as gr
4
-
5
- # Cargar los modelos
6
- learn_emotion = load_learner('emotions_jey.pkl')
7
- learn_emotion_labels = learn_emotion.dls.vocab
8
 
9
  learn_sentiment = load_learner('sentiment_jey.pkl')
10
  learn_sentiment_labels = learn_sentiment.dls.vocab
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Función de predicción
13
  def predict(img_path):
14
  img = PILImage.create(img_path)
@@ -16,13 +22,13 @@ def predict(img_path):
16
  pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
17
  pred_sentiment, pred_sentiment_idx, probs_sentiment = learn_sentiment.predict(img)
18
 
19
- emotions = {label: float(prob) for label, prob in zip(learn_emotion_labels, probs_emotion)}
20
- sentiments = {label: float(prob) for label, prob in zip(learn_sentiment_labels, probs_sentiment)}
21
 
22
  return emotions, sentiments
23
 
24
  # Interfaz de Gradio
25
- title = "Detector de emociones y sentimientos faciales "
26
  description = (
27
  "Esta interfaz utiliza redes neuronales para detectar emociones y sentimientos a partir de imágenes faciales."
28
  )
@@ -36,13 +42,12 @@ examples = [
36
  'happy2.jpg',
37
  'neutral1.jpg',
38
  'neutral2.jpg'
39
-
40
  ]
41
 
42
  iface = gr.Interface(
43
  fn=predict,
44
  inputs=gr.Image(shape=(48, 48), image_mode='L'),
45
- outputs=[gr.Label(label='Emotion'), gr.Label(label='Sentiment')],
46
  title=title,
47
  examples=examples,
48
  description=description,
@@ -51,3 +56,6 @@ iface = gr.Interface(
51
  )
52
 
53
  iface.launch(enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  learn_sentiment = load_learner('sentiment_jey.pkl')
3
  learn_sentiment_labels = learn_sentiment.dls.vocab
4
 
5
+ # Diccionario de mapeo de etiquetas en inglés a etiquetas en español
6
+ label_mapping = {
7
+ 'angry': 'enojado',
8
+ 'disgust': 'asco',
9
+ 'fear': 'miedo',
10
+ 'happy': 'feliz',
11
+ 'sad': 'triste',
12
+ 'surprise': 'sorpresa',
13
+ 'neutral': 'neutral',
14
+ 'negative': 'negativo',
15
+ 'positive': 'positivo'
16
+ }
17
+
18
  # Función de predicción
19
  def predict(img_path):
20
  img = PILImage.create(img_path)
 
22
  pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
23
  pred_sentiment, pred_sentiment_idx, probs_sentiment = learn_sentiment.predict(img)
24
 
25
+ emotions = {label_mapping[label]: float(prob) for label, prob in zip(learn_emotion_labels, probs_emotion)}
26
+ sentiments = {label_mapping[label]: float(prob) for label, prob in zip(learn_sentiment_labels, probs_sentiment)}
27
 
28
  return emotions, sentiments
29
 
30
  # Interfaz de Gradio
31
+ title = "Detector de emociones y sentimientos faciales"
32
  description = (
33
  "Esta interfaz utiliza redes neuronales para detectar emociones y sentimientos a partir de imágenes faciales."
34
  )
 
42
  'happy2.jpg',
43
  'neutral1.jpg',
44
  'neutral2.jpg'
 
45
  ]
46
 
47
  iface = gr.Interface(
48
  fn=predict,
49
  inputs=gr.Image(shape=(48, 48), image_mode='L'),
50
+ outputs=[gr.Label(label='Emoción'), gr.Label(label='Sentimiento')],
51
  title=title,
52
  examples=examples,
53
  description=description,
 
56
  )
57
 
58
  iface.launch(enable_queue=True)
59
+
60
+
61
+