Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Identifiant du modèle sur Hugging Face
|
6 |
+
model_id = "ilyes25/wav2vec2-large-mms-1b-DZ"
|
7 |
+
|
8 |
+
# Détermine le device (GPU si disponible, sinon CPU)
|
9 |
+
device = 0 if torch.cuda.is_available() else -1
|
10 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
11 |
+
|
12 |
+
# Création du pipeline pour l'ASR
|
13 |
+
pipe = pipeline(
|
14 |
+
task="automatic-speech-recognition",
|
15 |
+
model=model_id,
|
16 |
+
device=device,
|
17 |
+
torch_dtype=torch_dtype,
|
18 |
+
framework="pt"
|
19 |
+
)
|
20 |
+
|
21 |
+
LANGUAGES = {
|
22 |
+
"Kabyle": "kab",
|
23 |
+
"Arabe": "ar",
|
24 |
+
"Français": "fr"
|
25 |
+
}
|
26 |
+
|
27 |
+
def transcribe_audio(audio_path, lang_name):
|
28 |
+
lang = LANGUAGES[lang_name]
|
29 |
+
pipe.model.load_adapter(lang)
|
30 |
+
pipe.tokenizer.set_target_lang(lang)
|
31 |
+
result = pipe(audio_path)
|
32 |
+
return result["text"].replace("</s>", "").replace("<s>", "").strip()
|
33 |
+
|
34 |
+
# Interface Gradio avec deux onglets (microphone et upload)
|
35 |
+
with gr.Blocks() as app:
|
36 |
+
gr.Markdown("## Transcription Speech-to-Text avec sélection de langue")
|
37 |
+
with gr.Tabs():
|
38 |
+
with gr.TabItem("Utiliser le Microphone"):
|
39 |
+
mic_input = gr.Audio(sources="microphone", type="filepath", label="Enregistrez votre audio")
|
40 |
+
lang_dropdown = gr.Dropdown(choices=list(LANGUAGES.keys()), label="Langue", value="Kabyle")
|
41 |
+
mic_output = gr.Textbox(label="Transcription")
|
42 |
+
mic_button = gr.Button("Transcrire")
|
43 |
+
mic_button.click(transcribe_audio, inputs=[mic_input, lang_dropdown], outputs=mic_output)
|
44 |
+
|
45 |
+
with gr.TabItem("Téléverser un Fichier Audio"):
|
46 |
+
file_input = gr.Audio(sources="upload", type="filepath", label="Téléversez votre audio")
|
47 |
+
lang_dropdown_file = gr.Dropdown(choices=list(LANGUAGES.keys()), label="Langue", value="Kabyle")
|
48 |
+
file_output = gr.Textbox(label="Transcription")
|
49 |
+
file_button = gr.Button("Transcrire")
|
50 |
+
file_button.click(transcribe_audio, inputs=[file_input, lang_dropdown_file], outputs=file_output)
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
app.launch()
|