File size: 1,611 Bytes
55316a9 4ced22e 55316a9 4ced22e 55316a9 4ced22e 55316a9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
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
# 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: float(prob) for label, prob in zip(learn_emotion_labels, probs_emotion)}
sentiments = {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 para parciales finales de PIB"
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='Emotion'), gr.Label(label='Sentiment')],
title=title,
examples=examples,
description=description,
article=article,
allow_flagging='never'
)
iface.launch(enable_queue=True)
|