Package: cotipywhispercpp
pywhispercppPython bindings forwhisper.cppwith a simple Pythonic API on top of it.whisper.cpp is:High-performance inference ofOpenAI's Whisperautomatic speech recognition (ASR) model:Plain C/C++ implementation without dependenciesApple silicon first-class citizen - optimized via Arm Neon and Accelerate frameworkAVX intrinsics support for x86 architecturesVSX intrinsics support for POWER architecturesMixed F16 / F32 precisionLow memory usage (Flash Attention)Zero memory allocations at runtimeRuns on the CPUC-style APISupported platforms:Mac OS (Intel and Arm)iOSAndroidLinux /FreeBSDWebAssemblyWindows (MSVCandMinGW]Table of contentsInstallationQuick startExamplesMainAssistantRecordingLive Stream TranscriptionAdvanced usageDiscussions and contributionsLicenseInstallationInstallffmpeg# on Ubuntu or Debiansudoaptupdate&&sudoaptinstallffmpeg# on Arch Linuxsudopacman-Sffmpeg# on MacOS using Homebrew (https://brew.sh/)brewinstallffmpeg# on Windows using Chocolatey (https://chocolatey.org/)chocoinstallffmpeg# on Windows using Scoop (https://scoop.sh/)scoopinstallffmpegOnce ffmpeg is installed, installpywhispercpppipinstallpywhispercppIf you want to use the examples, you will need to install extra dependenciespipinstallpywhispercpp[examples]Or install the latest dev version from GitHubpipinstallgit+https://github.com/abdeladim-s/pywhispercppQuick startfrompywhispercpp.modelimportModelmodel=Model('base.en',n_threads=6)segments=model.transcribe('file.mp3',speed_up=True)forsegmentinsegments:print(segment.text)You can also assign a customnew_segment_callbackfrompywhispercpp.modelimportModelmodel=Model('base.en',print_realtime=False,print_progress=False)segments=model.transcribe('file.mp3',new_segment_callback=print)Theggmlmodel will be downloaded automatically.You can pass anywhisper.cppparameteras a keyword argument to theModelclass or to thetranscribefunction.Thetranscribefunction accepts any media file (audio/video), in any format.Check theModelclass documentation for more details.ExamplesTheexamples foldercontains several examples inspired from the originalwhisper.cpp/examples.MainJust a straightforward example with a simple Command Line Interface.Check the source codehere, or use the CLI as follows:pwcppfile.wav-mbase--output-srt--print_realtimetrueRunpwcpp --helpto get the help messageusage:pwcpp[-h][-mMODEL][--version][--processorsPROCESSORS][-otxt][-ovtt][-osrt][-ocsv][--strategySTRATEGY][--n_threadsN_THREADS][--n_max_text_ctxN_MAX_TEXT_CTX][--offset_msOFFSET_MS][--duration_msDURATION_MS][--translateTRANSLATE][--no_contextNO_CONTEXT][--single_segmentSINGLE_SEGMENT][--print_specialPRINT_SPECIAL][--print_progressPRINT_PROGRESS][--print_realtimePRINT_REALTIME][--print_timestampsPRINT_TIMESTAMPS][--token_timestampsTOKEN_TIMESTAMPS][--thold_ptTHOLD_PT][--thold_ptsumTHOLD_PTSUM][--max_lenMAX_LEN][--split_on_wordSPLIT_ON_WORD][--max_tokensMAX_TOKENS][--speed_upSPEED_UP][--audio_ctxAUDIO_CTX][--prompt_tokensPROMPT_TOKENS][--prompt_n_tokensPROMPT_N_TOKENS][--languageLANGUAGE][--suppress_blankSUPPRESS_BLANK][--suppress_non_speech_tokensSUPPRESS_NON_SPEECH_TOKENS][--temperatureTEMPERATURE][--max_initial_tsMAX_INITIAL_TS][--length_penaltyLENGTH_PENALTY][--temperature_incTEMPERATURE_INC][--entropy_tholdENTROPY_THOLD][--logprob_tholdLOGPROB_THOLD][--no_speech_tholdNO_SPEECH_THOLD][--greedyGREEDY][--beam_searchBEAM_SEARCH]media_file[media_file...]positionalarguments:media_fileThepathofthemediafileoralistoffilesseparatedbyspace

