Spaces:
Sleeping
Sleeping
Download src/MainApp.jsx from Aya07/emotion-ui: direct link, hf CLI and curl.
- Browser
- Download file 4.67 kB
-
https://huggingface.co/spaces/Aya07/emotion-ui/resolve/main/src/MainApp.jsx
- Command line
-
hf download hf://spaces/Aya07/emotion-ui/src/MainApp.jsx
-
curl -L -o MainApp.jsx https://huggingface.co/spaces/Aya07/emotion-ui/resolve/main/src/MainApp.jsx
4.67 kB
| // src/MainApp.jsx | |
| import React, { useState } from 'react'; | |
| import { useAuth } from './contexts/AuthContext'; | |
| import Header from './components/Header'; | |
| import ModeBar from './components/ModeBar'; | |
| import UploadArea from './components/UploadArea'; | |
| import RecordArea from './components/RecordArea'; | |
| import ResultsCard from './components/ResultsCard'; | |
| import StatsBar from './components/StatsBar'; | |
| import Footer from './components/Footer'; | |
| import RealtimePredictor from './components/RealtimePredictor'; | |
| import { translations } from './constants/translations'; | |
| import { useRecorder } from './hooks/useRecorder'; | |
| import { predictEmotion } from './utils/api'; | |
| const MainApp = () => { | |
| const { user, logout } = useAuth(); | |
| const [lang, setLang] = useState('en'); | |
| const [mode, setMode] = useState('file'); // 'file', 'record', 'realtime' | |
| const [file, setFile] = useState(null); | |
| const [fileName, setFileName] = useState(''); | |
| const [result, setResult] = useState(null); | |
| const [loading, setLoading] = useState(false); | |
| const [error, setError] = useState(null); | |
| const t = translations[lang]; | |
| const recorder = useRecorder(() => setError(t.errorMic)); | |
| const handleFileChange = (e) => { | |
| const selected = e.target.files[0]; | |
| if (selected) { | |
| setFile(selected); | |
| setFileName(selected.name); | |
| setResult(null); | |
| setError(null); | |
| } | |
| }; | |
| const handleModeChange = (newMode) => { | |
| setMode(newMode); | |
| setError(null); | |
| setResult(null); | |
| if (newMode === 'file') { | |
| recorder.reset(); | |
| } | |
| }; | |
| const handlePredict = async () => { | |
| let audioFile = mode === 'file' ? file : recorder.audioBlob; | |
| if (!audioFile && mode === 'file') { | |
| setError(t.errorSelectFile); | |
| return; | |
| } | |
| if (!audioFile && mode === 'record') { | |
| setError(t.errorRecordFirst); | |
| return; | |
| } | |
| if (mode === 'record' && audioFile instanceof Blob && !audioFile.name) { | |
| audioFile = new File([audioFile], `recording_${Date.now()}.wav`, { type: 'audio/wav' }); | |
| } | |
| setLoading(true); | |
| setResult(null); | |
| setError(null); | |
| try { | |
| const data = await predictEmotion(audioFile); | |
| setResult(data); | |
| } catch (err) { | |
| console.error(err); | |
| setError(err.message || t.errorServer); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }; | |
| const currentFileName = mode === 'file' ? fileName : recorder.fileName; | |
| // Rendu du contenu selon le mode | |
| const renderContent = () => { | |
| if (mode === 'realtime') { | |
| return <RealtimePredictor t={t} lang={lang} />; | |
| } | |
| return ( | |
| <div className="main-grid"> | |
| {mode === 'file' ? ( | |
| <UploadArea | |
| file={file} | |
| fileName={fileName} | |
| onFileChange={handleFileChange} | |
| onPredict={handlePredict} | |
| loading={loading} | |
| error={error} | |
| onErrorDismiss={() => setError(null)} | |
| t={t} | |
| /> | |
| ) : ( | |
| <RecordArea | |
| recorder={recorder} | |
| onPredict={handlePredict} | |
| loading={loading} | |
| error={error} | |
| onErrorDismiss={() => setError(null)} | |
| t={t} | |
| /> | |
| )} | |
| <ResultsCard | |
| result={result} | |
| lang={lang} | |
| fileName={currentFileName} | |
| mode={mode} | |
| t={t} | |
| /> | |
| </div> | |
| ); | |
| }; | |
| return ( | |
| <div className="app" dir={t.dir}> | |
| <div id="root"> | |
| <div className="user-header"> | |
| <div className="user-info"> | |
| <span>{t.Welcome}, {user?.name}!</span> | |
| </div> | |
| <button onClick={logout} className="logout-btn"> | |
| <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> | |
| <path d="M9 21H5C4.46957 21 3.96086 20.7893 3.58579 20.4142C3.21071 20.0391 3 19.5304 3 19V5C3 4.46957 3.21071 3.96086 3.58579 3.58579C3.96086 3.21071 4.46957 3 5 3H9" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/> | |
| <path d="M16 17L21 12L16 7" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/> | |
| <path d="M21 12H9" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/> | |
| </svg> | |
| {t.Logout} | |
| </button> | |
| </div> | |
| <Header lang={lang} setLang={setLang} t={t} /> | |
| <ModeBar mode={mode} setMode={handleModeChange} t={t} /> | |
| <div className="content-wrapper"> | |
| {renderContent()} | |
| </div> | |
| <StatsBar t={t} /> | |
| <Footer t={t} /> | |
| </div> | |
| </div> | |
| ); | |
| }; | |
| export default MainApp; |