Spaces:
Running
on
Zero
Running
on
Zero
Initial commit for Arabic Dialect ID demo
Browse files- app.py +52 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load the model
|
| 6 |
+
model_id = "badrex/mms-300m-arabic-dialect-identifier" # Replace with your model ID
|
| 7 |
+
classifier = pipeline("audio-classification", model=model_id)
|
| 8 |
+
|
| 9 |
+
# Define dialect names for better display
|
| 10 |
+
dialect_mapping = {
|
| 11 |
+
"MSA": "Modern Standard Arabic",
|
| 12 |
+
"Egyptian": "Egyptian Arabic",
|
| 13 |
+
"Gulf": "Gulf Arabic",
|
| 14 |
+
"Levantine": "Levantine Arabic",
|
| 15 |
+
"Maghrebi": "Maghrebi Arabic"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def predict_dialect(audio, sr):
|
| 19 |
+
# Process the audio input
|
| 20 |
+
if len(audio.shape) > 1:
|
| 21 |
+
audio = audio.mean(axis=1) # Convert stereo to mono
|
| 22 |
+
|
| 23 |
+
# Classify the dialect
|
| 24 |
+
predictions = classifier({"sampling_rate": sr, "raw": audio})
|
| 25 |
+
|
| 26 |
+
# Format results for display
|
| 27 |
+
results = {}
|
| 28 |
+
for pred in predictions:
|
| 29 |
+
dialect_name = dialect_mapping.get(pred['label'], pred['label'])
|
| 30 |
+
results[dialect_name] = float(pred['score'])
|
| 31 |
+
|
| 32 |
+
return results
|
| 33 |
+
|
| 34 |
+
# Create the Gradio interface
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=predict_dialect,
|
| 37 |
+
inputs=gr.Audio(type="numpy", label="Upload or Record Audio"),
|
| 38 |
+
outputs=gr.Label(num_top_classes=5, label="Predicted Dialect"),
|
| 39 |
+
title="Arabic Dialect Identifier",
|
| 40 |
+
description="""This demo identifies Arabic dialects from speech audio.
|
| 41 |
+
Upload an audio file or record your voice speaking Arabic to see which dialect it matches.
|
| 42 |
+
The model identifies: Modern Standard Arabic (MSA), Egyptian, Gulf, Levantine, and Maghrebi dialects.""",
|
| 43 |
+
examples=[
|
| 44 |
+
# Optional: Add example audio files here if you have them
|
| 45 |
+
# ["examples/msa_example.wav"],
|
| 46 |
+
# ["examples/egyptian_example.wav"],
|
| 47 |
+
],
|
| 48 |
+
allow_flagging="never"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Launch the app
|
| 52 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.50.2
|
| 2 |
+
transformers>=4.36.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
librosa>=0.10.1
|