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