-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add test recognize_vosk() #842
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d77950
feat: Download with vosk model
ftnext 1d37d8b
chore: vosk extra
ftnext c70f9e7
test: Developers can test recognize_vosk() in cloned repository
ftnext 93086ff
chore: Add vosk test in CI
ftnext 898aef1
feat: Handle potential download errors (thanks Copliot review)
ftnext 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 |
|---|---|---|
|
|
@@ -23,3 +23,5 @@ groq = | |
| httpx < 0.28 | ||
| assemblyai = | ||
| requests | ||
| vosk = | ||
| vosk | ||
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,51 @@ | ||
| # /// script | ||
| # requires-python = ">=3.9" | ||
| # dependencies = [ | ||
| # "requests", | ||
| # "tqdm", | ||
| # ] | ||
| # /// | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import zipfile | ||
|
|
||
| import requests | ||
| from tqdm import tqdm | ||
|
|
||
|
|
||
| def setup_vosk_model(model_url: str, model_dir: str) -> None: | ||
| model_filename = os.path.basename(model_url) | ||
| model_name = os.path.splitext(model_filename)[0] | ||
|
|
||
| print(f"Downloading model {model_filename} ...") | ||
| response = requests.get(model_url, stream=True) | ||
ftnext marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| response.raise_for_status() | ||
| total_size = int(response.headers.get("content-length", 0)) | ||
|
|
||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| download_path = os.path.join(temp_dir, model_filename) | ||
| with open(download_path, "wb") as f: | ||
| with tqdm(total=total_size, unit="B", unit_scale=True) as pbar: | ||
| for chunk in response.iter_content(chunk_size=8192): | ||
| if chunk: | ||
| f.write(chunk) | ||
| pbar.update(len(chunk)) | ||
|
|
||
| print("Unzip model...") | ||
| with zipfile.ZipFile(download_path, "r") as zip_ref: | ||
| zip_ref.extractall(temp_dir) | ||
|
|
||
| extracted_dir = os.path.join(temp_dir, model_name) | ||
| if os.path.exists(model_dir): | ||
| shutil.rmtree(model_dir) | ||
| shutil.copytree(extracted_dir, model_dir) | ||
|
|
||
| print(f"Setup complete! Model is placed in the directory: {model_dir}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| model_url = ( | ||
| "https://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip" | ||
| ) | ||
| setup_vosk_model(model_url, "model") | ||
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,18 @@ | ||
| from pathlib import Path | ||
|
|
||
| from speech_recognition import AudioData, Recognizer | ||
|
|
||
|
|
||
| def test_recognize_vosk(): | ||
| audio_file = str(Path(__file__).parent.parent / "english.wav") | ||
| audio_data = AudioData.from_file(audio_file) | ||
| sut = Recognizer() | ||
|
|
||
| actual = sut.recognize_vosk(audio_data) | ||
|
|
||
| expected = """\ | ||
| { | ||
| "text" : "one two three" | ||
| }\ | ||
| """ | ||
| assert actual == expected |
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.
[nitpick] If setup_vosk.py is a local script rather than an installed package, consider using 'python setup_vosk.py' to ensure reliable execution within the workflow.