badrex commited on
Commit
23545c8
·
1 Parent(s): dbaaa4a

add examples and update requirements.txt

Browse files
app.py CHANGED
@@ -1,12 +1,18 @@
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",
@@ -16,43 +22,66 @@ dialect_mapping = {
16
  }
17
 
18
  def predict_dialect(audio):
19
- # The audio input from Gradio is a tuple of (sample_rate, audio_array)
20
- if isinstance(audio, tuple) and len(audio) == 2:
 
 
 
21
  sr, audio_array = audio
22
- else:
23
- # Handle error case
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  return {"Error": 1.0}
25
-
26
- # Process the audio input
27
- if len(audio_array.shape) > 1:
28
- audio_array = audio_array.mean(axis=1) # Convert stereo to mono
29
-
30
- # Classify the dialect
31
- predictions = classifier({"sampling_rate": sr, "raw": audio_array})
32
-
33
- # Format results for display
34
- results = {}
35
- for pred in predictions:
36
- dialect_name = dialect_mapping.get(pred['label'], pred['label'])
37
- results[dialect_name] = float(pred['score'])
38
-
39
- return results
 
 
 
 
 
 
40
 
41
  # Create the Gradio interface
42
  demo = gr.Interface(
43
  fn=predict_dialect,
44
- inputs=gr.Audio(), # Simplified audio input
45
  outputs=gr.Label(num_top_classes=5, label="Predicted Dialect"),
46
  title="Arabic Dialect Identifier",
47
  description="""This demo identifies Arabic dialects from speech audio.
48
  Upload an audio file or record your voice speaking Arabic to see which dialect it matches.
49
  The model identifies: Modern Standard Arabic (MSA), Egyptian, Gulf, Levantine, and Maghrebi dialects.""",
50
- examples=[
51
- # Optional: Add example audio files here if you have them
52
- # ["examples/msa_example.wav"],
53
- # ["examples/egyptian_example.wav"],
54
- ],
55
- allow_flagging="never"
56
  )
57
 
58
  # Launch the app
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  import numpy as np
4
+ import os
5
 
6
  # Load the model
7
+ print("Loading model...")
8
+ model_id = "badrex/mms-300m-arabic-dialect-identifier"
9
+ try:
10
+ classifier = pipeline("audio-classification", model=model_id)
11
+ print("Model loaded successfully")
12
+ except Exception as e:
13
+ print(f"Error loading model: {e}")
14
+
15
+ # Define dialect mapping
16
  dialect_mapping = {
17
  "MSA": "Modern Standard Arabic",
18
  "Egyptian": "Egyptian Arabic",
 
22
  }
23
 
24
  def predict_dialect(audio):
25
+ try:
26
+ # The audio input from Gradio is a tuple of (sample_rate, audio_array)
27
+ if audio is None:
28
+ return {"Error": 1.0}
29
+
30
  sr, audio_array = audio
31
+
32
+ # Process the audio input
33
+ if len(audio_array.shape) > 1:
34
+ audio_array = audio_array.mean(axis=1) # Convert stereo to mono
35
+
36
+ print(f"Processing audio: sample rate={sr}, shape={audio_array.shape}")
37
+
38
+ # Classify the dialect
39
+ predictions = classifier({"sampling_rate": sr, "raw": audio_array})
40
+
41
+ # Format results for display
42
+ results = {}
43
+ for pred in predictions:
44
+ dialect_name = dialect_mapping.get(pred['label'], pred['label'])
45
+ results[dialect_name] = float(pred['score'])
46
+
47
+ return results
48
+ except Exception as e:
49
+ print(f"Error in prediction: {e}")
50
  return {"Error": 1.0}
51
+
52
+ # Find example files
53
+ example_files = []
54
+ examples_dir = "examples"
55
+ if os.path.exists(examples_dir):
56
+ for filename in os.listdir(examples_dir):
57
+ if filename.endswith((".wav", ".mp3", ".ogg")):
58
+ example_files.append(os.path.join(examples_dir, filename))
59
+
60
+ print(f"Found {len(example_files)} example files")
61
+ else:
62
+ print("Examples directory not found")
63
+
64
+ # Examples with labels
65
+ examples = []
66
+ if example_files:
67
+ for file in example_files:
68
+ basename = os.path.basename(file)
69
+ dialect = basename.split("_")[0] if "_" in basename else basename.split(".")[0]
70
+ label = dialect_mapping.get(dialect, dialect.capitalize())
71
+ examples.append([file, f"{label} Sample"])
72
 
73
  # Create the Gradio interface
74
  demo = gr.Interface(
75
  fn=predict_dialect,
76
+ inputs=gr.Audio(),
77
  outputs=gr.Label(num_top_classes=5, label="Predicted Dialect"),
78
  title="Arabic Dialect Identifier",
79
  description="""This demo identifies Arabic dialects from speech audio.
80
  Upload an audio file or record your voice speaking Arabic to see which dialect it matches.
81
  The model identifies: Modern Standard Arabic (MSA), Egyptian, Gulf, Levantine, and Maghrebi dialects.""",
82
+ examples=examples if examples else None,
83
+ examples_per_page=5,
84
+ flagging_mode=None # Updated from allow_flagging
 
 
 
85
  )
86
 
87
  # Launch the app
examples/07sQYGJjXp.mp3 ADDED
Binary file (19.7 kB). View file
 
examples/0Dx4G69NT1.mp3 ADDED
Binary file (56.1 kB). View file
 
examples/0EklRxI2r7.mp3 ADDED
Binary file (25.1 kB). View file
 
examples/0lhKyMVanh.mp3 ADDED
Binary file (27.7 kB). View file
 
examples/3EeaoDRPEd.mp3 ADDED
Binary file (36.9 kB). View file
 
examples/3Fi3cisTLe.mp3 ADDED
Binary file (45.2 kB). View file
 
examples/3bEPdiqxC7.mp3 ADDED
Binary file (39.7 kB). View file
 
examples/3zRKfpKl0Y.mp3 ADDED
Binary file (43.5 kB). View file
 
examples/56S1DlxtmW.mp3 ADDED
Binary file (48.3 kB). View file
 
examples/5jehSBK6Pg.mp3 ADDED
Binary file (26.5 kB). View file
 
examples/5q7nveNeiy.mp3 ADDED
Binary file (69.6 kB). View file
 
examples/67ub2dQVtY.mp3 ADDED
Binary file (27.2 kB). View file
 
examples/68jlcwwZNQ.mp3 ADDED
Binary file (92.8 kB). View file
 
examples/6D3IzKfPsg.mp3 ADDED
Binary file (33.4 kB). View file
 
examples/6fDC05Y789.mp3 ADDED
Binary file (35.2 kB). View file
 
examples/6tClWfytMf.mp3 ADDED
Binary file (54.5 kB). View file
 
examples/7hJUpVTD91.mp3 ADDED
Binary file (25 kB). View file
 
examples/9XiCrJdINc.mp3 ADDED
Binary file (53.3 kB). View file
 
examples/AICriJDIrA.mp3 ADDED
Binary file (59.2 kB). View file
 
examples/AT2Oo9AjZw.mp3 ADDED
Binary file (42.6 kB). View file
 
examples/BPyRgUKYav.mp3 ADDED
Binary file (38.5 kB). View file
 
examples/CbrP4lagnL.mp3 ADDED
Binary file (51.2 kB). View file
 
examples/CmBBDRsOVU.mp3 ADDED
Binary file (59.9 kB). View file
 
examples/FFAKzxhVgC.mp3 ADDED
Binary file (40.6 kB). View file
 
examples/Lionel-Messi_(arywiki)-2.mp3 ADDED
Binary file (94.5 kB). View file
 
examples/PDJbpexQFE.mp3 ADDED
Binary file (44.3 kB). View file
 
examples/bsBqTDHUgx.mp3 ADDED
Binary file (50 kB). View file
 
examples/bywrMXKv1a.mp3 ADDED
Binary file (94.4 kB). View file
 
examples/cLOEsibJJW.mp3 ADDED
Binary file (99.1 kB). View file
 
examples/fDyks4ZZsU.mp3 ADDED
Binary file (19.5 kB). View file
 
examples/gksGGsLoAq.mp3 ADDED
Binary file (290 kB). View file
 
examples/obud3p5tvb.mp3 ADDED
Binary file (31.6 kB). View file
 
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
- gradio>=3.50.2
2
  transformers>=4.36.0
3
  torch>=2.0.0
4
- librosa>=0.10.1
 
 
1
+ gradio>=5.20.0
2
  transformers>=4.36.0
3
  torch>=2.0.0
4
+ torchaudio>=2.0.0
5
+ librosa>=0.10.1