DexterSptizu
commited on
Commit
•
5954031
1
Parent(s):
8c63955
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torchaudio
|
3 |
+
from transformers import AutoModelForSpeechSeq2Seq, PreTrainedTokenizerFast
|
4 |
+
|
5 |
+
def transcribe_audio(audio_path):
|
6 |
+
# Load and resample audio
|
7 |
+
audio, sr = torchaudio.load(audio_path)
|
8 |
+
if sr != 16000:
|
9 |
+
audio = torchaudio.functional.resample(audio, sr, 16000)
|
10 |
+
|
11 |
+
# Get transcription
|
12 |
+
tokens = model(audio)
|
13 |
+
transcription = tokenizer.decode(tokens[0], skip_special_tokens=True)
|
14 |
+
return transcription
|
15 |
+
|
16 |
+
# Load model and tokenizer globally
|
17 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained('usefulsensors/moonshine-tiny', trust_remote_code=True)
|
18 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained('usefulsensors/moonshine-tiny')
|
19 |
+
|
20 |
+
# Create Gradio interface
|
21 |
+
demo = gr.Blocks()
|
22 |
+
|
23 |
+
with demo:
|
24 |
+
gr.Markdown("## Audio Transcription App")
|
25 |
+
|
26 |
+
with gr.Tabs():
|
27 |
+
with gr.TabItem("Upload Audio"):
|
28 |
+
audio_file = gr.Audio(source="upload", type="filepath")
|
29 |
+
output_text1 = gr.Textbox(label="Transcription")
|
30 |
+
upload_button = gr.Button("Transcribe")
|
31 |
+
upload_button.click(fn=transcribe_audio, inputs=audio_file, outputs=output_text1)
|
32 |
+
|
33 |
+
with gr.TabItem("Record Audio"):
|
34 |
+
audio_mic = gr.Audio(source="microphone", type="filepath")
|
35 |
+
output_text2 = gr.Textbox(label="Transcription")
|
36 |
+
record_button = gr.Button("Transcribe")
|
37 |
+
record_button.click(fn=transcribe_audio, inputs=audio_mic, outputs=output_text2)
|
38 |
+
|
39 |
+
demo.launch()
|