emociones / app.py
Jeysshon
Update app.py
f8aaecd
import os
from fastai.vision.all import *
import gradio as gr
# Cargar los modelos
learn_emotion = load_learner('emotions_jey.pkl')
learn_emotion_labels = learn_emotion.dls.vocab
learn_sentiment = load_learner('sentiment_jey.pkl')
learn_sentiment_labels = learn_sentiment.dls.vocab
# Diccionario
label_mapping = {
'angry': 'enojado',
'disgust': 'asco',
'fear': 'miedo',
'happy': 'feliz',
'sad': 'triste',
'surprise': 'sorpresa',
'neutral': 'neutral',
'negative': 'negativo',
'positive': 'positivo'
}
# Funci贸n de predicci贸n
def predict(img_path):
img = PILImage.create(img_path)
pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
pred_sentiment, pred_sentiment_idx, probs_sentiment = learn_sentiment.predict(img)
emotions = {label_mapping[label]: float(prob) for label, prob in zip(learn_emotion_labels, probs_emotion)}
sentiments = {label_mapping[label]: float(prob) for label, prob in zip(learn_sentiment_labels, probs_sentiment)}
return emotions, sentiments
# Interfaz de Gradio
title = "Detector de emociones y sentimientos faciales"
description = (
"Esta interfaz utiliza redes neuronales para detectar emociones y sentimientos a partir de im谩genes faciales."
)
article = "Esta herramienta proporciona una forma r谩pida de analizar emociones y sentimientos en im谩genes."
examples = [
'PrivateTest_10131363.jpg',
'angry1.png',
'angry2.jpg',
'happy1.jpg',
'happy2.jpg',
'neutral1.jpg',
'neutral2.jpg'
]
iface = gr.Interface(
fn=predict,
inputs=gr.Image(shape=(48, 48), image_mode='L'),
outputs=[gr.Label(label='Emoci贸n'), gr.Label(label='Sentimiento')],
title=title,
examples=examples,
description=description,
article=article,
allow_flagging='never'
)
iface.launch(enable_queue=True)