Spaces:
Sleeping

emotion-ui / src /components /RealtimePredictor.jsx
Aya07's picture
Upload RealtimePredictor.jsx
632bb6d verified
Raw History Blame Contribute Delete
8.52 kB
// components/RealtimePredictor.jsx
import React from 'react';
import { useRealtimeWebSocket } from '../hooks/useRealtimeWebSocket';
const RealtimePredictor = ({ t, lang }) => {
const {
isConnected,
isRecording,
currentPrediction,
history,
error,
bufferingProgress,
startRealtime,
stopRealtime
} = useRealtimeWebSocket();
// 🔍 DEBUG: Affiche dans la console quand currentPrediction change
React.useEffect(() => {
console.log('🔍 RealtimePredictor - currentPrediction a changé:', currentPrediction);
}, [currentPrediction]);
React.useEffect(() => {
console.log('🔍 RealtimePredictor - history a changé:', history);
}, [history]);
const getEmotionLabel = (emotion) => {
const labels = {
en: { Angry: '😠 Angry', Happy: '😊 Happy', Neutral: '😐 Neutral', Sad: '😢 Sad' },
fr: { Angry: '😠 Colère', Happy: '😊 Joie', Neutral: '😐 Neutre', Sad: '😢 Tristesse' },
ar: { Angry: '😠 غضب', Happy: '😊 سعادة', Neutral: '😐 محايد', Sad: '😢 حزن' }
};
return labels[lang]?.[emotion] || emotion;
};
const getEmotionColor = (emotion) => {
const colors = {
Angry: { bg: '#dc3545', light: '#ffebee' },
Happy: { bg: '#28a745', light: '#e8f5e9' },
Neutral: { bg: '#6c757d', light: '#f5f5f5' },
Sad: { bg: '#007bff', light: '#e3f2fd' }
};
return colors[emotion] || { bg: '#6c757d', light: '#f5f5f5' };
};
return (
<div style={{ padding: '20px', maxWidth: '600px', margin: '0 auto' }}>
{/* 🟢 BANDEAU DE DEBUG - À SUPPRIMER PLUS TARD */}
{/* Titre */}
{/* Statut */}
<div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '20px' }}>
<div style={{
padding: '8px 16px',
borderRadius: '20px',
background: isConnected ? '#d4edda' : '#f8d7da',
color: isConnected ? '#155724' : '#721c24'
}}>
{isConnected ? '🟢 ' + (t.wsConnected || 'Connecté') : '🔴 ' + (t.wsDisconnected || 'Déconnecté')}
</div>
{isRecording && (
<div style={{ padding: '8px 16px', background: '#dc3545', color: 'white', borderRadius: '20px' }}>
🔴 {t.recording || 'Enregistrement...'}
</div>
)}
</div>
{/* Boutons */}
<div style={{ textAlign: 'center', marginBottom: '30px' }}>
{!isRecording ? (
<button
onClick={startRealtime}
disabled={!isConnected}
style={{
padding: '15px 40px',
fontSize: '18px',
background: isConnected ? '#28a745' : '#6c757d',
color: 'white',
border: 'none',
borderRadius: '50px',
cursor: isConnected ? 'pointer' : 'not-allowed'
}}
>
🎤 {t.startRealtime || 'Démarrer'}
</button>
) : (
<button
onClick={stopRealtime}
style={{
padding: '15px 40px',
fontSize: '18px',
background: '#dc3545',
color: 'white',
border: 'none',
borderRadius: '50px',
cursor: 'pointer'
}}
>
⏹️ {t.stopRealtime || 'Arrêter'}
</button>
)}
</div>
{/* Barre de progression */}
{isRecording && bufferingProgress > 0 && bufferingProgress < 100 && (
<div style={{ marginBottom: '20px' }}>
<div style={{ fontSize: '12px', marginBottom: '5px' }}>
{t.buffering || 'Bufferisation'}: {bufferingProgress}%
</div>
<div style={{ background: '#e0e0e0', borderRadius: '10px', height: '8px' }}>
<div style={{
width: `${bufferingProgress}%`,
background: '#007bff',
height: '8px',
borderRadius: '10px',
transition: 'width 0.3s'
}}></div>
</div>
</div>
)}
{/* Erreur */}
{error && (
<div style={{
background: '#f8d7da',
border: '1px solid #f5c6cb',
borderRadius: '8px',
padding: '12px',
marginBottom: '20px',
color: '#721c24'
}}>
❌ {error}
</div>
)}
{/* 🟢 PRÉDICTION ACTUELLE - GRAND AFFICHAGE */}
{currentPrediction && (
<div style={{
background: getEmotionColor(currentPrediction.emotion).light,
border: `2px solid ${getEmotionColor(currentPrediction.emotion).bg}`,
borderRadius: '20px',
padding: '25px',
marginBottom: '25px',
textAlign: 'center'
}}>
<div style={{ fontSize: '14px', color: '#666', marginBottom: '10px' }}>
{t.currentPrediction || 'Prédiction actuelle'}
</div>
<div style={{
fontSize: '48px',
fontWeight: 'bold',
color: getEmotionColor(currentPrediction.emotion).bg
}}>
{getEmotionLabel(currentPrediction.emotion)}
</div>
<div style={{
fontSize: '24px',
marginTop: '10px',
color: getEmotionColor(currentPrediction.emotion).bg
}}>
{currentPrediction.confidence}% {t.confidence || 'confiance'}
</div>
{/* Détails des probabilités */}
{currentPrediction.details && (
<div style={{ marginTop: '20px', textAlign: 'left' }}>
<div style={{ fontSize: '12px', color: '#666', marginBottom: '10px' }}>
{t.detailedProbabilities || 'Probabilités détaillées'}:
</div>
{Object.entries(currentPrediction.details).map(([emotion, prob]) => (
<div key={emotion} style={{ marginBottom: '10px' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', fontSize: '12px', marginBottom: '3px' }}>
<span>{getEmotionLabel(emotion)}</span>
<span>{prob}%</span>
</div>
<div style={{ background: '#e0e0e0', borderRadius: '5px', height: '6px' }}>
<div style={{
width: `${prob}%`,
background: getEmotionColor(emotion).bg,
height: '6px',
borderRadius: '5px'
}}></div>
</div>
</div>
))}
</div>
)}
</div>
)}
{/* 🟢 HISTORIQUE */}
{history.length > 0 && (
<div>
<h4 style={{ marginBottom: '10px' }}>📜 {t.history || 'Historique'}</h4>
<div style={{ maxHeight: '200px', overflowY: 'auto' }}>
{history.map((pred, idx) => (
<div key={idx} style={{
padding: '10px',
borderBottom: '1px solid #ddd',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center'
}}>
<div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>
<span style={{
width: '10px',
height: '10px',
borderRadius: '50%',
background: getEmotionColor(pred.emotion).bg
}}></span>
<span style={{ fontWeight: 'bold' }}>{getEmotionLabel(pred.emotion)}</span>
<span>{pred.confidence}%</span>
</div>
<span style={{ fontSize: '11px', color: '#888' }}>{pred.timestamp}</span>
</div>
))}
</div>
</div>
)}
{/* Message d'attente */}
{!isRecording && !currentPrediction && !error && history.length === 0 && (
<div style={{
textAlign: 'center',
padding: '40px',
background: '#f8f9fa',
borderRadius: '16px',
color: '#6c757d'
}}>
<div style={{ fontSize: '48px', marginBottom: '15px' }}>🎙️</div>
<div>{t.startRealtime || 'Cliquez sur Démarrer pour commencer'}</div>
<div style={{ fontSize: '12px', marginTop: '10px' }}>
(parlez pendant au moins 3 secondes pour voir le résultat)
</div>
</div>
)}
</div>
);
};
export default RealtimePredictor;