diak24 commited on
Commit
864e62b
·
verified ·
1 Parent(s): cd043df

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Créer les blocs
24
+ demo = gr.Blocks(theme='JohnSmith9982/small_and_pretty')
25
+ # Créer un interface ASR whisper avec un microphone
26
+ mic_transcrire = gr.Interface(
27
+ fn=transcrire1,
28
+ inputs=gr.Audio(sources="microphone",
29
+ type="filepath"),
30
+ cache_examples=True,
31
+ outputs=gr.Textbox(label="Transcription",
32
+ lines=3),
33
+ title = 'Transcrire par microphone - Whisper')
34
+
35
+ # Créer un interface ASR whisper par audio
36
+ fich_transcrire = gr.Interface(
37
+ fn=transcrire1,
38
+ inputs=gr.Audio(sources="upload",
39
+ type="filepath"),
40
+ outputs=gr.Textbox(label="Transcription",
41
+ lines=3),
42
+ title = 'Transcrire un fichier audio - Whisper'
43
+ )
44
+ # Créer un interface ASR canary avec un microphone
45
+ mic_transcrire1 = gr.Interface(
46
+ fn=transcrire2,
47
+ inputs=[gr.Audio(sources="microphone",type="filepath"),
48
+ gr.Dropdown(choices = ['fr', 'en'], label ='Source languge'),
49
+ gr.Dropdown(choices = ['fr', 'en'], label = 'Target language')],
50
+ cache_examples=True,
51
+ outputs=gr.Textbox(label="Transcription",
52
+ lines=3),
53
+ title = 'Transcrire par microphone - Canary')
54
+
55
+ # Créer un interface ASR canary par audio
56
+ fich_transcrire1 = gr.Interface(
57
+ fn=transcrire2,
58
+ inputs=[gr.Audio(sources="upload",type="filepath"),
59
+ gr.Dropdown(choices = ['fr', 'en'], label ='Source languge'),
60
+ gr.Dropdown(choices = ['fr', 'en'], label ='Target language')],
61
+ outputs=gr.Textbox(label="Transcription",
62
+ lines=3),
63
+ title= 'Transcrire un fichier audio - Canary'
64
+ )
65
+ # Faire un tabbed des interfaces sur demo
66
+ with demo:
67
+ gr.TabbedInterface(
68
+ [mic_transcrire,
69
+ fich_transcrire,
70
+ mic_transcrire1,
71
+ fich_transcrire1],
72
+ ["Transcrire Microphone",
73
+ "Transcrire Audio",
74
+ "Transcrire Microphone",
75
+ "Transcrire Audio"],
76
+ )
77
+
78
+ demo.launch()