salomonsky commited on
Commit
6beb61d
·
verified ·
1 Parent(s): 8307fd0

Create audio.py

Browse files
Files changed (1) hide show
  1. audio.py +49 -0
audio.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from pathlib import Path
4
+ from transformers import pipeline
5
+
6
+ model_id = "Sandiago21/whisper-large-v2-spanish"
7
+
8
+ cache_path = Path("~/.cache/huggingface/transformers") / model_id
9
+ if not cache_path.is_dir():
10
+ pipe = pipeline("automatic-speech-recognition", model=model_id)
11
+ else:
12
+ pipe = pipeline("automatic-speech-recognition", model=cache_path)
13
+
14
+ def transcribe_speech(filepath):
15
+ output = pipe(
16
+ filepath,
17
+ max_new_tokens=256,
18
+ generate_kwargs={
19
+ "task": "transcribe",
20
+ "language": "spanish",
21
+ },
22
+ chunk_length_s=30,
23
+ batch_size=8,
24
+ )
25
+ return output["text"]
26
+
27
+ demo = gr.Blocks()
28
+
29
+ mic_transcribe = gr.Interface(
30
+ fn=transcribe_speech,
31
+ inputs=gr.Audio(source="microphone", type="filepath"),
32
+ outputs=gr.outputs.Textbox(),
33
+
34
+ )
35
+
36
+ file_transcribe = gr.Interface(
37
+ fn=transcribe_speech,
38
+ inputs=gr.Audio(source="upload", type="filepath"),
39
+ outputs=gr.outputs.Textbox(),
40
+
41
+ )
42
+
43
+ with demo:
44
+ gr.TabbedInterface(
45
+ [mic_transcribe, file_transcribe],
46
+ ["Transcribe Microphone", "Transcribe Audio File"],
47
+ ),
48
+
49
+ demo.launch()