File size: 8,368 Bytes
97eaafb
 
 
 
fc7b4a9
 
 
 
c84f2c4
fc7b4a9
 
75d43d2
fc7b4a9
 
c84f2c4
fc7b4a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75d43d2
fc7b4a9
 
75d43d2
 
fc7b4a9
75d43d2
fc7b4a9
91f3c16
fc7b4a9
 
75d43d2
 
 
 
fc7b4a9
 
75d43d2
fc7b4a9
 
 
 
 
 
2b37a16
97eaafb
fc7b4a9
 
 
75d43d2
344eaf8
 
 
 
 
 
 
 
 
 
fc7b4a9
 
c84f2c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97eaafb
c84f2c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0534c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc7b4a9
 
75d43d2
 
 
 
 
c84f2c4
75d43d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from src.preprocessing.preprocessor import (
    single_preprocessing,
    single_audio_preprocessing,
)
from src.spectttra.spectttra_trainer import spectttra_predict
from src.llm2vectrain.model import load_llm2vec_model
from src.llm2vectrain.llm2vec_trainer import l2vec_single_train, load_pca_model
from src.models.mlp import build_mlp, load_config
from src.utils.dataset import instance_scaler, audio_instance_scaler

import numpy as np
import pandas as pd


def predict_multimodal(audio_file, lyrics):
    """
    Predict script which includes preprocessing, feature extraction, and
    training the MLP model for a single data sample.

    Parameters
    ----------
    audio : audio_object
        Audio object file

    lyric : string
        Lyric string

    Returns
    -------
    prediction : str
        A string result of the prediction

    label : int
        A numerical representation of the prediction
    """
    # 1.) Instantiate LLM2Vec Model
    llm2vec_model = load_llm2vec_model()

    # 2.) Preprocess both audio and lyrics
    audio, lyrics = single_preprocessing(audio_file, lyrics)

    # 3.) Call the train method for both models
    audio_features = spectttra_predict(audio)
    audio_features = audio_features.reshape(1, -1)
    lyrics_features = l2vec_single_train(llm2vec_model, lyrics)

    # 4.) Scale the vectors using Z-Score
    audio_features, lyrics_features = instance_scaler(audio_features, lyrics_features)

    # 5.) Reduce the lyrics using saved PCA model
    reduced_lyrics = load_pca_model(lyrics_features)

    # 6.) Concatenate the vectors of audio_features + lyrics_features
    results = np.concatenate([audio_features, reduced_lyrics], axis=1)

    # ---- Load MLP Classifier ----
    config = load_config("config/model_config.yml")
    classifier = build_mlp(input_dim=results.shape[1], config=config)

    # 7.) Load trained weights
    model_path = "models/mlp/mlp_best_multimodal.pth"
    classifier.load_model(model_path)
    classifier.model.eval()

    # 8.) Run prediction
    confidence, prediction, label, probability = classifier.predict_single(
        results.flatten()
    )

    return {
        "confidence": confidence,
        "prediction": prediction,
        "label": label,
        "probability": probability,
    }


def predict_unimodal(audio_file):
    """
    Predict script of AUDIO only which includes preprocessing, feature extraction, and
    training the MLP model for a single data sample.

    Parameters
    ----------
    audio : audio_object
        Audio object file

    Returns
    -------
    prediction : str
        A string result of the prediction

    label : int
        A numerical representation of the prediction
    """

    # 1.) Preprocess the audio
    audio = single_audio_preprocessing(audio_file)

    # 2.) Call the inference method from SpecTTTra
    audio_features = spectttra_predict(audio)
    audio_features = audio_features.reshape(1, -1)

    # 4.) Scale the vector using Z-Score
    audio_features = audio_instance_scaler(audio_features)

    # 5.) Load MLP Classifier
    config = load_config("config/model_config.yml")
    classifier = build_mlp(input_dim=audio_features.shape[1], config=config)

    # 6.) Load trained weights
    model_path = "models/mlp/mlp_best_unimodal.pth"
    classifier.load_model(model_path)
    classifier.model.eval()

    # 8.) Run prediction
    confidence, prediction, label, probability = classifier.predict_single(
        audio_features.flatten()
    )

    return {
        "confidence": confidence,
        "prediction": prediction,
        "label": label,
        "probability": probability,
    }


