i want to run fast-whisper on google colab but at the end of the running, it don't…
- User
i want to run fast-whisper on google colab but at the end of the running, it don't automatically save the output in srt and txt. can you add the code? here's the code: from faster_whisper import WhisperModel
model_size = "large-v2"
Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")
or run on GPU with INT8
model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
or run on CPU with INT8
model = WhisperModel(model_size, device="cpu", compute_type="int8")
segments, info = model.transcribe("audio.mp3", beam_size=5)
print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
for segment in segments: print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
- Assistant
Sure, here's an updated code that saves the output in SRT and TXT format:
from faster_whisper import WhisperModel import os
model_size = "large-v2"
Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")
or run on GPU with INT8
model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
or run on CPU with INT8
model = WhisperModel(model_size, device="cpu", compute_type="int8")
segments, info = model.transcribe("audio.mp3", beam_size=5)
print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
Save output in SRT and TXT format
with open('output.srt', 'w') as srt_file: with open('output.txt', 'w') as txt_file: idx = 1 for segment in segments: srt_file.write(str(idx) + '\n') srt_file.write("{:.2f}".format(segment.start) + ' --> ' + "{:.2f}".format(segment.end) + '\n') srt_file.write(segment.text + '\n\n')
txt_file.write("[{:.2f} -> {:.2f}] {}\n".format(segment.start, segment.end, segment.text)) idx += 1print("Output saved in SRT and TXT format.")
- User
please make the output in txt file without timestamp
- Assistant
Sure, here's the updated code that saves the output in TXT format without timestamps:
from faster_whisper import WhisperModel import os
model_size = "large-v2"
Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")
or run on GPU with INT8
model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
or run on CPU with INT8
model = WhisperModel(model_size, device="cpu", compute_type="int8")
segments, info = model.transcribe("audio.mp3", beam_size=5)
print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
Save output in TXT format without timestamps
with open('output.txt', 'w') as txt_file: for segment in segments: txt_file.write(segment.text + '\n')
print("Output saved in TXT format without timestamps.")
- User
srt with timestamp and txt without timestamp
- Assistant
Sure, here's the updated code that saves the output in both SRT with timestamps and TXT without timestamps:
from faster_whisper import WhisperModel import os
model_size = "large-v2"
Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")
or run on GPU with INT8
model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
or run on CPU with INT8
model = WhisperModel(model_size, device="cpu", compute_type="int8")
segments, info = model.transcribe("audio.mp3", beam_size=5)
print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
Save output in SRT format with timestamps
with open('output.srt', 'w') as srt_file: idx = 1 for segment in segments: srt_file.write(str(idx) + '\n') srt_file.write("{:.2f}".format(segment.start) + ' --> ' + "{:.2f}".format(segment.end) + '\n') srt_file.write(segment.text + '\n\n')
idx += 1print("Output saved in SRT format with timestamps.")
- User
please make from segment in segment outside "save output" so the process visualize in google colab
- Assistant
Sure, here's the updated code where the output is saved after the segments are processed. This will allow you to visualize the process in Google Colab.
from faster_whisper import WhisperModel import os
model_size = "large-v2"
Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")
or run on GPU with INT8
model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
or run on CPU with INT8
model = WhisperModel(model_size, device="cpu", compute_type="int8")
segments, info = model.transcribe("audio.mp3", beam_size=5)
print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
Print segmentation process
for i, segment in enumerate(segments): print(f"Segment {i+1}/{len(segments)}: [{round(segment.start,2)} s - {round(segment.end,2)} s]")