|
import gradio as gr |
|
from transformers import pipeline |
|
import os |
|
import numpy as np |
|
import torch |
|
|
|
|
|
print("Loading model...") |
|
model_id = "badrex/mms-300m-arabic-dialect-identifier" |
|
classifier = pipeline("audio-classification", model=model_id) |
|
print("Model loaded successfully") |
|
|
|
|
|
dialect_mapping = { |
|
"MSA": "Modern Standard Arabic", |
|
"Egyptian": "Egyptian Arabic", |
|
"Gulf": "Gulf Arabic", |
|
"Levantine": "Levantine Arabic", |
|
"Maghrebi": "Maghrebi Arabic" |
|
} |
|
|
|
def predict_dialect(audio): |
|
if audio is None: |
|
return {"Error": 1.0} |
|
|
|
|
|
sr, audio_array = audio |
|
|
|
|
|
if len(audio_array.shape) > 1: |
|
audio_array = audio_array.mean(axis=1) |
|
|
|
|
|
if audio_array.dtype != np.float32: |
|
|
|
if audio_array.dtype == np.int16: |
|
audio_array = audio_array.astype(np.float32) / 32768.0 |
|
else: |
|
audio_array = audio_array.astype(np.float32) |
|
|
|
print(f"Processing audio: sample rate={sr}, shape={audio_array.shape}") |
|
|
|
|
|
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 |
|
|
|
|
|
examples = [] |
|
examples_dir = "examples" |
|
if os.path.exists(examples_dir): |
|
for filename in os.listdir(examples_dir): |
|
if filename.endswith((".wav", ".mp3", ".ogg")): |
|
examples.append([os.path.join(examples_dir, filename)]) |
|
|
|
print(f"Found {len(examples)} example files") |
|
else: |
|
print("Examples directory not found") |
|
|
|
|
|
demo = gr.Interface( |
|
fn=predict_dialect, |
|
inputs=gr.Audio(), |
|
outputs=gr.Label(num_top_classes=5, label="Predicted Dialect"), |
|
title="ποΈ Arabic Dialect Identification in Speech!", |
|
description=""" |
|
Use this AI-powered tool to identify five major Arabic varieties from just a short audio clip: |
|
|
|
β¦ Modern Standard Arabic (MSA) - The formal language of media and education |
|
|
|
β¦ Egyptian Arabic - The dialect of Cairo, Alexandria, and popular Arabic cinema |
|
|
|
β¦ Gulf Arabic - Spoken across Saudi Arabia, UAE, Kuwait, Qatar, Bahrain, and Oman |
|
|
|
β¦ Levantine Arabic - The dialect of Syria, Lebanon, Jordan, and Palestine |
|
|
|
β¦ Maghrebi Arabic - The distinctive varieties of Morocco, Algeria, Tunisia, and Libya |
|
|
|
Simply **upload an audio file** or **record yourself speaking** to see which dialect you match! Perfect for language learners, linguistics enthusiasts, or anyone curious about Arabic language variation. |
|
|
|
The demo is based on a Transformer model adapted for the ADI task [badrex/mms-300m-arabic-dialect-identifier](https://huggingface.co/badrex/mms-300m-arabic-dialect-identifier). |
|
|
|
Developed with β€οΈπ€π by [Badr Alabsi](https://badrex.github.io/)""", |
|
examples=examples if examples else None, |
|
cache_examples=False, |
|
flagging_mode=None |
|
) |
|
|
|
|
|
demo.launch() |