File size: 1,459 Bytes
3367919 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import gradio as gr
from transformers import pipeline
import numpy as np
import os
from huggingface_hub import login
from scipy.io.wavfile import write
import uuid
access_token_read = os.environ.get('HF_TOKEN', None)
login(token = access_token_read)
os.mkdir(os.path.join("data", "hfcache"))
os.mkdir(os.path.join("data", "audio"))
os.mkdir(os.path.join("data", "audio_texts"))
os.environ["HF_HOME"] = os.path.join("data/hfcache")
transcriber = pipeline("automatic-speech-recognition", model='Simranjit/whisper-medical-french', device="cuda")
def transcribe(audio):
sr, y = audio
y = y.astype(np.float32)
y /= np.max(np.abs(y))
text = transcriber({"sampling_rate": sr, "raw": y})["text"]
text = text.replace("nouvelle ligne", "\n")
text = text.replace("à la ligne", "\n")
return text
def save_fn(audio, text):
sr, y = audio
y = y.astype(np.float32)
y /= np.max(np.abs(y))
uid = str(uuid.uuid4())
with open(os.path.join("data", "audio_texts", f"{uid}.txt"), "w", encoding="utf-8") as f:
f.write(text)
write(os.path.join("data", "audio", f"{uid}.wav"), sr, y)
return [None, ""]
with gr.Blocks() as demo:
audio = gr.Audio()
text = gr.TextArea(show_copy_button=True)
btn = gr.Button("run")
btn.click(fn=transcribe, inputs=audio, outputs=text)
save = gr.Button("save")
save.click(fn=save_fn, inputs=[audio, text], outputs=[audio, text])
demo.launch(share=True) |