Spaces:
Sleeping

File size: 8,522 Bytes
632bb6d
2bb1cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
632bb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb1cae
632bb6d
2bb1cae
 
 
 
632bb6d
 
 
 
2bb1cae
632bb6d
2bb1cae
 
 
632bb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb1cae
 
632bb6d
 
2bb1cae
 
 
 
632bb6d
 
2bb1cae
 
 
 
632bb6d
 
 
 
 
 
 
 
 
2bb1cae
632bb6d
2bb1cae
 
 
 
632bb6d
 
 
 
 
 
 
 
 
2bb1cae
632bb6d
2bb1cae
 
 
 
632bb6d
2bb1cae
632bb6d
 
 
2bb1cae
632bb6d
 
 
 
 
 
 
 
2bb1cae
 
 
 
 
 
632bb6d
 
 
 
 
 
 
 
 
2bb1cae
 
 
632bb6d
2bb1cae
632bb6d
 
 
 
 
 
 
 
 
 
2bb1cae
632bb6d
 
 
 
 
 
2bb1cae
632bb6d
 
 
 
 
 
2bb1cae
 
632bb6d
 
 
 
 
2bb1cae
632bb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb1cae
 
 
632bb6d
2bb1cae
632bb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb1cae
 
 
 
 
 
632bb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb1cae
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// 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;