Sandouno2003 commited on
Commit
9cc4dae
·
verified ·
1 Parent(s): d0c793c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importer les packages
2
+ import librosa # package utilisé dans le traitement d'audios
3
+ import IPython.display as ipd # Lire un audio
4
+
5
+ # importer gradio
6
+ import gradio as gr
7
+ from transformers import pipeline
8
+ # Importer nemo.collections.asr
9
+ import nemo.collections.asr as nemo_asr
10
+ # Instancier le modèle
11
+ asr_canary = nemo_asr.models.ASRModel.from_pretrained("nvidia/canary-1b-flash")
12
+ # Instanstier le modèle
13
+ asr_whisper = pipeline("automatic-speech-recognition", model="openai/whisper-small")
14
+
15
+ # Fonction de transcription whisper
16
+ def transcrire1(fpath):
17
+ output = asr_whisper(fpath)
18
+ return output["text"]
19
+
20
+ # Fonction de transcription canary-1b-flash
21
+ def transcrire2(fpath, source_lang, target_lang):
22
+ transcriptions = asr_canary.transcribe([fpath],
23
+ source_lang = source_lang, target_lang = target_lang)
24
+ text = transcriptions[0].text
25
+
26
+ return text
27
+
28
+ # Créer les blocs
29
+ demo = gr.Blocks(theme='JohnSmith9982/small_and_pretty')
30
+ # Créer un interface ASR whisper avec un microphone
31
+ mic_transcrire = gr.Interface(
32
+ fn=transcrire1,
33
+ inputs=gr.Audio(sources="microphone",
34
+ type="filepath"),
35
+ cache_examples=True,
36
+ outputs=gr.Textbox(label="Transcription",
37
+ lines=3),
38
+ title = 'Transcrire par microphone - Whisper')
39
+
40
+ # Créer un interface ASR whisper par audio
41
+ fich_transcrire = gr.Interface(
42
+ fn=transcrire1,
43
+ inputs=gr.Audio(sources="upload",
44
+ type="filepath"),
45
+ outputs=gr.Textbox(label="Transcription",
46
+ lines=3),
47
+ title = 'Transcrire un fichier audio - Whisper'
48
+ )
49
+
50
+ # Créer un interface ASR canary avec un microphone
51
+ mic_transcrire1 = gr.Interface(
52
+ fn=transcrire2,
53
+ inputs=[gr.Audio(sources="microphone",type="filepath"),
54
+ gr.Dropdown(choices = ['fr', 'en', 'de', 'es'], label ='Source languge'),
55
+ gr.Dropdown(choices = ['fr', 'en', 'de', 'es'], label = 'Target language')],
56
+ cache_examples=True,
57
+ outputs=gr.Textbox(label="Transcription",
58
+ lines=3),
59
+ title = 'Transcrire par microphone - Canary')
60
+
61
+ # Créer un interface ASR canary par audio
62
+ fich_transcrire1 = gr.Interface(
63
+ fn=transcrire2,
64
+ inputs=[gr.Audio(sources="upload",type="filepath"),
65
+ gr.Dropdown(choices = ['fr', 'en', 'de', 'es'], label ='Source languge'),
66
+ gr.Dropdown(choices = ['fr', 'en', 'de', 'es'], label ='Target language')],
67
+ outputs=gr.Textbox(label="Transcription",
68
+ lines=3),
69
+ title= 'Transcrire un fichier audio - Canary'
70
+ )
71
+
72
+ # Faire un tabbed des interfaces sur demo
73
+ with demo:
74
+ gr.TabbedInterface(
75
+ [mic_transcrire,
76
+ fich_transcrire,
77
+ mic_transcrire1,
78
+ fich_transcrire1],
79
+ ["Transcrire Microphone",
80
+ "Transcrire Audio",
81
+ "Transcrire Microphone",
82
+ "Transcrire Audio"],
83
+ )
84
+
85
+ demo.launch()