Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProgressBar handler #80

Closed
nicko27 opened this issue Mar 26, 2023 · 11 comments
Closed

ProgressBar handler #80

nicko27 opened this issue Mar 26, 2023 · 11 comments

Comments

@nicko27
Copy link

nicko27 commented Mar 26, 2023

Hi,
I want to use your script with a progressbar but segments doesn't implement len. Is it possible to solve this ? Please

Bonjour d'Evreux (27)

Thanks

@guillaumekln
Copy link
Contributor

Hello,

The transcribe method returns a structure AudioInfo which contains the duration of the audio file. It can be used to implement the progress bar. For example:

from tqdm import tqdm
from faster_whisper import WhisperModel

model = WhisperModel("tiny")
segments, info = model.transcribe("audio.mp3")

total_duration = round(info.duration, 2)  # Same precision as the Whisper timestamps.

with tqdm(total=total_duration, unit=" seconds") as pbar:
    for segment in segments:
        segment_duration = segment.end - segment.start
        pbar.update(segment_duration)

@nicko27
Copy link
Author

nicko27 commented Mar 27, 2023

Thanks

@phineas-pta
Copy link

phineas-pta commented Apr 10, 2023

just a minor enhancement, when using SileroVAD, not all segments are continuous of others

from tqdm import tqdm
from faster_whisper import WhisperModel

model = WhisperModel("tiny")
segments, info = model.transcribe("audio.mp3", vad_filter=True)

total_duration = round(info.duration, 2)  # Same precision as the Whisper timestamps.
timestamps = 0.0  # to get the current segments

with tqdm(total=total_duration, unit=" audio seconds") as pbar:
    for segment in segments:
        pbar.update(segment.end - timestamps)
        timestamps = segment.end
    if timestamps < info.duration: # silence at the end of the audio
        pbar.update(info.duration - timestamps)

@Purfview
Copy link
Contributor

Purfview commented May 23, 2023

How to redirect tqdm's output to print()?

@phineas-pta
Copy link

what do you mean? you want to print(tqdm(...)) ?

@Purfview
Copy link
Contributor

I want print() showing a progress bar instead of tqdm().

PS
I think I figured it out, need to test it.

@phineas-pta
Copy link

why ? tqdm can handle console progress bar

@Purfview
Copy link
Contributor

@phineas-pta But it can't handle console's title bar, that's where I want to see progress bar.

@phineas-pta
Copy link

is there any library to do that ?

@Purfview
Copy link
Contributor

Purfview commented May 26, 2023

is there any library to do that ?

You don't need lib for that. My problem was solved, but I saw various glitches from tqdm and I think the progress bar examples posted above doesn't work properly.
Today I'll try to debug that and will post a solution.

@Purfview
Copy link
Contributor

Purfview commented May 26, 2023

Examples above are inaccurate because there tqdm process only a first segment from chunk and the rest are ignored.
Here is my accurate implementation + progress bar in Windows console's title:

import time
import io
from threading import Thread
from time import sleep
from os import system
from tqdm import tqdm
from faster_whisper import WhisperModel


model = WhisperModel("tiny")
segments, info = model.transcribe("audio.mp3")


def pbar_delayed():       # to get last timestamp from chunk
    global timestamp_prev
    sleep(set_delay)      # wait for whole chunk to be iterated
    pbar.update(timestamp_last - timestamp_prev)
    timestamp_prev = timestamp_last
    system("title " + capture.getvalue().splitlines()[-1].replace("|","^|").replace("<","^<").replace("?","0"))
    print(capture.getvalue().splitlines()[-1])

total_dur = round(info.duration)
td_len = str(len(str(total_dur)))      # get length for n_fmt
global timestamp_prev, timestamp_last
timestamp_prev = 0                     # last timestamp in previous chunk
timestamp_last = 0                     # current timestamp
capture = io.StringIO()                # capture progress bars from tqdm
last_burst = 0.0                       # time of last iteration burst aka chunk
set_delay = 0.1                        # max time it takes to iterate chunk & minimum time between chunks.

s_time = time.time()
print("")
bar_f = "{percentage:3.0f}% | {n_fmt:>"+td_len+"}/{total_fmt} | {elapsed}<<{remaining} | {rate_noinv_fmt}"

with tqdm(file=capture, total=total_dur, unit=" audio seconds", smoothing=0.00001, bar_format=bar_f) as pbar:
    for segment in segments:
        timestamp_last = round(segment.end)
        time_now = time.time()
        if time_now - last_burst > set_delay:  # catch new chunk
            last_burst = time_now
            Thread(target=pbar_delayed, daemon=False).start()
    sleep(set_delay + 0.3) # wait for the last pbar_delayed to finish
    if timestamp_last < total_dur: # silence at the end of the audio
        pbar.update(total_dur - timestamp_last)
        system("title " + capture.getvalue().splitlines()[-1].replace("|","^|").replace("<","^<").replace("?","0"))
        print(capture.getvalue().splitlines()[-1])

print("\n\nFinished! Speed: %s audio seconds/s" % round(info.duration / ((time.time() - s_time)), 2))

EDIT:

To get progress in Linux terminal's title try to replace system(.... lines with this:
print('\33]0;'+ capture.getvalue().splitlines()[-1] +'\a', end='', flush=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants