Pijush2023 commited on
Commit
2148c23
·
verified ·
1 Parent(s): 3d0f5ea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
5
+
6
+ model_id = 'openai/whisper-large-v3'
7
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
8
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
9
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
10
+ processor = AutoProcessor.from_pretrained(model_id)
11
+
12
+ pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True)
13
+
14
+ base_audio_drive = "/data/audio"
15
+
16
+ def transcribe_function(audio):
17
+ sr, y = audio[0], audio[1]
18
+ y = y.astype(np.float32) / np.max(np.abs(y))
19
+ result = pipe_asr({"array": y, "sampling_rate": sr}, return_timestamps=False)
20
+ full_text = result.get("text", "")
21
+ return full_text
22
+
23
+ def gradio_transcribe(audio):
24
+ sr, y = audio
25
+ return transcribe_function((sr, y))
26
+
27
+ iface = gr.Interface(fn=gradio_transcribe, inputs=gr.inputs.Audio(source="microphone", type="numpy"), outputs="text", title="Voice to Text Transcription", description="Transcribe your voice input to text using a pre-trained Whisper model.")
28
+
29
+ iface.launch()