Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
|
|
4 |
|
5 |
# Load Whisper model and processor from Hugging Face
|
6 |
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
7 |
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base").to("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
|
9 |
-
def transcribe(
|
10 |
try:
|
11 |
-
#
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
# Move to appropriate device
|
15 |
-
|
16 |
-
|
17 |
# Generate transcription
|
18 |
-
predicted_ids = model.generate(
|
19 |
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
20 |
|
21 |
return transcription
|
22 |
except Exception as e:
|
23 |
return f"Error: {str(e)}"
|
24 |
|
25 |
-
def convert_to_wav(audio_file_path):
|
26 |
-
try:
|
27 |
-
wav_file_path = os.path.splitext(audio_file_path)[0] + '.wav'
|
28 |
-
audio = AudioSegment.from_file(audio_file_path)
|
29 |
-
audio.export(wav_file_path, format='wav')
|
30 |
-
logging.info(f'Converted {audio_file_path} to {wav_file_path}')
|
31 |
-
return wav_file_path
|
32 |
-
except Exception as e:
|
33 |
-
logging.error(f'Error converting file to WAV: {e}')
|
34 |
-
raise
|
35 |
-
|
36 |
# Create a Gradio interface
|
37 |
iface = gr.Interface(
|
38 |
fn=transcribe,
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
4 |
+
import soundfile as sf
|
5 |
|
6 |
# Load Whisper model and processor from Hugging Face
|
7 |
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
8 |
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base").to("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
|
10 |
+
def transcribe(audio_path):
|
11 |
try:
|
12 |
+
# Read audio file
|
13 |
+
audio, sampling_rate = sf.read(audio_path)
|
14 |
+
|
15 |
+
# Process audio
|
16 |
+
inputs = processor(audio, sampling_rate=sampling_rate, return_tensors="pt").input_features
|
17 |
|
18 |
# Move to appropriate device
|
19 |
+
inputs = inputs.to(model.device)
|
20 |
+
|
21 |
# Generate transcription
|
22 |
+
predicted_ids = model.generate(inputs)
|
23 |
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
24 |
|
25 |
return transcription
|
26 |
except Exception as e:
|
27 |
return f"Error: {str(e)}"
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Create a Gradio interface
|
30 |
iface = gr.Interface(
|
31 |
fn=transcribe,
|