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
36 37 38 39 40 41 42 43 44 |
|
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
68 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 |
|
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
98 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 |
|
get_params
get_params()
Returns a dict
representation of the actual params
Returns:
-
dict
–params dict
Source code in pywhispercpp/model.py
158 159 160 161 162 163 164 165 166 167 168 169 |
|
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
171 172 173 174 175 176 177 |
|
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
179 180 181 182 183 184 185 186 |
|
print_timings
print_timings()
Direct binding to whisper.cpp/whisper_print_timings
Returns:
-
None
–None
Source code in pywhispercpp/model.py
188 189 190 191 192 193 194 |
|
system_info
staticmethod
system_info()
Direct binding to whisper.cpp/whisper_print_system_info
Returns:
-
None
–None
Source code in pywhispercpp/model.py
196 197 198 199 200 201 202 203 |
|
available_languages
staticmethod
available_languages()
Returns a list of supported language codes
Returns:
-
list
–list of supported language codes
Source code in pywhispercpp/model.py
205 206 207 208 209 210 211 212 213 214 215 216 |
|
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.en",
"base.en-q5_1",
"large-v1",
"large-v2",
"large-v2-q5_0",
"large-v3",
"large-v3-q5_0",
"large-v3-turbo",
"large-v3-turbo-q5_0",
"medium",
"medium-q5_0",
"medium.en",
"medium.en-q5_0",
"small",
"small-q5_1",
"small.en",
"small.en-q5_1",
"tiny",
"tiny-q5_1",
"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",
"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": "en",
},
"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_log_level=logging.INFO,
**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
(int
, default:INFO
) –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
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 85 86 87 |
|
start
start()
Use this function to start the assistant
Returns:
-
None
–None
Source code in pywhispercpp/examples/assistant.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
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 |
|