bilalfaye commited on
Commit
960a3ac
·
verified ·
1 Parent(s): 1ea7b59

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torchaudio
3
+ from transformers import pipeline
4
+ import torch
5
+ from datasets import load_dataset
6
+
7
+ # Modèle 1 : Transcription audio Wolof -> texte Wolof
8
+ pipe_wolof = pipeline(
9
+ task="automatic-speech-recognition",
10
+ model="bilalfaye/wav2vec2-large-mms-1b-wolof",
11
+ processor="bilalfaye/wav2vec2-large-mms-1b-wolof",
12
+ device="cuda" if torch.cuda.is_available() else "cpu"
13
+ )
14
+
15
+ # Fonction 1 : Transcription audio Wolof -> texte Wolof
16
+ def transcribe_audio_wolof(audio):
17
+ # Charger l'audio avec torchaudio
18
+ waveform, sample_rate = torchaudio.load(audio)
19
+
20
+ # Convertir stéréo en mono
21
+ if waveform.shape[0] > 1:
22
+ mono_audio = waveform.mean(dim=0, keepdim=True)
23
+ else:
24
+ mono_audio = waveform
25
+
26
+ # Rééchantillonner à 16 kHz si nécessaire
27
+ if sample_rate != 16000:
28
+ resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
29
+ mono_audio = resampler(mono_audio)
30
+ sample_rate = 16000
31
+
32
+ # Convertir en tableau numpy
33
+ mono_audio = mono_audio.squeeze(0).numpy()
34
+
35
+ # Transcrire l'audio
36
+ result = pipe_wolof({"array": mono_audio, "sampling_rate": sample_rate})
37
+ return result['text']
38
+
39
+ # Modèle 2 : Texte Wolof -> audio Wolof
40
+ synthesiser_wolof = pipeline("text-to-speech", "bilalfaye/speecht5_tts-wolof")
41
+
42
+ # Charger les embeddings pour les voix masculine et féminine
43
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
44
+ speaker_embedding_male = torch.tensor(embeddings_dataset[0]["xvector"]).unsqueeze(0)
45
+ speaker_embedding_female = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
46
+
47
+
48
+
49
+ # Fonction 2 : Texte Wolof -> audio Wolof
50
+ def text_to_speech_wolof(text, voice_type):
51
+ embedding = speaker_embedding_male if voice_type == "Male" else speaker_embedding_female
52
+ speech = synthesiser_wolof(text, forward_params={"speaker_embeddings": embedding})
53
+ return speech["sampling_rate"], speech["audio"]
54
+
55
+ # Interface Gradio
56
+ with gr.Blocks() as app:
57
+ with gr.Tab("Transcription Audio -> Texte"):
58
+ gr.Markdown("### Transcription audio Wolof vers texte")
59
+ audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Enregistrer ou importer un fichier audio")
60
+ transcription_output = gr.Textbox(label="Texte transcrit")
61
+ transcribe_button = gr.Button("Transcrire")
62
+ transcribe_button.click(transcribe_audio_wolof, inputs=audio_input, outputs=transcription_output)
63
+
64
+ with gr.Tab("Texte -> Synthèse Vocale"):
65
+ gr.Markdown("### Conversion de texte Wolof en audio")
66
+ text_input = gr.Textbox(label="Entrez du texte en Wolof")
67
+ voice_selector = gr.Radio(["Male", "Female"], label="Type de voix", value="Male")
68
+ audio_output = gr.Audio(label="Synthèse vocale")
69
+ synthesize_button = gr.Button("Synthétiser")
70
+ synthesize_button.click(text_to_speech_wolof, inputs=[text_input, voice_selector], outputs=audio_output)
71
+
72
+ # Lancer l'application
73
+ app.launch(debug=True, share=True)