def predict_combined(audio_file, lyrics):
    """
    Generate both multimodal and audio-only predictions efficiently.

    Follows the exact same logic as separate functions but reuses audio features.

    Parameters
    ----------
    audio_file : audio_object
        Audio object file
    lyrics : str
        Lyric string

    Returns
    -------
    dict
        Combined results containing both multimodal and audio-only predictions
    """
    import time

    start_time = time.time()

    # Load config once
    config = load_config("config/model_config.yml")

    # [1] Multimdoal prediction
    print("[Predict] Running multimodal prediction...")
    multimodal_start = time.time()

    # 1.) Load LLM2Vec Model
    llm2vec_model = load_llm2vec_model()

    # 2.) Preprocess both audio and lyrics
    audio_mm, lyrics_mm = single_preprocessing(audio_file, lyrics)

    # 3.) Extract features
    audio_features_mm = spectttra_predict(audio_mm)
    audio_features_mm = audio_features_mm.reshape(1, -1)
    lyrics_features = l2vec_single_train(llm2vec_model, lyrics_mm)

    # 4.) Scale the vectors using Z-Score
    audio_features_mm_scaled, lyrics_features_scaled = instance_scaler(
        audio_features_mm, lyrics_features
    )

    # 5.) Reduce the lyrics using saved PCA model
    reduced_lyrics = load_pca_model(lyrics_features_scaled)

    # 6.) Concatenate the vectors
    multimodal_features = np.concatenate(
        [audio_features_mm_scaled, reduced_lyrics], axis=1
    )

    # Load MLP Classifier
    multimodal_classifier = build_mlp(
        input_dim=multimodal_features.shape[1], config=config
    )
    multimodal_classifier.load_model("models/mlp/mlp_best_multimodal.pth")
    multimodal_classifier.model.eval()

    # Run prediction
    mm_confidence, mm_prediction, mm_label, mm_probability = (
        multimodal_classifier.predict_single(multimodal_features.flatten())
    )

    multimodal_time = time.time() - multimodal_start
    print(f"[Predict] Multimodal prediction completed in {multimodal_time:.2f}s")

    # [2] Unimodal prediction (audio-only)
    print("[Predict] Running audio-only prediction...")
    audio_only_start = time.time()

    # 1.) Preprocess the audio
    audio_au = single_audio_preprocessing(audio_file)

    # 2.) Extract audio features
    audio_features_au = spectttra_predict(audio_au)
    audio_features_au = audio_features_au.reshape(1, -1)

    # 3.) Scale the vector using Z-Score
    audio_features_au_scaled = audio_instance_scaler(audio_features_au)

    # Load MLP Classifier
    audio_classifier = build_mlp(
        input_dim=audio_features_au_scaled.shape[1], config=config
    )
    audio_classifier.load_model("models/mlp/mlp_best_unimodal.pth")
    audio_classifier.model.eval()

    # Run prediction
    au_confidence, au_prediction, au_label, au_probability = (
        audio_classifier.predict_single(audio_features_au_scaled.flatten())
    )

    audio_only_time = time.time() - audio_only_start
    print(f"[Predict] Audio-only prediction completed in {audio_only_time:.2f}s")

    # Summary
    total_time = time.time() - start_time

    print("\n[Predict] Combined prediction completed!")
    print(f"[Predict] Multimodal: {multimodal_time:.2f}s")
    print(f"[Predict] Audio-only: {audio_only_time:.2f}s")
    print(f"[Predict] Total: {total_time:.2f}s")

    return {
        "multimodal": {
            "confidence": mm_confidence,
            "prediction": mm_prediction,
            "label": mm_label,
            "probability": mm_probability,
        },
        "audio_only": {
            "confidence": au_confidence,
            "prediction": au_prediction,
            "label": au_label,
            "probability": au_probability,
        },
        "performance": {
            "total_time_seconds": total_time,
            "multimodal_time_seconds": multimodal_time,
            "audio_only_time_seconds": audio_only_time,
        },
    }


if __name__ == "__main__":
    # Example usage (replace with real inputs, place song inside data/raw.)
    data = pd.read_csv("data/raw/predict_data_final.csv")

    result = []
    label = []
    for row in data.itertuples():
        prediction = predict_multimodal(row.song, row.lyrics)
        result.append(
            {
                "song": row.song,
                "label": row.label,
                "predicted_label": prediction["label"],
                "probability": prediction["probability"],
            }
        )

    for r in result:
        print(f"Song: {r['song']}")
        print(f"Actual Label: {r['label']}")
        print(f"Predicted: {r['predicted_label']}")
        print(f"Confidence: {r['probability']: .8f}%")
        print("-" * 50)