-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Fix Audio Classification Pipeline top_k Documentation Mismatch and Bug #35736 #35771
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
Merged
Rocketknight1
merged 7 commits into
huggingface:main
from
sambhavnoobcoder:top_k-Doc-Mismatch
Feb 5, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27d05d3
added condition for top_k Doc mismatch fix
sambhavnoobcoder 5316973
initilation of test file for top_k changes
sambhavnoobcoder 9fc18f6
added test for returning all labels
sambhavnoobcoder e887054
added test for few labels
sambhavnoobcoder 94cb96a
tests/test_audio_classification_top_k.py
sambhavnoobcoder 82928b2
final fix
sambhavnoobcoder bc16a09
ruff fix
sambhavnoobcoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from transformers import pipeline | ||
| from transformers.testing_utils import require_torch | ||
|
|
||
|
|
||
| @require_torch | ||
| class AudioClassificationTopKTest(unittest.TestCase): | ||
| def test_top_k_none_returns_all_labels(self): | ||
| model_name = "superb/wav2vec2-base-superb-ks" # model with more than 5 labels | ||
| classification_pipeline = pipeline( | ||
| "audio-classification", | ||
| model=model_name, | ||
| top_k=None, | ||
| ) | ||
|
|
||
| # Create dummy input | ||
| sampling_rate = 16000 | ||
| signal = np.zeros((sampling_rate,), dtype=np.float32) | ||
|
|
||
| result = classification_pipeline(signal) | ||
| num_labels = classification_pipeline.model.config.num_labels | ||
|
|
||
| self.assertEqual(len(result), num_labels, "Should return all labels when top_k is None") | ||
|
|
||
| def test_top_k_none_with_few_labels(self): | ||
| model_name = "superb/hubert-base-superb-er" # model with fewer labels | ||
| classification_pipeline = pipeline( | ||
| "audio-classification", | ||
| model=model_name, | ||
| top_k=None, | ||
| ) | ||
|
|
||
| # Create dummy input | ||
| sampling_rate = 16000 | ||
| signal = np.zeros((sampling_rate,), dtype=np.float32) | ||
|
|
||
| result = classification_pipeline(signal) | ||
| num_labels = classification_pipeline.model.config.num_labels | ||
|
|
||
| self.assertEqual(len(result), num_labels, "Should handle models with fewer labels correctly") | ||
|
|
||
| def test_top_k_greater_than_labels(self): | ||
| model_name = "superb/hubert-base-superb-er" | ||
| classification_pipeline = pipeline( | ||
| "audio-classification", | ||
| model=model_name, | ||
| top_k=100, # intentionally large number | ||
| ) | ||
|
|
||
| # Create dummy input | ||
| sampling_rate = 16000 | ||
| signal = np.zeros((sampling_rate,), dtype=np.float32) | ||
|
|
||
| result = classification_pipeline(signal) | ||
| num_labels = classification_pipeline.model.config.num_labels | ||
|
|
||
| self.assertEqual(len(result), num_labels, "Should cap top_k to number of labels") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
cc @Rocketknight1 this should not need a single file on it's own, this should go in the pipeline tests
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.
Will fix, my bad for not noticing it was a separate file in the review!