Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,40 +3,34 @@ import os
|
|
| 3 |
import soundfile as sf
|
| 4 |
import torch
|
| 5 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 6 |
-
# Assuming you have your .env file configured with necessary API keys or configurations
|
| 7 |
-
# load_dotenv()
|
| 8 |
|
| 9 |
-
# Initialize the model outside the main app function to load it only once
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def transcribe_audio(audio_file):
|
| 17 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 18 |
-
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 19 |
-
|
| 20 |
-
model_id = "distil-whisper/distil-large-v2"
|
| 21 |
-
|
| 22 |
-
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 23 |
-
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 24 |
-
)
|
| 25 |
-
model.to(device)
|
| 26 |
-
|
| 27 |
-
processor = AutoProcessor.from_pretrained(model_id)
|
| 28 |
-
|
| 29 |
-
pipe = pipeline(
|
| 30 |
-
"automatic-speech-recognition",
|
| 31 |
-
model=model,
|
| 32 |
-
tokenizer=processor.tokenizer,
|
| 33 |
-
feature_extractor=processor.feature_extractor,
|
| 34 |
-
max_new_tokens=128,
|
| 35 |
-
chunk_length_s=15,
|
| 36 |
-
batch_size=16,
|
| 37 |
-
torch_dtype=torch_dtype,
|
| 38 |
-
device=device,
|
| 39 |
-
)
|
| 40 |
# Save the audio file to a temporary file
|
| 41 |
with open("temp_audio_file", "wb") as f:
|
| 42 |
f.write(audio_file.getbuffer())
|
|
|
|
| 3 |
import soundfile as sf
|
| 4 |
import torch
|
| 5 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
|
|
|
|
|
|
| 6 |
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 11 |
|
| 12 |
+
model_id = "distil-whisper/distil-large-v2"
|
| 13 |
|
| 14 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 15 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 16 |
+
)
|
| 17 |
+
model.to(device)
|
| 18 |
|
| 19 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 20 |
+
|
| 21 |
+
pipe = pipeline(
|
| 22 |
+
"automatic-speech-recognition",
|
| 23 |
+
model=model,
|
| 24 |
+
tokenizer=processor.tokenizer,
|
| 25 |
+
feature_extractor=processor.feature_extractor,
|
| 26 |
+
max_new_tokens=128,
|
| 27 |
+
chunk_length_s=15,
|
| 28 |
+
batch_size=16,
|
| 29 |
+
torch_dtype=torch_dtype,
|
| 30 |
+
device=device,
|
| 31 |
+
)
|
| 32 |
|
| 33 |
def transcribe_audio(audio_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Save the audio file to a temporary file
|
| 35 |
with open("temp_audio_file", "wb") as f:
|
| 36 |
f.write(audio_file.getbuffer())
|