HAMMALE commited on
Commit
aa1d421
·
verified ·
1 Parent(s): dd48b42

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import librosa
4
+ import numpy as np
5
+ from transformers import AutoProcessor, AutoModelForCTC
6
+
7
+ # Load model and processor
8
+ print("Loading model...")
9
+ processor = AutoProcessor.from_pretrained("HAMMALE/mms-darija-finetuned")
10
+ model = AutoModelForCTC.from_pretrained("HAMMALE/mms-darija-finetuned")
11
+
12
+ def transcribe_audio(audio_file):
13
+ try:
14
+ # Load audio
15
+ if audio_file is None:
16
+ return "Please upload an audio file."
17
+
18
+ # Load and preprocess audio
19
+ audio, sr = librosa.load(audio_file, sr=16000)
20
+
21
+ # Handle very short audio
22
+ if len(audio) < 1600: # Less than 0.1 seconds
23
+ return "Audio too short. Please upload a longer audio file."
24
+
25
+ # Process with model
26
+ inputs = processor(audio, sampling_rate=16000, return_tensors="pt")
27
+
28
+ # Inference
29
+ with torch.no_grad():
30
+ logits = model(**inputs).logits
31
+
32
+ predicted_ids = torch.argmax(logits, dim=-1)
33
+ transcription = processor.batch_decode(predicted_ids)[0]
34
+
35
+ return transcription if transcription.strip() else "No transcription generated."
36
+
37
+ except Exception as e:
38
+ return f"Error processing audio: {str(e)}"
39
+
40
+ # Create Gradio interface
41
+ demo = gr.Interface(
42
+ fn=transcribe_audio,
43
+ inputs=gr.Audio(type="filepath", label="Upload Darija Audio"),
44
+ outputs=gr.Textbox(label="Transcription", placeholder="Transcription will appear here..."),
45
+ title="🎤 Darija Speech Recognition",
46
+ description="Upload an audio file in Moroccan Arabic (Darija) and get the transcription. This model was fine-tuned on the Darija Bible dataset.",
47
+ article="Model: [HAMMALE/mms-darija-finetuned](https://huggingface.co/HAMMALE/mms-darija-finetuned)",
48
+ examples=[
49
+ # You can add example audio files here if you have them
50
+ ],
51
+ cache_examples=False,
52
+ theme=gr.themes.Soft()
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch()