File size: 9,697 Bytes
8fd08e0
aaa3b8b
da5b41d
8fd08e0
 
aaa3b8b
 
41d9375
aaa3b8b
 
8fd08e0
 
 
aaa3b8b
 
 
 
8fd08e0
 
 
 
 
 
 
 
 
 
 
 
 
41d9375
8fd08e0
 
41d9375
 
8fd08e0
aaa3b8b
8fd08e0
 
aaa3b8b
8fd08e0
 
 
aaa3b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fd08e0
41d9375
 
8fd08e0
da5b41d
 
8fd08e0
41d9375
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa3b8b
41d9375
 
 
aaa3b8b
41d9375
 
aaa3b8b
41d9375
aaa3b8b
41d9375
 
 
 
 
 
 
 
da5b41d
 
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
import streamlit as st
import pandas as pd
from st_audiorec import st_audiorec
import datetime
import os
import matplotlib.pyplot as plt
import librosa
from src.model.transcriber import transcribe_audio
from src.model.predict import predict_emotion


DIRECTORY = "audios"
FILE_NAME = "audio.wav"
CHUNK = 1024
# FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000

def application():
    st.title("SISE ultimate challenge")
    st.write("C'est le dernier challenge de la formation SISE.")
    st.markdown("""
        **Overview:**
        - Analyse de logs
        - Analyse de données
        - Machine learning
    """)
    
    st.markdown("---")
    
    tab1, tab2, tab3 = st.tabs(["⬆️ Record Audio", "🔈 Realtime Audio", "📝 Transcription"])

    with tab1:
        st.header("⬆️ Upload Audio Record")
        st.write("Here you can upload a pre-recorded audio.")
        audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "ogg"])
        
        if audio_file is not None:
            
            with open(os.path.join(DIRECTORY,FILE_NAME), "wb") as f:
                f.write(audio_file.getbuffer())
                st.success(f"Saved file: {FILE_NAME}")


            start_inference = st.button("Start emotion recogniton","inf_on_upl_btn")
            emotion_labels = ["joie", "colère", "neutre"]
            colors = ['#f6d60a', '#f71c1c', '#cac8c8']
        
            if start_inference:
                # Configuration Streamlit
                with st.spinner("Real-time emotion analysis..."):
                    # uploaded_file = st.file_uploader("Choisissez un fichier audio", type=["wav", "mp3"])

                    if audio_file is not None:
                        # Charger et rééchantillonner l'audio
                        audio, sr = librosa.load(audio_file, sr=RATE)
                        # chunk = audio_file
                        
                        # Paramètres de la fenêtre glissante
                        window_size = 1  # en secondes
                        hop_length = 0.5  # en secondes
                        
                        # Créer un graphique en temps réel
                        fig, ax = plt.subplots()
                        lines = [ax.plot([], [], label=emotion)[0] for emotion in emotion_labels]
                        ax.set_ylim(0, 1)
                        ax.set_xlim(0, len(audio) / sr)
                        ax.set_xlabel("Temps (s)")
                        ax.set_ylabel("Probabilité")
                        ax.legend()
                        
                        chart = st.pyplot(fig)
                        
                        scores = [[],[],[]] # 3 émotions pour l'instant
                        
                        # Traitement par fenêtre glissante
                        for i in range(0, len(audio), int(hop_length * sr)):
                            chunk = audio[i:i + int(window_size * sr)]
                            if len(chunk) < int(window_size * sr):
                                break
                            
                            emotion_scores = predict_emotion(chunk, output_probs=True, sampling_rate=RATE)
                            
                            # Mettre à jour le graphique
                            for emotion, line in zip(emotion_labels, lines):
                                xdata = list(line.get_xdata())
                                ydata = list(line.get_ydata())
                                xdata.append(i / sr)
                                ydata.append(emotion_scores[emotion])
                                scores[list(emotion_scores).index(emotion)].append(emotion_scores[emotion])
                                line.set_data(xdata, ydata)
                            
                            ax.relim()
                            ax.autoscale_view()
                            chart.pyplot(fig, use_container_width=True)
                        
                        # Prepare the styling
                        st.markdown("""        
                                    <style>
                                    .colored-box {
                                        padding: 10px;
                                        border-radius: 5px;
                                        color: white;
                                        font-weight: bold;
                                        text-align: center;
                                    }
                                    </style>
                                    """
                                    , unsafe_allow_html=True)

                        # Dynamically create the specified number of columns
                        columns = st.columns(len(emotion_scores))

                        # emotion_scores_mean = [sum(sublist) / len(sublist) for sublist in scores]
                        emotion_scores_mean = {emotion:sum(sublist) / len(sublist) for emotion, sublist in zip(emotion_labels, scores)}
                        max_emo = max(emotion_scores_mean)
                        emotion_scores_sorted = dict(sorted(emotion_scores_mean.items(), key=lambda x: x[1], reverse=True))
                        colors_sorted = [colors[list(emotion_scores_mean.keys()).index(key)] for key in list(emotion_scores_sorted.keys())]

                        # Add content to each column
                        for i, (col, emotion) in enumerate(zip(columns, emotion_scores_sorted)):
                            color = colors_sorted[i % len(colors_sorted)]  # Cycle through colors if more columns than colors
                            col.markdown(f"""
                                        <div class="colored-box" style="background-color: {color};">
                                            {emotion} : {100*emotion_scores_sorted[emotion]:.2f} %
                                        </div>
                                        """
                            , unsafe_allow_html=True)
                            


                        st.success("Analyse terminée !")
                    else:
                        st.warning("You need to load an audio file !")
                
                st.subheader("Feedback")

                # Initialisation du fichier CSV
                csv_file = os.path.join("src","predictions","feedback.csv")

                # Vérifier si le fichier CSV existe, sinon le créer avec des colonnes appropriées
                if not os.path.exists(csv_file):
                    df = pd.DataFrame(columns=["filepath", "prediction", "feedback"])
                    df.to_csv(csv_file, index=False)

                # Charger les données existantes du CSV
                df = pd.read_csv(csv_file)

                with st.form("feedback_form"):
                    st.write("What should have been the correct prediction ? (*Choose the same emotion if the prediction was correct*).")
                    feedback = st.selectbox("Your answer :", ['Sadness','Anger', 'Disgust', 'Fear', 'Surprise', 'Joy', 'Neutral'])
                    submit_button = st.form_submit_button("Submit")
                    st.write("En cliquant sur ce bouton, vous acceptez que votre audio soit sauvegardé dans notre base de données.")

                    if submit_button:
                        # Ajouter le feedback au DataFrame
                        new_entry = {"filepath": audio_file.name, "prediction": max_emo, "feedback": feedback}
                        df = df.append(new_entry, ignore_index=True)
                        
                        # Sauvegarder les données mises à jour dans le fichier CSV
                        df.to_csv(csv_file, index=False)

                        # Sauvegarder le fichier audio
                        with open(os.path.join("src","predictions","data"), "wb") as f:
                            f.write(audio_file.getbuffer())
                        
                        # Confirmation pour l'utilisateur
                        st.success("Merci pour votre retour ! Vos données ont été sauvegardées.")



    with tab2:
        st.header("🔈 Realtime Audio Record")
        st.write("Here you can record an audio.")
        
        if st.button("Register", key="register-button"):
            st.success("Audio registered successfully.")

        audio_file = st_audiorec()

        if audio_file is not None:
            st.audio(audio_file, format='audio/wav')
            
    with tab3:
        st.header("📝 Speech2Text Transcription")
        st.write("Here you can get the audio transcript.")

        save = st.checkbox("Save transcription to .txt", value=False, key="save-transcript")

        ############################# A décommenté quand ce sera débogué
        if st.button("Transcribe", key="transcribe-button"):
        #     # Fonction pour transcrire l'audio
            # transcription = transcribe_audio(st.audio)

        #     # Charger et transcrire l'audio
        #     # audio, rate = load_audio(audio_file_path) # (re)chargement de l'audio si nécessaire
            # transcription = transcribe_audio(audio_file, sampling_rate=16000)

        #     # Afficher la transcription
            # st.write("Transcription :", transcription)
                
            st.success("Audio registered successfully.")
        #     if save:
        #         file_path = "transcript.txt"
    
        #         # Write the text to the file
        #         with open(file_path, "w") as file:
        #             file.write(transcription)
                
        #         st.success(f"Text saved to {file_path}")