Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Créer un interface ASR canary avec un microphone
|
| 47 |
+
mic_transcrire1 = gr.Interface(
|
| 48 |
+
fn=transcrire2,
|
| 49 |
+
inputs=[gr.Audio(sources="microphone",type="filepath"),
|
| 50 |
+
gr.Dropdown(choices = ['fr', 'en'], label ='Source languge'),
|
| 51 |
+
gr.Dropdown(choices = ['fr', 'en'], label = 'Target language')],
|
| 52 |
+
cache_examples=True,
|
| 53 |
+
outputs=gr.Textbox(label="Transcription",
|
| 54 |
+
lines=3),
|
| 55 |
+
title = 'Transcrire par microphone - Canary')
|
| 56 |
+
|
| 57 |
+
# Créer un interface ASR canary par audio
|
| 58 |
+
fich_transcrire1 = gr.Interface(
|
| 59 |
+
fn=transcrire2,
|
| 60 |
+
inputs=[gr.Audio(sources="upload",type="filepath"),
|
| 61 |
+
gr.Dropdown(choices = ['fr', 'en'], label ='Source languge'),
|
| 62 |
+
gr.Dropdown(choices = ['fr', 'en'], label ='Target language')],
|
| 63 |
+
outputs=gr.Textbox(label="Transcription",
|
| 64 |
+
lines=3),
|
| 65 |
+
title= 'Transcrire un fichier audio - Canary'
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Faire un tabbed des interfaces sur demo
|
| 69 |
+
with demo:
|
| 70 |
+
gr.TabbedInterface(
|
| 71 |
+
[mic_transcrire,
|
| 72 |
+
fich_transcrire,
|
| 73 |
+
mic_transcrire1,
|
| 74 |
+
fich_transcrire1],
|
| 75 |
+
["Transcrire Microphone",
|
| 76 |
+
"Transcrire Audio",
|
| 77 |
+
"Transcrire Microphone",
|
| 78 |
+
"Transcrire Audio"],
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
demo.launch()
|