PyWhisperCpp API Reference
pywhispercpp.model
This module contains a simple Python API on-top of the C-style whisper.cpp API.
Segment
Segment(t0, t1, text)
A small class representing a transcription segment
Parameters:
-
t0
(int
) –start time
-
t1
(int
) –end time
-
text
(str
) –text
Source code in pywhispercpp/model.py
37 38 39 40 41 42 43 44 45 |
|
Model
Model(
model="tiny",
models_dir=None,
params_sampling_strategy=0,
redirect_whispercpp_logs_to=False,
**params
)
This classes defines a Whisper.cpp model.
Example usage.
model = Model('base.en', n_threads=6)
segments = model.transcribe('file.mp3')
for segment in segments:
print(segment.text)
Parameters:
-
model
(str
, default:'tiny'
) –The name of the model, one of the AVAILABLE_MODELS, (default to
tiny
), or a direct path to aggml
model. -
models_dir
(str
, default:None
) –The directory where the models are stored, or where they will be downloaded if they don't exist, default to MODELS_DIR
-
params_sampling_strategy
(int
, default:0
) –0 -> GREEDY, else BEAM_SEARCH
-
redirect_whispercpp_logs_to
(Union[bool, TextIO, str, None]
, default:False
) –where to redirect the whisper.cpp logs, default to False (no redirection), accepts str file path, sys.stdout, sys.stderr, or use None to redirect to devnull
-
params
–keyword arguments for different whisper.cpp parameters, see PARAMS_SCHEMA
Source code in pywhispercpp/model.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
transcribe
transcribe(
media,
n_processors=None,
new_segment_callback=None,
**params
)
Transcribes the media provided as input and returns list of Segment
objects.
Accepts a media_file path (audio/video) or a raw numpy array.
Parameters:
-
media
(Union[str, ndarray]
) –Media file path or a numpy array
-
n_processors
(int
, default:None
) –if not None, it will run the transcription on multiple processes binding to whisper.cpp/whisper_full_parallel > Split the input audio in chunks and process each chunk separately using whisper_full()
-
new_segment_callback
(Callable[[Segment], None]
, default:None
) –callback function that will be called when a new segment is generated
-
params
–keyword arguments for different whisper.cpp parameters, see ::: constants.PARAMS_SCHEMA
Returns:
-
List[Segment]
–List of transcription segments
Source code in pywhispercpp/model.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
get_params
get_params()
Returns a dict
representation of the actual params
Returns:
-
dict
–params dict
Source code in pywhispercpp/model.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
get_params_schema
staticmethod
get_params_schema()
A simple link to ::: constants.PARAMS_SCHEMA
Returns:
-
dict
–dict of params schema
Source code in pywhispercpp/model.py
177 178 179 180 181 182 183 |
|
lang_max_id
staticmethod
lang_max_id()
Returns number of supported languages. Direct binding to whisper.cpp/lang_max_id
Returns:
-
int
–
Source code in pywhispercpp/model.py
185 186 187 188 189 190 191 192 |
|
print_timings
print_timings()
Direct binding to whisper.cpp/whisper_print_timings
Returns:
-
None
–None
Source code in pywhispercpp/model.py
194 195 196 197 198 199 200 |
|
system_info
staticmethod
system_info()
Direct binding to whisper.cpp/whisper_print_system_info
Returns:
-
None
–None
Source code in pywhispercpp/model.py
202 203 204 205 206 207 208 209 |
|
available_languages
staticmethod
available_languages()
Returns a list of supported language codes
Returns:
-
list[str]
–list of supported language codes
Source code in pywhispercpp/model.py
211 212 213 214 215 216 217 218 219 220 221 222 |
|
auto_detect_language
auto_detect_language(media, offset_ms=0, n_threads=4)
Automatic language detection using whisper.cpp/whisper_pcm_to_mel and whisper.cpp/whisper_lang_auto_detect
Parameters:
-
media
(Union[str, ndarray]
) –Media file path or a numpy array
-
offset_ms
(int
, default:0
) –offset in milliseconds
-
n_threads
(int
, default:4
) –number of threads to use
Returns:
-
Tuple[Tuple[str, float32], dict[str, float32]]
–((detected_language, probability), probabilities for all languages)
Source code in pywhispercpp/model.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|
pywhispercpp.constants
Constants
WHISPER_SAMPLE_RATE
module-attribute
WHISPER_SAMPLE_RATE = WHISPER_SAMPLE_RATE
MODELS_BASE_URL
module-attribute
MODELS_BASE_URL = (
"https://huggingface.co/ggerganov/whisper.cpp"
)
MODELS_PREFIX_URL
module-attribute
MODELS_PREFIX_URL = 'resolve/main/ggml'
PACKAGE_NAME
module-attribute
PACKAGE_NAME = 'pywhispercpp'
MODELS_DIR
module-attribute
MODELS_DIR = Path(user_data_dir(PACKAGE_NAME)) / 'models'
AVAILABLE_MODELS
module-attribute
AVAILABLE_MODELS = [
"base",
"base-q5_1",
"base-q8_0",
"base.en",
"base.en-q5_1",
"base.en-q8_0",
"large-v1",
"large-v2",
"large-v2-q5_0",
"large-v2-q8_0",
"large-v3",
"large-v3-q5_0",
"large-v3-turbo",
"large-v3-turbo-q5_0",
"large-v3-turbo-q8_0",
"medium",
"medium-q5_0",
"medium-q8_0",
"medium.en",
"medium.en-q5_0",
"medium.en-q8_0",
"small",
"small-q5_1",
"small-q8_0",
"small.en",
"small.en-q5_1",
"small.en-q8_0",
"tiny",
"tiny-q5_1",
"tiny-q8_0",
"tiny.en",
"tiny.en-q5_1",
"tiny.en-q8_0",
]
PARAMS_SCHEMA
module-attribute
PARAMS_SCHEMA = {
"n_threads": {
"type": int,
"description": "Number of threads to allocate for the inferencedefault to min(4, available hardware_concurrency)",
"options": None,
"default": None,
},
"n_max_text_ctx": {
"type": int,
"description": "max tokens to use from past text as prompt for the decoder",
"options": None,
"default": 16384,
},
"offset_ms": {
"type": int,
"description": "start offset in ms",
"options": None,
"default": 0,
},
"duration_ms": {
"type": int,
"description": "audio duration to process in ms",
"options": None,
"default": 0,
},
"translate": {
"type": bool,
"description": "whether to translate the audio to English",
"options": None,
"default": False,
},
"no_context": {
"type": bool,
"description": "do not use past transcription (if any) as initial prompt for the decoder",
"options": None,
"default": False,
},
"single_segment": {
"type": bool,
"description": "force single segment output (useful for streaming)",
"options": None,
"default": False,
},
"print_special": {
"type": bool,
"description": "print special tokens (e.g. <SOT>, <EOT>, <BEG>, etc.)",
"options": None,
"default": False,
},
"print_progress": {
"type": bool,
"description": "print progress information",
"options": None,
"default": True,
},
"print_realtime": {
"type": bool,
"description": "print results from within whisper.cpp (avoid it, use callback instead)",
"options": None,
"default": False,
},
"print_timestamps": {
"type": bool,
"description": "print timestamps for each text segment when printing realtime",
"options": None,
"default": True,
},
"token_timestamps": {
"type": bool,
"description": "enable token-level timestamps",
"options": None,
"default": False,
},
"thold_pt": {
"type": float,
"description": "timestamp token probability threshold (~0.01)",
"options": None,
"default": 0.01,
},
"thold_ptsum": {
"type": float,
"description": "timestamp token sum probability threshold (~0.01)",
"options": None,
"default": 0.01,
},
"max_len": {
"type": int,
"description": "max segment length in characters, note: token_timestamps needs to be set to True for this to work",
"options": None,
"default": 0,
},
"split_on_word": {
"type": bool,
"description": "split on word rather than on token (when used with max_len)",
"options": None,
"default": False,
},
"max_tokens": {
"type": int,
"description": "max tokens per segment (0 = no limit)",
"options": None,
"default": 0,
},
"audio_ctx": {
"type": int,
"description": "overwrite the audio context size (0 = use default)",
"options": None,
"default": 0,
},
"initial_prompt": {
"type": str,
"description": "Initial prompt, these are prepended to any existing text context from a previous call",
"options": None,
"default": None,
},
"prompt_tokens": {
"type": Tuple,
"description": "tokens to provide to the whisper decoder as initial prompt",
"options": None,
"default": None,
},
"prompt_n_tokens": {
"type": int,
"description": "tokens to provide to the whisper decoder as initial prompt",
"options": None,
"default": 0,
},
"language": {
"type": str,
"description": 'for auto-detection, set to None, "" or "auto"',
"options": None,
"default": "",
},
"suppress_blank": {
"type": bool,
"description": "common decoding parameters",
"options": None,
"default": True,
},
"suppress_non_speech_tokens": {
"type": bool,
"description": "common decoding parameters",
"options": None,
"default": False,
},
"temperature": {
"type": float,
"description": "initial decoding temperature",
"options": None,
"default": 0.0,
},
"max_initial_ts": {
"type": float,
"description": "max_initial_ts",
"options": None,
"default": 1.0,
},
"length_penalty": {
"type": float,
"description": "length_penalty",
"options": None,
"default": -1.0,
},
"temperature_inc": {
"type": float,
"description": "temperature_inc",
"options": None,
"default": 0.2,
},
"entropy_thold": {
"type": float,
"description": 'similar to OpenAI\'s "compression_ratio_threshold"',
"options": None,
"default": 2.4,
},
"logprob_thold": {
"type": float,
"description": "logprob_thold",
"options": None,
"default": -1.0,
},
"no_speech_thold": {
"type": float,
"description": "no_speech_thold",
"options": None,
"default": 0.6,
},
"greedy": {
"type": dict,
"description": "greedy",
"options": None,
"default": {"best_of": -1},
},
"beam_search": {
"type": dict,
"description": "beam_search",
"options": None,
"default": {"beam_size": -1, "patience": -1.0},
},
}
pywhispercpp.utils
Helper functions
download_model
download_model(
model_name, download_dir=None, chunk_size=1024
)
Helper function to download the ggml
models
Parameters:
-
model_name
(str
) –name of the model, one of ::: constants.AVAILABLE_MODELS
-
download_dir
–Where to store the models
-
chunk_size
–size of the download chunk
Returns:
-
str
–Absolute path of the downloaded model
Source code in pywhispercpp/utils.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
to_timestamp
to_timestamp(t, separator=',')
376 -> 00:00:03,760 1344 -> 00:00:13,440
Implementation from whisper.cpp/examples/main
Parameters:
-
t
(int
) –input time from whisper timestamps
-
separator
–seprator between seconds and milliseconds
Returns:
-
str
–time representation in hh: mm: ss[separator]ms
Source code in pywhispercpp/utils.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
output_txt
output_txt(segments, output_file_path)
Creates a raw text from a list of segments
Implementation from whisper.cpp/examples/main
Parameters:
-
segments
(list
) –list of segments
Returns:
-
str
–path of the file
Source code in pywhispercpp/utils.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
output_vtt
output_vtt(segments, output_file_path)
Creates a vtt file from a list of segments
Implementation from whisper.cpp/examples/main
Parameters:
-
segments
(list
) –list of segments
Returns:
-
str
–Absolute path of the file
Source code in pywhispercpp/utils.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
output_srt
output_srt(segments, output_file_path)
Creates a srt file from a list of segments
Parameters:
-
segments
(list
) –list of segments
Returns:
-
str
–Absolute path of the file
Source code in pywhispercpp/utils.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
output_csv
output_csv(segments, output_file_path)
Creates a srt file from a list of segments
Parameters:
-
segments
(list
) –list of segments
Returns:
-
str
–Absolute path of the file
Source code in pywhispercpp/utils.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
redirect_stderr
redirect_stderr(to=False)
Redirect stderr to the specified target.
Parameters:
-
to
–- None to suppress output (redirect to devnull), - sys.stdout to redirect to stdout, - A file path (str) to redirect to a file, - False to do nothing (no redirection).
Source code in pywhispercpp/utils.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
pywhispercpp.examples
assistant
A simple example showcasing the use of pywhispercpp
as an assistant.
The idea is to use a VAD
to detect speech (in this example we used webrtcvad), and when speech is detected
we run the inference.
Assistant
Assistant(
model="tiny",
input_device=None,
silence_threshold=8,
q_threshold=16,
block_duration=30,
commands_callback=None,
**model_params
)
Assistant class
Example usage
from pywhispercpp.examples.assistant import Assistant
my_assistant = Assistant(commands_callback=print, n_threads=8)
my_assistant.start()
Parameters:
-
model
–whisper.cpp model name or a direct path to a
ggml
model -
input_device
(int
, default:None
) –The input device (aka microphone), keep it None to take the default
-
silence_threshold
(int
, default:8
) –The duration of silence after which the inference will be running
-
q_threshold
(int
, default:16
) –The inference won't be running until the data queue is having at least
q_threshold
elements -
block_duration
(int
, default:30
) –minimum time audio updates in ms
-
commands_callback
(Callable[[str], None]
, default:None
) –The callback to run when a command is received
-
model_log_level
–Logging level
-
model_params
–any other parameter to pass to the whsiper.cpp model see ::: pywhispercpp.constants.PARAMS_SCHEMA
Source code in pywhispercpp/examples/assistant.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
start
start()
Use this function to start the assistant
Returns:
-
None
–None
Source code in pywhispercpp/examples/assistant.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
gui
WorkerSignals
Bases: QObject
Defines signals available from a running worker thread. Supported signals are: - finished: No data - error: tuple (exctype, value, traceback.format_exc()) - result: list (the transcribed segments) - progress: int (0-100) - status_update: str
PyWhisperCppWorker
PyWhisperCppWorker(
audio_file_path, model_name, **transcribe_params
)
Bases: Thread
Source code in pywhispercpp/examples/gui.py
257 258 259 260 261 262 263 |
|
run
run()
Executes the transcription process.
Source code in pywhispercpp/examples/gui.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
TranscriptionApp
TranscriptionApp()
Bases: QWidget
Source code in pywhispercpp/examples/gui.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
initUI
initUI()
Initializes the user interface of the application.
Source code in pywhispercpp/examples/gui.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
|
toggle_settings_visibility
toggle_settings_visibility()
Toggles the visibility of the settings content frame and updates the arrow.
Source code in pywhispercpp/examples/gui.py
529 530 531 532 533 534 535 536 |
|
select_file
select_file()
Opens a file dialog to select an audio file.
Source code in pywhispercpp/examples/gui.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
|
start_transcription
start_transcription()
Starts the transcription process in a separate thread, passing selected settings.
Source code in pywhispercpp/examples/gui.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
|
format_time
format_time(milliseconds)
Converts milliseconds to HH:MM:SS.ms format.
Source code in pywhispercpp/examples/gui.py
625 626 627 628 629 630 |
|
on_transcription_result
on_transcription_result(segments)
Populates the results table with the transcription segments. Stores segments for export.
Source code in pywhispercpp/examples/gui.py
646 647 648 649 650 651 652 653 |
|
on_transcription_finished
on_transcription_finished()
Cleans up after the transcription thread is finished.
Source code in pywhispercpp/examples/gui.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
|
on_transcription_error
on_transcription_error(err)
Displays an error message if transcription fails.
Source code in pywhispercpp/examples/gui.py
670 671 672 673 674 675 676 677 |
|
export_transcription
export_transcription(format_type)
Handles exporting the transcription to a chosen file format.
Source code in pywhispercpp/examples/gui.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
|
copy_all_text_to_clipboard
copy_all_text_to_clipboard()
Concatenates all text from segments and copies it to the clipboard.
Source code in pywhispercpp/examples/gui.py
741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
|
show_about_dialog
show_about_dialog()
Opens a small dialog with About information.
Source code in pywhispercpp/examples/gui.py
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
|
livestream
Quick and dirty realtime livestream transcription.
Not fully satisfying though :) You are welcome to make it better.
LiveStream
LiveStream(
url,
model="tiny.en",
block_size=1024,
buffer_size=20,
sample_size=4,
output_device=None,
model_log_level=logging.CRITICAL,
**model_params
)
LiveStream class
Note
It heavily depends on the machine power, the processor will jump quickly to 100% with the wrong parameters.
Example usage
from pywhispercpp.examples.livestream import LiveStream
url = "" # Make sure it is a direct stream URL
ls = LiveStream(url=url, n_threads=4)
ls.start()
Parameters:
-
url
– -
model
–whisper.cpp model
-
block_size
(int
, default:1024
) –block size, default to 1024
-
buffer_size
(int
, default:20
) –number of blocks used for buffering, default to 20
-
sample_size
(int
, default:4
) –sample size
-
output_device
(int
, default:None
) –the output device, aka the speaker, leave it None to take the default
-
model_log_level
–logging level
-
model_params
–any other whisper.cpp params
Source code in pywhispercpp/examples/livestream.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
main
A simple Command Line Interface to test the package
recording
A simple example showcasing how to use pywhispercpp to transcribe a recording.
Recording
Recording(duration, model='tiny.en', **model_params)
Recording class
Example usage
from pywhispercpp.examples.recording import Recording
myrec = Recording(5)
myrec.start()
Source code in pywhispercpp/examples/recording.py
38 39 40 41 42 43 44 45 |
|