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
Adding chunking for whisper (all seq2seq actually). Very crude matching algorithm. #20104
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this. Not sure if the PR is ready for (at least core maintainer) review yet?
# if self.type not in {"ctc", "ctc_with_lm"}: | ||
# raise ValueError( | ||
# "`chunk_length_s` is only valid for CTC models, use other chunking options for other models" | ||
# ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clean up?
# self.assertEqual( | ||
# str(v.exception), | ||
# "`chunk_length_s` is only valid for CTC models, use other chunking options for other models", | ||
# ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clean up as well?
# waveform = np.tile(np.arange(1000, dtype=np.float32), 34) | ||
# output = speech_recognizer(waveform) | ||
# self.assertEqual(output, {"text": ""}) | ||
|
||
ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation").sort("id") | ||
filename = ds[40]["file"] | ||
# output = speech_recognizer(filename) | ||
# self.assertEqual(output, {"text": " A man said to the universe, Sir, I exist."}) | ||
print(filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments and print statements to clean up.
Yup sorry it was slightly early for you. We chunk with stride. and we make a hopeful stitch to find the longest sequence from all the subsequences. PROs:
CONs:
|
As we discussed offline with @Narsil , will be implementing the |
Seems it's going to be complex because of fault tolerance which does seem to be important. You can try doing something like #!wget https://www.archive.org/download/around_world_80_days_mfs_librivox/around_world_in_80_days_01_verne.mp3
from transformers import pipeline
speech_recognizer = pipeline(
task="automatic-speech-recognition",
model="openai/whisper-small",
framework="pt",
batch_size=2,
device=0,
chunk_length_s=30,
generate_kwargs={"max_new_tokens": 1024},
)
out = speech_recognizer(["around_world_in_80_days_01_verne.mp3"])
print(out) This will required some suboptimal stitches to work. |
@sgugger it's now ready for review. The TODO is left intentionnally. It might really become relevant on hour+ long files where the current naive algorithm might become too slow. However the code is likely to be orders of magnitude more complex (if a O(n) solution exists, I'm pretty sure we could find an expected O(n) algorithm, but not sure about worst case). I added a warning because the current code Will fail in some know circumstances. I updated the PR description to reflect those. If those tradeoffs are not good enough, I'm happy to not merge this PR in this state. The only other option I see is whisper specific with timestamps and it would only alleviate some of the issues. |
Before merging, would love to try a little bit, otherwise LGTM (looking for a solution to the faults) |
@ArthurZucker What are your conclusions ? |
5eb0179
to
bd13f54
Compare
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
I think that including timestamp tokens in the process could help with the error tolerance as they are consistently predicted at the end of pauses in the speech. If the stride is big enough not at least include pauses in speech, it boils down to matching these. |
@sgugger would like your opinion on this if possible. The results are pretty decent imo on regular speech. I'm still mentionning the caveats because they are real. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks a lot for working on this
output = speech_recognizer([filename], chunk_length_s=5, batch_size=4) | ||
self.assertEqual(output, [{"text": " A man said to the universe, Sir, I exist."}]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one comment on the warning, otherwise LGTM! Thanks!
logger.warning( | ||
"Using `chunk_length_s` is very experimental. The results will not necessarily be entirely" | ||
" accurate and will have caveats. More information:" | ||
" https://github.com/huggingface/transformers/pull/20104" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some logic to only throw this warning once? Users are complaining Transformers is too verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there already a created way to do that ?
Otherwise I can create some tool for it.
Any other location we could add this "single" warning ? (Will add in a different PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use a dict in the state like this one. No need to overengineer another solution IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
Has this been added to the current transformers version? I am getting the "ValueError: |
There hasn't been a release yet, you must use |
Using chunk_length_s=10 without stride_length_s=(4, 2) Is there an alternative way to transcribe large audio files when I am using a fine-tuned whisper model? |
Could you provide an example with the repetitions ? There might be some optimizations to be made on the tying of the chunks. |
condition_on_previous_text: bool I think it's true by default and it uses something like GPT to "verify" the transcription. When I simply use whisper with medium or large model, I prefer to set it to False I get a lot of repetitions even with small samples, like the audio from this commercial https://www.youtube.com/watch?v=LkllgKVgz8o or this trailer https://www.youtube.com/watch?v=xncMdIGR2pk I have fine tuned whisper for Greek and I am trying to use it with the following lines (after of course loading transformers and the model, etc) from transformers import pipeline transcript = pipeline( |
hi, I think |
We use a different decoding strategy here, because |
Did you try using I'm getting for the first example:
Which seems corect to me. |
For the second. |
Yes that was good Can you please tell me if this is the only way to transcribe large audio files with pipeline? Thank you all :) |
The stride defaults are |
when I ! pip install git+https://github.com/openai/whisper.git and import whisper, all is fine with medium model I have tried pipeline with
and still I get repetitions both with openai/whisper-medium openai/whisper-large and emilios/whisper-medium-el |
I've just noticed that translated is ok, but the transcription in the original language has repetitions https://www.youtube.com/watch?v=e_eCryyPRus model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language = "el", task = "transcribe") greek transcript with repetitions, removed model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language = "el", task = "translate") transcript translated into english, removed |
It is possible that the model is in cause then ? ML generative models are know to be repetitive. And the kin dof repetition I'm seeing here really looks like bad model generation more than erroneous stitching. |
Nope, I think the translation engine fixes (or hides) the repetitive phrases |
I confirm it is the model. Take the audio of the video you linked.
Then do the inference: from transformers import pipeline, AutoProcessor
processor = AutoProcessor.from_pretrained("emilios/whisper-medium-el")
pipe = pipeline(model="emilios/whisper-medium-el")
pipe.model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language="el", task="transcribe")
out = pipe("out_repete.mp3")
print(out) And you will see that the model goes looping all by itself. This is not the chunking's doing. |
But I get the same problem with openai model too [{'text': ' [μουσική] Εμείς σήμερα, εγώ του λαμβάνω,πικά, αλλά φαντάζομαι όσοι από μας προσπαθούν να σκεφτούν σοβαρά, μέσα σε όλο αυτό το χάος του ιστορικού υλικού που έχουμε μπροστά μας, επιλέγουμε μια παράδοση. Αυτό δεν σημαίνει ότι την επιλέγουμε για να σημαίνουμε δούλοι.. Επιλέγουμε ακριβώς την παράδοση εκείνη, δηλαδή αυτήν που ονομάζω Έλληνοδυτική, μέσα στην οποία η αμφισβήτηση της παράδοσης είναι ένα βασικό στοιχείο. Η αμφισβήτηση όχι για την ευχαρίστηση της αμφισβήτησης, Η αμφισβήτηση όταν υπάρχει λόγος, η δυνατότητα της αμφισβήτησης, η δυνατότητα του να σκεφτώ αλλιώς, του να μιλήσω αλλιώς από τη σκέφτετη. Η πλειοψηφία, η εκκλησία, το κράτος, το κόμμα κτλ. Δεν είναι έτσι; Ο δάσκαλος, οι γονείς ενδεχομένως. Και από εκεί και πέρα η δυνατότητα να βάλω σαν άτομο ή να βάλει μια κοινωνική ομάδα ή μια πολιτική κίνηση ερωτήματα σχετικά με το αν η σημερινή θέσμη της κοινωνίας είναι δίκαιη ή δεν είναι δίκαιη, εάν η ισότητα εντός εισαγωγικών, την οποία επαγγέλλεται το Σύνταγμα και ο νόμος για τους πολίτες, τα βασικά χαρακτηριστικά αυτής της παράδοσης, πιο όχι άλλο νόμο. Κάθε κοινωνία δημιουργεί τους θεσμούς της, αλλά η ιδέα ότι η θεσμία αυτή είναι η δική της δημιουργία ακριβώς δε είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρκετά. Είναι αρονομία της κοινωνίας δεν είναι μόνο και δεν είναι τόσο η εκμετάλλευση, η καταπίεση, η υπάρξη μιας εξουσίας χωρισμένης από την κοινωνία. Είναι η ιδέα ότι οι θεσμοί ήρθαν απαλού.ει και σε πρωτόγωνες κοινωνίες, στις οποίες δεν βλέπουμε αυτά τα συνόμενα. Η ετερονομία της κοινωνίας είναι το γεγονός ακριβώς ότι η κοινωνία αλοτριώνεται στους θεσμούς της οποίες η ίδια η δημιούργησε, διότι δεν ξέρει ότι η ίδια τους η δημιούργησε, Αν δεν υπήρχε Θεός, όλα θα ήσουν αυτοί που θα έρθουν.ημειωταίων δεν ανήκει στον Ντοστογεύσκη, αλλά μπορεί να το πάει κανείς πίσω, ως τουλάχιστον με έκακε τον Πλάτονα. Και το οποίον εγώ θεωρώ επιχείρημα υπαστηνό μου βήτα, δηλαδή ότι χρειάζεται ένας Θεός, διότι αλλιώς όλα αυτά τα ρεμάλια θα κάνουν, τους κατεύαιναν, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, εγώ, η αρχαία αθηνή, ναι. Τους κάνουμε τους νόμους μας και όσον δεν τους έχουμε αλλάξει, τους ευώμαστε. Αυτό είναι το πράγμα που πρέπει να δίνει. Από αυτή την άψη, αυτό το οποίο ενεργώ εγώ ως αυτώνομη κοινωνία, είναι μια κοινωνία, όχι οποία είναι διαφανής, αλλά είναι μια κοινωνία η οποία ξέρει ότι δεν υπάρχει υπερβατικότητα, ότι δεν υπάρχει υπερβατική πηγή των θεσμών και των νόμων, ότι δεν υπάρχει μεταθάνατον ζωή αυτό που ξέραν οι αρχαίοι Έλληνες, οι οποίοι δεν επίστευαν σε μεταθάνατο ζωή,υτό μας, στους εαυτούς μας, σαν κοινωνικό σύνολο, κανόνες και νόμιες, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να κάνουμε, να δούμε ότι όσο πρέπει να δούμε, να δούμε ότι όσο πρέπει να δούμε, να δούμε ότι όσο πρέπει να δούμε, να δούμε ότι όσο πρέπει να δούμε, να δούμες έχουμε να το κάνουμε και έχουμε να δώσουμε στον εαυτό μας, στους εαυτούς μας σαν κοινωνικό σύνολο, κανόνες και νόμους που να μας επιτρέπουν να υπάρχουμε σαν αυτώνομη κοινωνία και σαν αυτώνομα άτομα μέσα σε αυτή την κοινωνία.'}] Please check it with the following code in a notepad when you can !pip install git+https://github.com/huggingface/transformers mymodel = "openai/whisper-medium" from transformers import WhisperForConditionalGeneration link = 'https://www.youtube.com/watch?v=e_eCryyPRus' try: model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language = "el", task = "transcribe") from transformers import pipeline transcript = pipeline( out = transcript(["YouTube.mp4"]) |
Yes, this is what I'm saying. The model is repeating itself, there's not much we can do about it. If you could fine tune it even more, or on more data, or more diverse data, that could probably help. For faster solutions, you could try and reduce amount of repetition, with That should help you get started. But please bear in mind it's only a temporary solution, the real solution is fixing the model itself I'm afraid. (But all models end up doing repetition when out of domain). |
I thought that when I use pipeline and hf whisper medium model from openai mymodel = "openai/whisper-medium" is exactly the same model with the following code import whisper Which does not have any repetitions What am I missing? Transcript of the last video Η τ Playstation Εμείς σήμερα, εγώ τουλάχιστον προσωπικά, αλλά φαντάζομαι όσοι από μας προσπαθούν να σκεφτούν σοβαρά, μέσα σε όλο αυτό το χάος του ιστορικού υλικού που έχουμε μπροστά μας, επιλέγουμε μια παράδοση. Αυτό δεν σημαίνει ότι την επιλέγουμε για να τσιμίνουμε δούλοι. Επιλέγουμε ακριβώς την παράδοση εκείνη, δηλαδή αυτή που ονομάζω Έλληνο-Δυτική, μέσα στην οποία η αμφισβήτηση της παράδοσης είναι ένα βασικό στοιχείο. Η αμφισβήτηση όχι για την ευχαρίστηση της αμφισβήτησης, η αμφισβήτηση όταν υπάρχει λόγος, η δυνατότητα της αμφισβήτησης, η δυνατότητα του να σκεφτώ αλλιώς, του να μιλήσω αλλιώς από τι σκέφτεται η πλειοψηφία, η εκκλησία, το κράτος, το κόμμα κτλ. Δεν είναι έτσι? Ο δάσκαλος, οι γονείς ενδεχομένως. Και από εκεί και πέρα η δυνατότητα να βάλω σαν άτομο ή να βάλει μια κοινωνική ομάδα ή μια πολιτική κίνηση ερωτήματα σχετικά με το αν η σημερινή θέσμηση της κοινωνίας είναι δίκαιη ή δεν είναι δίκαιη, εάν η ισότητα εντός αορικών, την οποία αν επαγγέλεται το σύνταγμα και ο νόμος για τους πολίτες, υπάρχει στην πραγματικότητα ή δεν υπάρχει, αυτή η δυνατότητα είναι συμφύσης επίσης με τα βασικά χαρακτηριστικά αυτής της παράδοσης, πιο όχι άλλο νόμο. Κάθε κοινωνία δημιουργεί τους θεσμούς της, αλλά η ιδέα ότι η θεσμία αυτή είναι η δική της δημιουργία ακριβώς δεν υπάρχει στις περισσότερες κοινωνίες. Γι' αυτό και οι θεσμοί μένουν άθηκτοι. Υπάρχει η ιδέα ότι οι θεσμοί ήρθαν απαλού. Η ετερονομία της κοινωνίας δεν είναι μόνο και δεν είναι τόσο η εκμετάλλευση, η καταπίεση, η υπάρξη μιας εξουσίας χωρισμένης από την κοινωνία και τα λοιπά, γιατί η ετερονομία της κοινωνίας υπάρχει και σε πρωτόγωνες κοινωνίες, εσείς όπου δεν βλέπουμε αυτά τα φαινόμενα. Η ετερονομία της κοινωνίας είναι το γιόνωσό ακριβώς ότι η κοινωνία αλοτριώνεται στους θεσμούς τις οποίες η ίδια η δημιούργησε, διότι δεν ξέρει ότι η ίδια τους η δημιούργησε και κατά κάποιο τρόπο δεν μπορεί να το ξέρει, γιατί είναι τρομερά δύσκολο να το ξέρει. Και αυτό το περίφουμο επιχείρημα του Ντοστογεύσκη, το οποίο τόσο έχει εκθιαστεί, ότι εάν δεν υπήρχε Θεός όλα θα ήσανε επιτρεπτά, το οποίο σημειωταίων δεν ανήκει στον Ντοστογεύσκη, αλλά μπορεί να το πάει κανείς πίσω, ως τουλάχιστον με έκανε και τον Πλάτονα, και το οποίο εγώ θεωρώ επιχείρημα υπαστεινό μου βήτα, δηλαδή ότι χρειάζεται ένας Θεός, όλα αυτά τα ρεμάλια θα κάνουν ότι τους κατέβαιναν, παρά τη χειδαιότητα του επιχείρημα του Ντοστογεύσκη, παρά τη χειδαιότητά του εκφράζει μια βασική αλήθεια της θέσμης των ετερονόμων κοινωνιών. Δηλαδή χρειάζεται να λεχτεί ότι ο θεσμός έχει έρθει από αλλού, για να μπορεί να κατοχυρωθεί ο θεσμός. Εάν οι άνθρωποι ξέραν ότι κάνουν ήδη τους νόμους τους, θα τους ευβόντουσαν. Εσ' αυτό απαντάνε οι αρχαίοι Έλληνες και οι αρχαίοι Αθηνέοι, ναι, τους κάνουμε τους νόμους μας και όσον δεν τους έχουμε αλλάξει, τους ευόμαστε. Και σ' αυτό, κατά κάποιο τρόπο, προσπάθησε να απαντήσει το νεότερο δημοκρατικό και παναστατικό κίνημα στο μέτρο που απάντησε, προσπαθώντας να βάλει μπροστά την ιδέα ότι τους νόμους τους δημιουργεί ο λαός και ότι αυτό δεν είναι λόγος να μην είναι σεβαστοί αυτή η νόμη, παραδείγματι, δεν είναι έτσι. Από αυτή την άψη, αυτό το οποίο ενεργώ εγώ ως αυτώνομη κοινωνία, είναι μια κοινωνία όχι η οποία είναι διαφανής αλλά είναι μια κοινωνία η οποία ξέρει ότι δεν υπάρχει υπερβατικότητα, ότι δεν υπάρχει υπερβατική πηγή των θεσμών και των νόμων, ότι δεν υπάρχει μεταθάνατο ζωή αυτό που ξέραν οι αρχαίοι Έλληνες οι οποίοι δεν επίστευαν σε μεταθάνατο ζωή ή αν επίστευαν σε μεταθάνατο ζωή της δίνα ένα περιεχόμενο όπως φαίνεται στην Οδύσσια που ήταν 100 φορές χειρότερο από την επίγελ ζωή η οποία ξέρει συναιπώς ότι ό,τι γίνεται γίνεται εδώ κάτω και ότι ό,τι έχουμε να κάνουμε εμείς έχουμε να το κάνουμε και έχουμε να δώσουμε στον εαυτό μας, στους εαυτούς μας σαν κοινωνικό σύνολο κανόνες και νόμους που να μας επιτρέπουν να υπάρχουμε σαν αυτόνομη κοινωνία και σαν αυτόνομα άτομα μέσα σε αυτή την κοινωνία |
Both methods are different, and it's just luck based I think into how the split occurs. |
…ng algorithm. (huggingface#20104) * Very crude matching algorithm. * Fixing tests. * Removing comments * Adding warning + fix short matches. * Cleanup tests. * Quality. * Less noisy. * Fixup.
What does this PR do?
This adds
chunk_length_s
toseq2seq
algorithms.Approach
Since we have no way of finding a matching between output and input with
seq2seq
this is an alternative route.
This runs the pipeline on the various chunks and finds all generated output.
Then it tries to find the longest sequence of non special ids that could correspond
to the subsequences within the batch.
Pros
Cons
whisper
can currently do, and it does come with caveats). The currently algorithm will favor long chain of matching tokens."sir"
and¨Sir"
are different and will fail to match leading to some `¨Yes, sir. Sir Thomas" stitching instead of the intended "Yes, Sir Thomas.".Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.