File size: 8,573 Bytes
5f1dc09
 
 
6d1af41
5b2ee5d
 
 
 
 
5f1dc09
 
5b2ee5d
 
 
 
 
 
5f1dc09
5b2ee5d
 
ea51b52
 
5b2ee5d
5f1dc09
ea51b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f1dc09
ea51b52
 
 
 
 
 
 
 
 
 
 
5b2ee5d
5f1dc09
ea51b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299806d
 
 
 
ea51b52
 
5b2ee5d
5f1dc09
5b2ee5d
 
5f1dc09
ea51b52
 
 
 
5f1dc09
5b2ee5d
 
5f1dc09
ea51b52
5f1dc09
ea51b52
5f1dc09
ea51b52
5f1dc09
ea51b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f1dc09
ea51b52
5b2ee5d
5f1dc09
5b2ee5d
5f1dc09
5b2ee5d
ea51b52
5f1dc09
5b2ee5d
 
5f1dc09
 
ea51b52
5b2ee5d
5f1dc09
ea51b52
 
5f1dc09
ea51b52
5b2ee5d
 
 
5f1dc09
 
5b2ee5d
ea51b52
5b2ee5d
 
 
 
 
 
 
5f1dc09
ea51b52
5b2ee5d
 
5f1dc09
5b2ee5d
5f1dc09
5b2ee5d
5f1dc09
ea51b52
5f1dc09
ea51b52
 
 
 
 
 
5f1dc09
5b2ee5d
 
 
 
5f1dc09
ea51b52
5b2ee5d
 
 
 
5f1dc09
ea51b52
5b2ee5d
 
 
5f1dc09
5b2ee5d
5f1dc09
5b2ee5d
 
 
 
5f1dc09
ea51b52
5b2ee5d
 
 
 
 
 
 
5f1dc09
 
ea51b52
e7b6720
5f1dc09
ea51b52
5f1dc09
ea51b52
5f1dc09
 
 
 
 
 
ea51b52
5f1dc09
5b2ee5d
5f1dc09
 
 
 
ea51b52
 
5b2ee5d
ea51b52
5b2ee5d
 
 
 
 
ea51b52
5b2ee5d
ea51b52
5b2ee5d
 
 
 
 
 
ea51b52
5f1dc09
 
5b2ee5d
 
5f1dc09
 
ea51b52
 
5f1dc09
ea51b52
5b2ee5d
ea51b52
 
 
 
 
 
 
5f1dc09
 
5b2ee5d
ea51b52
 
5b2ee5d
5f1dc09
 
 
ea51b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b2ee5d
5f1dc09
ea51b52
 
 
5f1dc09
 
 
ea51b52
e7b6720
5f1dc09
 
5b2ee5d
 
 
 
 
 
5f1dc09
 
ea51b52
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import streamlit as st
from PIL import Image


from models import (
    load_models,
    analyse_image,
    analyse_text,
    transcribe_audio
)

from fusion import (
    fusion_logic,
    generate_summary,
    create_bar_chart,
    create_timeline_chart
)

st.set_page_config(
    page_title="MoodSyncAI",
    layout="wide",
    initial_sidebar_state="expanded"
)

# ---------------- CUSTOM CSS ----------------
st.markdown(
    """
    <style>
    .main {
        background-color: #0f172a;
        color: white;
    }

    .stApp {
        background: linear-gradient(to bottom right, #0f172a, #1e293b);
    }

    h1, h2, h3 {
        color: #f8fafc;
    }

    .glass-box {
        background: rgba(255,255,255,0.08);
        padding: 20px;
        border-radius: 20px;
        backdrop-filter: blur(10px);
        margin-bottom: 20px;
        border: 1px solid rgba(255,255,255,0.1);
    }

    .metric-card {
        background: rgba(255,255,255,0.08);
        padding: 15px;
        border-radius: 15px;
        text-align: center;
    }

    .big-font {
        font-size: 20px;
        font-weight: bold;
    }

    .small-font {
        color: #cbd5e1;
    }

    .stButton>button {
        background: linear-gradient(to right, #3b82f6, #8b5cf6);
        color: white;
        border-radius: 12px;
        border: none;
        height: 50px;
        width: 100%;
        font-size: 18px;
        font-weight: bold;
    }
    </style>
    """,
    unsafe_allow_html=True
)

# ---------------- HEADER ----------------
st.markdown(
    """
    <div class="glass-box">
        <h1>MoodSyncAI</h1>
        <p class="small-font">
        Multi-Modal Emotion & Sentiment Analysis using Image, Audio, Text and Webcam.
        </p>
    </div>
    """,
    unsafe_allow_html=True
)

# ---------------- SIDEBAR ----------------
st.sidebar.title("⚙️ Settings")

# st.sidebar.info(
#     "This application analyses emotions using multiple modalities."
# )

input_mode = st.sidebar.radio(
    "Choose Input Mode",
    ["Upload Image", "Use Webcam"]
)

show_charts = st.sidebar.checkbox(
    "Show Confidence Charts",
    value=True
)

show_timeline = st.sidebar.checkbox(
    "Show Webcam Timeline",
    value=True
)

# ---------------- LOAD MODELS ----------------
image_model, text_model, whisper_model = load_models()

