Tonic commited on
Commit
ad3bc83
·
1 Parent(s): 167c05e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4
+ from datasets import load_dataset
5
+ import soundfile as sf
6
+
7
+ # Check if CUDA is available and set the device
8
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
10
+
11
+ # Load the model and processor
12
+ model_id = "openai/whisper-large-v3"
13
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(
14
+ model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
15
+ ).to(device)
16
+ processor = AutoProcessor.from_pretrained(model_id)
17
+
18
+ # Define the ASR pipeline
19
+ asr_pipeline = pipeline(
20
+ "automatic-speech-recognition",
21
+ model=model,
22
+ tokenizer=processor.tokenizer,
23
+ feature_extractor=processor.feature_extractor,
24
+ max_new_tokens=128,
25
+ chunk_length_s=30,
26
+ batch_size=16,
27
+ return_timestamps=True,
28
+ torch_dtype=torch_dtype,
29
+ device=device,
30
+ )
31
+
32
+ # Function to process audio in chunks and return the combined text
33
+ def process_audio(file_info):
34
+ path = file_info["path"]
35
+ audio_stream = sf.SoundFile(path, 'r')
36
+ results = []
37
+ while True:
38
+ data = audio_stream.read(dtype='float32')
39
+ if len(data) == 0:
40
+ break
41
+ result = asr_pipeline(data)
42
+ results.append(result)
43
+ audio_stream.close()
44
+ combined_text = " ".join([r["text"] for r in results])
45
+ return combined_text
46
+
47
+ # Create the Gradio interface
48
+ iface = gr.Interface(
49
+ fn=process_audio,
50
+ inputs=gr.inputs.Audio(source="upload", type="file", label="Upload your audio file"),
51
+ outputs="text",
52
+ title="👋🏻Welcome To 🙋🏻‍♂️Patrick's Whisper🌬️",
53
+ description="Upload a large audio file to transcribe it into text using [Whisper3Large](https://huggingface.co/openai/whisper-large-v3) !",
54
+ )
55
+
56
+ # Launch the application
57
+ iface.launch()