Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import librosa
|
4 |
+
import onnxruntime as rt
|
5 |
+
from transformers import Pipeline
|
6 |
+
|
7 |
+
class MujawwadPipeline:
|
8 |
+
def __init__(self, model_path='model/mujawwad_classifier.onnx'):
|
9 |
+
self.session = rt.InferenceSession(model_path)
|
10 |
+
self.labels = ['bayati','hijaz','jiharkah','nihawand','rast','shoba','sikah']
|
11 |
+
self.descriptions = {
|
12 |
+
'bayati': 'Bayati (بياتي)',
|
13 |
+
'hijaz': 'Hijaz (حجاز)',
|
14 |
+
'jiharkah': 'Jiharkah (جهاركاه)',
|
15 |
+
'nihawand': 'Nihawand (نهاوند)',
|
16 |
+
'rast': 'Rast (راست)',
|
17 |
+
'shoba': 'Shoba (صبا)',
|
18 |
+
'sikah': 'Sikah (سيكاه)'
|
19 |
+
}
|
20 |
+
|
21 |
+
def predict(self, audio_path):
|
22 |
+
try:
|
23 |
+
input_data = self.preprocess(audio_path)
|
24 |
+
input_name = self.session.get_inputs()[0].name
|
25 |
+
output_name = self.session.get_outputs()[0].name
|
26 |
+
predictions = self.session.run([output_name], {input_name: input_data.astype(np.float32)})[0]
|
27 |
+
|
28 |
+
# Format results with descriptions
|
29 |
+
results = {self.descriptions[self.labels[i]]: float(predictions[0][i])
|
30 |
+
for i in range(len(self.labels))}
|
31 |
+
return results
|
32 |
+
except Exception as e:
|
33 |
+
return {"Error": str(e)}
|
34 |
+
|
35 |
+
def classify_audio(audio_path):
|
36 |
+
classifier = MujawwadPipeline()
|
37 |
+
result = classifier.predict(audio_path)
|
38 |
+
return result
|
39 |
+
|
40 |
+
# Create Gradio interface
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=classify_audio,
|
43 |
+
inputs=gr.Audio(type="filepath", label="Upload Quranic Recitation"),
|
44 |
+
outputs=gr.Label(num_top_classes=7),
|
45 |
+
title="Maqamat Classification Model",
|
46 |
+
description="Classify Quranic recitation maqamat into 7 traditional Arabic melodic modes",
|
47 |
+
examples=[["examples/bayati_example.mp3"], ["examples/hijaz_example.mp3"]]
|
48 |
+
)
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
iface.launch()
|