Spaces:
Runtime error
Runtime error
Update transcribe.py
Browse files- transcribe.py +84 -83
transcribe.py
CHANGED
|
@@ -1,84 +1,85 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
import numpy as np
|
| 4 |
-
import torch
|
| 5 |
-
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, WhisperFeatureExtractor
|
| 6 |
-
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 7 |
-
import nltk
|
| 8 |
-
nltk.download('punkt', quiet=True)
|
| 9 |
-
from nltk.tokenize import sent_tokenize
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
input_features =
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
current_time
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
return f"{int(h):02d}:{int(m):02d}:{s:06.3f}".replace('.', ',')
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, WhisperFeatureExtractor
|
| 6 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 7 |
+
import nltk
|
| 8 |
+
nltk.download('punkt', quiet=True)
|
| 9 |
+
from nltk.tokenize import sent_tokenize
|
| 10 |
+
|
| 11 |
+
@spaces.GPU(duration=300)
|
| 12 |
+
|
| 13 |
+
def transcribe(video_file, transcribe_to_text=True, transcribe_to_srt=True, target_language='en'):
|
| 14 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 16 |
+
|
| 17 |
+
model_id = "openai/whisper-large-v3"
|
| 18 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 19 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 20 |
+
)
|
| 21 |
+
model.to(device)
|
| 22 |
+
|
| 23 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 24 |
+
feature_extractor = WhisperFeatureExtractor.from_pretrained(model_id)
|
| 25 |
+
|
| 26 |
+
video = VideoFileClip(video_file)
|
| 27 |
+
audio = video.audio
|
| 28 |
+
duration = audio.duration
|
| 29 |
+
chunk_duration = 60
|
| 30 |
+
n_chunks = int(np.ceil(duration / chunk_duration))
|
| 31 |
+
|
| 32 |
+
full_transcription = ""
|
| 33 |
+
for i in range(n_chunks):
|
| 34 |
+
start_time = i * chunk_duration
|
| 35 |
+
end_time = min((i + 1) * chunk_duration, duration)
|
| 36 |
+
|
| 37 |
+
audio_chunk = audio.subclip(start_time, end_time)
|
| 38 |
+
temp_file_path = f"temp_audio_chunk_{i}.wav"
|
| 39 |
+
audio_chunk.write_audiofile(temp_file_path, codec='pcm_s16le')
|
| 40 |
+
|
| 41 |
+
sound_array = AudioFileClip(temp_file_path).to_soundarray(fps=16000)
|
| 42 |
+
if sound_array.ndim > 1:
|
| 43 |
+
sound_array = np.mean(sound_array, axis=1)
|
| 44 |
+
|
| 45 |
+
input_features = feature_extractor(sound_array, sampling_rate=16000, return_tensors="pt").input_features
|
| 46 |
+
input_features = input_features.to(device=device, dtype=torch_dtype)
|
| 47 |
+
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
if target_language:
|
| 50 |
+
model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language=target_language,
|
| 51 |
+
task="transcribe")
|
| 52 |
+
generated_ids = model.generate(input_features, max_length=448)
|
| 53 |
+
transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 54 |
+
|
| 55 |
+
full_transcription += transcription + " "
|
| 56 |
+
os.remove(temp_file_path)
|
| 57 |
+
print(f"Processed chunk {i + 1}/{n_chunks}")
|
| 58 |
+
|
| 59 |
+
# Split the transcription into sentences
|
| 60 |
+
sentences = sent_tokenize(full_transcription.strip())
|
| 61 |
+
|
| 62 |
+
# Estimate time for each sentence based on its length relative to the total transcription
|
| 63 |
+
total_chars = sum(len(s) for s in sentences)
|
| 64 |
+
sentence_times = []
|
| 65 |
+
current_time = 0
|
| 66 |
+
for sentence in sentences:
|
| 67 |
+
sentence_duration = (len(sentence) / total_chars) * duration
|
| 68 |
+
sentence_times.append((current_time, current_time + sentence_duration))
|
| 69 |
+
current_time += sentence_duration
|
| 70 |
+
|
| 71 |
+
output = ""
|
| 72 |
+
if transcribe_to_text:
|
| 73 |
+
output += "Text Transcription:\n" + full_transcription + "\n\n"
|
| 74 |
+
|
| 75 |
+
if transcribe_to_srt:
|
| 76 |
+
output += "SRT Transcription:\n"
|
| 77 |
+
for i, (sentence, (start, end)) in enumerate(zip(sentences, sentence_times), 1):
|
| 78 |
+
output += f"{i}\n{format_time(start)} --> {format_time(end)}\n{sentence}\n\n"
|
| 79 |
+
|
| 80 |
+
return output
|
| 81 |
+
|
| 82 |
+
def format_time(seconds):
|
| 83 |
+
m, s = divmod(seconds, 60)
|
| 84 |
+
h, m = divmod(m, 60)
|
| 85 |
return f"{int(h):02d}:{int(m):02d}:{s:06.3f}".replace('.', ',')
|