Spaces:
Running
Running
app.py
CHANGED
@@ -1,44 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
import librosa
|
3 |
import torch
|
4 |
-
from transformers import
|
5 |
-
|
6 |
-
# Load
|
7 |
-
|
8 |
-
processor = Wav2Vec2Processor.from_pretrained(
|
9 |
-
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
input_values = processor(input_audio, return_tensors="pt", padding=True).input_values
|
19 |
-
|
20 |
-
# Get the model's logits
|
21 |
logits = model(input_values).logits
|
22 |
-
|
23 |
-
#
|
24 |
tokens = torch.argmax(logits, axis=-1)
|
25 |
-
|
26 |
-
# Decode the tokens
|
27 |
-
transcription =
|
28 |
-
|
29 |
-
return transcription
|
30 |
-
|
31 |
-
# Create
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
],
|
38 |
-
outputs="text", # Output will be a transcription text
|
39 |
-
title="Moroccan Darija Speech-to-Text", # Interface title
|
40 |
-
description="Upload an audio file or record audio directly from your microphone to transcribe it into Moroccan Darija."
|
41 |
-
)
|
42 |
-
|
43 |
-
# Launch the interface
|
44 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import librosa
|
3 |
import torch
|
4 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
5 |
+
|
6 |
+
# Load pre-trained model and processor directly from Hugging Face Hub
|
7 |
+
model = Wav2Vec2ForCTC.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
8 |
+
processor = Wav2Vec2Processor.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
9 |
+
|
10 |
+
def transcribe_audio(audio):
|
11 |
+
# Load the audio file from Gradio interface
|
12 |
+
audio_array, sr = librosa.load(audio, sr=16000)
|
13 |
+
|
14 |
+
# Tokenize the audio file
|
15 |
+
input_values = processor(audio_array, return_tensors="pt", padding=True).input_values
|
16 |
+
|
17 |
+
# Get the model's logits (predicted token scores)
|
|
|
|
|
|
|
18 |
logits = model(input_values).logits
|
19 |
+
|
20 |
+
# Get the predicted tokens
|
21 |
tokens = torch.argmax(logits, axis=-1)
|
22 |
+
|
23 |
+
# Decode the tokens into text
|
24 |
+
transcription = processor.decode(tokens[0])
|
25 |
+
|
26 |
+
return transcription
|
27 |
+
|
28 |
+
# Create a Gradio interface for uploading audio or recording from the browser
|
29 |
+
demo = gr.Interface(fn=transcribe_audio,
|
30 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
31 |
+
outputs="text")
|
32 |
+
|
33 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|