File size: 1,902 Bytes
e69d597
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import librosa
import onnxruntime as rt
from transformers import Pipeline

class MujawwadPipeline:
    def __init__(self, model_path='model/mujawwad_classifier.onnx'):
        self.session = rt.InferenceSession(model_path)
        self.labels = ['bayati','hijaz','jiharkah','nihawand','rast','shoba','sikah']
        self.descriptions = {
            'bayati': 'Bayati (بياتي)',
            'hijaz': 'Hijaz (حجاز)',
            'jiharkah': 'Jiharkah (جهاركاه)',
            'nihawand': 'Nihawand (نهاوند)',
            'rast': 'Rast (راست)',
            'shoba': 'Shoba (صبا)',
            'sikah': 'Sikah (سيكاه)'
        }

    def predict(self, audio_path):
        try:
            input_data = self.preprocess(audio_path)
            input_name = self.session.get_inputs()[0].name
            output_name = self.session.get_outputs()[0].name
            predictions = self.session.run([output_name], {input_name: input_data.astype(np.float32)})[0]
            
            # Format results with descriptions
            results = {self.descriptions[self.labels[i]]: float(predictions[0][i]) 
                      for i in range(len(self.labels))}
            return results
        except Exception as e:
            return {"Error": str(e)}

def classify_audio(audio_path):
    classifier = MujawwadPipeline()
    result = classifier.predict(audio_path)
    return result

# Create Gradio interface
iface = gr.Interface(
    fn=classify_audio,
    inputs=gr.Audio(type="filepath", label="Upload Quranic Recitation"),
    outputs=gr.Label(num_top_classes=7),
    title="Maqamat Classification Model",
    description="Classify Quranic recitation maqamat into 7 traditional Arabic melodic modes",
    examples=[["examples/bayati_example.mp3"], ["examples/hijaz_example.mp3"]]
)

if __name__ == "__main__":
    iface.launch()