moustaphasidibe commited on
Commit
b59bc30
·
verified ·
1 Parent(s): 830581a

Create app.py

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