Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,80 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
import
|
7 |
-
import
|
8 |
-
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
# Load
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# Load
|
19 |
-
tokenizer = AutoTokenizer.from_pretrained("ai4bharat/IndicBERTv2-MLM-only")
|
20 |
-
bert_model = TFAutoModel.from_pretrained("ai4bharat/IndicBERTv2-MLM-only"
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import librosa
|
6 |
+
import joblib
|
7 |
+
import tensorflow as tf
|
8 |
+
from keras.models import load_model
|
9 |
+
from transformers import AutoTokenizer, TFAutoModel
|
10 |
+
|
11 |
+
# ====================
|
12 |
+
# 1. Load Model and Assets
|
13 |
+
# ====================
|
14 |
+
model = load_model("raga_predictor_model.h5")
|
15 |
+
scaler = joblib.load("scaler.pkl")
|
16 |
+
encoder = joblib.load("label_encoder.pkl")
|
17 |
+
|
18 |
+
# Load tokenizer and BERT model directly from Hugging Face
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained("ai4bharat/IndicBERTv2-MLM-only")
|
20 |
+
bert_model = TFAutoModel.from_pretrained("ai4bharat/IndicBERTv2-MLM-only")
|
21 |
+
|
22 |
+
# Load metadata
|
23 |
+
meta = pd.read_csv("raga_metadata.csv")
|
24 |
+
raga_descriptions = dict(zip(meta['raga'], meta['description']))
|
25 |
+
|
26 |
+
# ====================
|
27 |
+
# 2. Define Utility Functions
|
28 |
+
# ====================
|
29 |
+
def extract_features(file_path):
|
30 |
+
y, sr = librosa.load(file_path, sr=22050)
|
31 |
+
features = {
|
32 |
+
"chroma_stft": np.mean(librosa.feature.chroma_stft(y=y, sr=sr)),
|
33 |
+
"spec_cent": np.mean(librosa.feature.spectral_centroid(y=y, sr=sr)),
|
34 |
+
}
|
35 |
+
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=18)
|
36 |
+
for i in range(18):
|
37 |
+
features[f"mfcc{i+1}"] = np.mean(mfccs[i])
|
38 |
+
return pd.DataFrame([features])
|
39 |
+
|
40 |
+
def tokenize_description(description_text):
|
41 |
+
desc_tok = tokenizer(description_text, padding=True, truncation=True, max_length=64, return_tensors="tf")
|
42 |
+
desc_embed = bert_model(desc_tok['input_ids'], attention_mask=desc_tok['attention_mask'])[0][:, 0, :]
|
43 |
+
return desc_embed
|
44 |
+
|
45 |
+
def predict_raga(audio_file):
|
46 |
+
# Extract features
|
47 |
+
audio_df = extract_features(audio_file.name)
|
48 |
+
audio_scaled = scaler.transform(audio_df)
|
49 |
+
audio_lstm_input = audio_scaled.reshape((1, 1, audio_scaled.shape[1]))
|
50 |
+
|
51 |
+
# Use a dummy description
|
52 |
+
description_text = ""
|
53 |
+
|
54 |
+
# Tokenize dummy description
|
55 |
+
desc_embed = tokenize_description([description_text])
|
56 |
+
|
57 |
+
# Predict
|
58 |
+
pred = model.predict([audio_lstm_input, desc_embed])
|
59 |
+
raga_pred = encoder.inverse_transform([np.argmax(pred)])[0]
|
60 |
+
|
61 |
+
# Get description
|
62 |
+
description = raga_descriptions.get(raga_pred, "No description available.")
|
63 |
+
|
64 |
+
return f"🎵 Predicted Raga: {raga_pred}\n\n📝 Description:\n{description}"
|
65 |
+
|
66 |
+
# ====================
|
67 |
+
# 3. Gradio Interface
|
68 |
+
# ====================
|
69 |
+
title = "🎶 Raga Prediction App"
|
70 |
+
description = "Upload an Indian classical music clip, and I will predict the Raga for you!"
|
71 |
+
|
72 |
+
interface = gr.Interface(
|
73 |
+
fn=predict_raga,
|
74 |
+
inputs=gr.Audio(type="file", label="Upload Audio File"),
|
75 |
+
outputs="text",
|
76 |
+
title=title,
|
77 |
+
description=description,
|
78 |
+
)
|
79 |
+
|
80 |
+
interface.launch()
|