if "emotion_timeline" not in st.session_state:
    st.session_state.emotion_timeline = []

# ---------------- INPUT SECTION ----------------
st.subheader("Input Section")

col1, col2 = st.columns(2)

uploaded_image = None
webcam_image = None

with col1:

    st.markdown("### Face Input")

    if input_mode == "Upload Image":

        uploaded_image = st.file_uploader(
            "Upload a face image",
            type=["jpg", "jpeg", "png"]
        )

    else:

        webcam_image = st.camera_input(
            "Capture image from webcam"
        )

with col2:

    st.markdown("### Audio & Text")

    uploaded_audio = st.file_uploader(
        "Upload audio clip (optional)",
        type=["wav", "mp3", "m4a"]
    )

    user_text = st.text_area(
        "Enter spoken sentence",
        placeholder="Example: No, everything is perfectly fine."
    )

# ---------------- ANALYSIS BUTTON ----------------
if st.button("Analyse Emotion"):

    image_source = uploaded_image if input_mode == "Upload Image" else webcam_image

    if image_source is None:
        st.error("Please upload or capture an image.")

    elif user_text.strip() == "" and uploaded_audio is None:
        st.error("Please enter text or upload audio.")

    else:

        image = Image.open(image_source).convert("RGB")

        st.subheader("Captured Face")
        st.image(image, width=350)

        # IMAGE ANALYSIS
        image_predictions, image_emotion, image_score = analyse_image(
            image_model,
            image
        )

        if input_mode == "Use Webcam":

            st.session_state.emotion_timeline.append(
                {
                    "frame": len(st.session_state.emotion_timeline) + 1,
                    "emotion": image_emotion,
                    "confidence": round(image_score * 100, 2)
                }
            )

        # AUDIO + TEXT
        final_text = user_text.strip()
        audio_transcript = ""

        if uploaded_audio is not None:

            st.audio(uploaded_audio)

            with st.spinner("Transcribing audio..."):

                audio_transcript = transcribe_audio(
                    whisper_model,
                    uploaded_audio
                )

            st.success(f"Transcript: {audio_transcript}")

            if final_text == "":
                final_text = audio_transcript
            else:
                final_text = final_text + " " + audio_transcript

        # TEXT SENTIMENT
        text_predictions, text_sentiment, text_score = analyse_text(
            text_model,
            final_text
        )

        # AUDIO SENTIMENT
        audio_sentiment = "not provided"
        audio_score = 0.0
        audio_predictions = None

        if audio_transcript.strip() != "":

            audio_predictions, audio_sentiment, audio_score = analyse_text(
                text_model,
                audio_transcript
            )

        # FUSION
        fusion_result = fusion_logic(
            image_emotion=image_emotion,
            image_score=image_score,
            text_sentiment=text_sentiment,
            text_score=text_score,
            audio_sentiment=audio_sentiment,
            audio_score=audio_score
        )

        # ---------------- RESULTS ----------------
        st.subheader("Analysis Results")

        m1, m2, m3, m4 = st.columns(4)

        with m1:
            st.metric(
                "Visual Emotion",
                image_emotion,
                f"{round(image_score * 100, 2)}%"
            )

        with m2:
            st.metric(
                "Text Sentiment",
                text_sentiment.capitalize(),
                f"{round(text_score * 100, 2)}%"
            )

        with m3:

            if audio_transcript.strip() != "":

                st.metric(
                    "Audio Sentiment",
                    audio_sentiment.capitalize(),
                    f"{round(audio_score * 100, 2)}%"
                )

            else:

                st.metric(
                    "Audio Sentiment",
                    "Not Provided",
                    "Optional"
                )

        with m4:
            st.metric(
                "Fusion Result",
                fusion_result["badge"],
                f'{fusion_result["confidence"]}%'
            )

        # ---------------- CHARTS ----------------
        if show_charts:

            st.subheader("📈 Confidence Visualisations")

            st.plotly_chart(
                create_bar_chart(
                    image_predictions,
                    "Visual Emotion Confidence"
                ),
                use_container_width=True
            )

            st.plotly_chart(
                create_bar_chart(
                    text_predictions,
                    "Text Sentiment Confidence"
                ),
                use_container_width=True
            )

            if audio_predictions is not None:

                st.plotly_chart(
                    create_bar_chart(
                        audio_predictions,
                        "Audio Sentiment Confidence"
                    ),
                    use_container_width=True
                )

        # ---------------- TIMELINE ----------------
        if (
            input_mode == "Use Webcam"
            and len(st.session_state.emotion_timeline) > 0
            and show_timeline
        ):

            st.subheader("🎥 Webcam Emotion Timeline")

            st.plotly_chart(
                create_timeline_chart(
                    st.session_state.emotion_timeline
                ),
                use_container_width=True
            )

        # ---------------- SUMMARY ----------------
        st.subheader("Generative Summary")

        summary = generate_summary(
            image_emotion=image_emotion,
            image_sentiment=fusion_result["image_sentiment"],
            text_sentiment=text_sentiment,
            audio_sentiment=audio_sentiment,
            fusion_status=fusion_result["status"],
            audio_used=audio_transcript.strip() != ""
        )

        st.success(summary)

        # ---------------- FOOTER ----------------
st.markdown("---")

# st.markdown(
#     unsafe_allow_html=True
# )