options:-h,--helpshowthishelpmessageandexit-mMODEL,--modelMODELPathtothe`ggml`model,orjustthemodelname--versionshowprogram's version number and exit--processors PROCESSORSnumber of processors to use during computation-otxt, --output-txt   output result in a text file-ovtt, --output-vtt   output result in a vtt file-osrt, --output-srt   output result in a srt file-ocsv, --output-csv   output result in a CSV file--strategy STRATEGY   Available sampling strategiesGreefyDecoder -> 0BeamSearchDecoder -> 1--n_threads N_THREADSNumber of threads to allocate for the inferencedefault to min(4, available hardware_concurrency)--n_max_text_ctx N_MAX_TEXT_CTXmax tokens to use from past text as prompt for the decoder--offset_ms OFFSET_MSstart offset in ms--duration_ms DURATION_MSaudio duration to process in ms--translate TRANSLATEwhether to translate the audio to English--no_context NO_CONTEXTdo not use past transcription (if any) as initial prompt for the decoder--single_segment SINGLE_SEGMENTforce single segment output (useful for streaming)--print_special PRINT_SPECIALprint special tokens (e.g. <SOT>, <EOT>, <BEG>, etc.)--print_progress PRINT_PROGRESSprint progress information--print_realtime PRINT_REALTIMEprint results from within whisper.cpp (avoid it, use callback instead)--print_timestamps PRINT_TIMESTAMPSprint timestamps for each text segment when printing realtime--token_timestamps TOKEN_TIMESTAMPSenable token-level timestamps--thold_pt THOLD_PT   timestamp token probability threshold (~0.01)--thold_ptsum THOLD_PTSUMtimestamp token sum probability threshold (~0.01)--max_len MAX_LEN     max segment length in characters--split_on_word SPLIT_ON_WORDsplit on word rather than on token (when used with max_len)--max_tokens MAX_TOKENSmax tokens per segment (0 = no limit)--speed_up SPEED_UP   speed-up the audio by 2x using Phase Vocoder--audio_ctx AUDIO_CTXoverwrite the audio context size (0 = use default)--prompt_tokens PROMPT_TOKENStokens to provide to the whisper decoder as initial prompt--prompt_n_tokens PROMPT_N_TOKENStokens to provide to the whisper decoder as initial prompt--language LANGUAGE   for auto-detection, set to None, "" or "auto"--suppress_blank SUPPRESS_BLANKcommon decoding parameters--suppress_non_speech_tokens SUPPRESS_NON_SPEECH_TOKENScommon decoding parameters--temperature TEMPERATUREinitial decoding temperature--max_initial_ts MAX_INITIAL_TSmax_initial_ts--length_penalty LENGTH_PENALTYlength_penalty--temperature_inc TEMPERATURE_INCtemperature_inc--entropy_thold ENTROPY_THOLDsimilar to OpenAI's"compression_ratio_threshold"--logprob_tholdLOGPROB_THOLDlogprob_thold--no_speech_tholdNO_SPEECH_THOLDno_speech_thold--greedyGREEDYgreedy--beam_searchBEAM_SEARCHbeam_searchAssistantThis is a simple example showcasing the use ofpywhispercppas an assistant.
The idea is to use aVADto detect speech (in this example we used webrtcvad), and when some speech is detected,
we run the transcription.It is inspired from thewhisper.cpp/examples/commandexample.You can check the source codehereor you can use the class directly to create your own assistant:frompywhispercpp.examples.assistantimportAssistantmy_assistant=Assistant(commands_callback=print,n_threads=8)my_assistant.start()Here we set thecommands_callbackto a simpleprint, so the commands will just get printed on the screen.You can run this example from the command line as well$pwcpp-assistant--help

usage:pwcpp-assistant[-h][-mMODEL][-indINPUT_DEVICE][-stSILENCE_THRESHOLD][-bdBLOCK_DURATION]options:-h,--helpshowthishelpmessageandexit-mMODEL,--modelMODELWhisper.cppmodel,defaulttotiny.en-indINPUT_DEVICE,--input_deviceINPUT_DEVICEIdofTheinputdevice(akamicrophone)-stSILENCE_THRESHOLD,--silence_thresholdSILENCE_THRESHOLDhedurationofsilenceafterwhichtheinferencewillberunning,defaultto16-bdBLOCK_DURATION,--block_durationBLOCK_DURATIONminimumtimeaudioupdatesinms,defaultto30RecordingAnother simpleexampleto transcribe your own recordings.You can use it from Python as follows:frompywhispercpp.examples.recordingimportRecordingmyrec=Recording(5)myrec.start()Or from the command line:$pwcpp-recording--help

usage:pwcpp-recording[-h][-mMODEL]duration

positionalarguments:durationdurationinseconds

options:-h,--helpshowthishelpmessageandexit-mMODEL,--modelMODELWhisper.cppmodel,defaulttotiny.enLive Stream TranscriptionThisexampleis an attempt to transcribe a livestream in realtime, but the results are not quite satisfactory yet, the CPU jumps quickly to 100% and I cannot use huge models on my descent machine.
(Or maybe I am doing something wrong!) :sweat_smile:If you have a powerful machine, give it a try.From python :frompywhispercpp.examples.livestreamimportLiveStreamurl=""# Make sure it is a direct stream URLls=LiveStream(url=url,n_threads=4)ls.start()From the command line:$pwcpp-livestream--help

usage:pwcpp-livestream[-h][-ntN_THREADS][-mMODEL][-odOUTPUT_DEVICE][-blsBLOCK_SIZE][-busBUFFER_SIZE][-ssSAMPLE_SIZE]url

positionalarguments:urlStreamURL

options:-h,--helpshowthishelpmessageandexit-ntN_THREADS,--n_threadsN_THREADSnumberofthreads,defaultto3-mMODEL,--modelMODELWhisper.cppmodel,defaulttotiny.en-odOUTPUT_DEVICE,--output_deviceOUTPUT_DEVICEtheoutputdevice,akathespeaker,leaveitNonetotakethedefault-blsBLOCK_SIZE,--block_sizeBLOCK_SIZEblocksize,defaultto1024-busBUFFER_SIZE,--buffer_sizeBUFFER_SIZEnumberofblocksusedforbuffering,defaultto20-ssSAMPLE_SIZE,--sample_sizeSAMPLE_SIZESamplesize,defaultto4Advanced usageFirst check theAPI documentationfor more advanced usage.If you are a more experienced user, you can access theC-Style APIdirectly, almost all functions fromwhisper.hare exposed with the binding module_pywhispercpp.import_pywhispercppaspwcppctx=pwcpp.whisper_init_from_file('path/to/ggml/model')Discussions and contributionsIf you find any bug, please open anissue.If you have any feedback, or you want to share how you are using this project, feel free to use theDiscussionsand open a new topic.LicenseThis project is licensed under the same license aswhisper.cpp(MITLicense).