|
import gradio as gr |
|
from transformers import pipeline |
|
import numpy as np |
|
|
|
|
|
model_id = "badrex/mms-300m-arabic-dialect-identifier" |
|
classifier = pipeline("audio-classification", model=model_id) |
|
|
|
|
|
dialect_mapping = { |
|
"MSA": "Modern Standard Arabic", |
|
"Egyptian": "Egyptian Arabic", |
|
"Gulf": "Gulf Arabic", |
|
"Levantine": "Levantine Arabic", |
|
"Maghrebi": "Maghrebi Arabic" |
|
} |
|
|
|
def predict_dialect(audio): |
|
|
|
if isinstance(audio, tuple) and len(audio) == 2: |
|
sr, audio_array = audio |
|
else: |
|
|
|
return {"Error": 1.0} |
|
|
|
|
|
if len(audio_array.shape) > 1: |
|
audio_array = audio_array.mean(axis=1) |
|
|
|
|
|
predictions = classifier({"sampling_rate": sr, "raw": audio_array}) |
|
|
|
|
|
results = {} |
|
for pred in predictions: |
|
dialect_name = dialect_mapping.get(pred['label'], pred['label']) |
|
results[dialect_name] = float(pred['score']) |
|
|
|
return results |
|
|
|
|
|
demo = gr.Interface( |
|
fn=predict_dialect, |
|
inputs=gr.Audio(), |
|
outputs=gr.Label(num_top_classes=5, label="Predicted Dialect"), |
|
title="Arabic Dialect Identifier", |
|
description="""This demo identifies Arabic dialects from speech audio. |
|
Upload an audio file or record your voice speaking Arabic to see which dialect it matches. |
|
The model identifies: Modern Standard Arabic (MSA), Egyptian, Gulf, Levantine, and Maghrebi dialects.""", |
|
examples=[ |
|
|
|
|
|
|
|
], |
|
allow_flagging="never" |
|
) |
|
|
|
|
|
demo.launch() |