Instructions to use MoYoYoTech/Translator with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use MoYoYoTech/Translator with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf MoYoYoTech/Translator:Q5_0 # Run inference directly in the terminal: llama cli -hf MoYoYoTech/Translator:Q5_0
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf MoYoYoTech/Translator:Q5_0 # Run inference directly in the terminal: llama cli -hf MoYoYoTech/Translator:Q5_0
Build from source code
git clone https://github.com/ggml-org/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-app # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama serve -hf MoYoYoTech/Translator:Q5_0 # Run inference directly in the terminal: ./build/bin/llama cli -hf MoYoYoTech/Translator:Q5_0
Use Docker
docker model run hf.co/MoYoYoTech/Translator:Q5_0
- Llama new
- LM Studio
- Jan
- Ollama
How to use MoYoYoTech/Translator with Ollama:
ollama run hf.co/MoYoYoTech/Translator:Q5_0
- Unsloth Desktop
- Pi
How to use MoYoYoTech/Translator with Pi:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf MoYoYoTech/Translator:Q5_0
Configure the model in Pi
# Install Pi: npm install -g @earendil-works/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "llama-cpp": { "baseUrl": "http://localhost:9931/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "MoYoYoTech/Translator:Q5_0" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Docker Model Runner
How to use MoYoYoTech/Translator with Docker Model Runner:
docker model run hf.co/MoYoYoTech/Translator:Q5_0
- Lemonade
How to use MoYoYoTech/Translator with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull MoYoYoTech/Translator:Q5_0
Run and chat with the model
lemonade run user.Translator-Q5_0
List all available models
lemonade list
- Hermes Agent
How to use MoYoYoTech/Translator with Hermes Agent:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf MoYoYoTech/Translator:Q5_0
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:9931/v1 hermes config set model.default MoYoYoTech/Translator:Q5_0
Run Hermes
hermes
- Atomic Chat
- OpenClaw
How to use MoYoYoTech/Translator with OpenClaw:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf MoYoYoTech/Translator:Q5_0
Configure OpenClaw
# Install OpenClaw: npm install -g openclaw@latest # Register the local server and set it as the default model: openclaw onboard --non-interactive --mode local \ --auth-choice custom-api-key \ --custom-base-url http://127.0.0.1:9931/v1 \ --custom-model-id "MoYoYoTech/Translator:Q5_0" \ --custom-provider-id llama-cpp \ --custom-compatibility openai \ --custom-text-input \ --accept-risk \ --skip-health
Run OpenClaw
openclaw agent --local --agent main --message "Hello from Hugging Face"
daihui.zhang commited on
Commit ·
586518f
1
Parent(s): a257a82
init whispercpp transcirption
Browse filesFiles changed (16)
- .gitignore-1
- .python-versionAdded+1
- config.pyAdded+19
- pyproject.tomlAdded+19
- requirements.txtAdded+114
- run_client.py+1-1
- transcribe/__pycache__/__init__.cpython-311.pycDeleted
- transcribe/__pycache__/client.cpython-311.pycDeleted
- transcribe/__pycache__/server.cpython-311.pycDeleted
- transcribe/__pycache__/utils.cpython-311.pycDeleted
- transcribe/__pycache__/vad.cpython-311.pycDeleted
- transcribe/server/__init__.pyAdded+2
- transcribe/server.py → transcribe/server/base.pyRenamed+14-315
- transcribe/server/transcription.pyAdded+316
- transcribe/server/whispercpp.pyAdded+326
- uv.lockAdded
1234567__pycache__/*.py[cod]*$py.class# C extensions*.so123456__pycache__/*.py[cod]*$py.class# C extensions*.so
13.1112345678910111213141516171819import pathlib
BASE_DIR = pathlib.Path(__file__).parentMODEL_DIR = BASE_DIR / "moyoyo_asr_models"# 标点SENTENCE_END_MARKERS = ['.', '!', '?', '。', '!', '?', ';', ';', ':', ':']PAUSE_END_MARKERS = [',', ',', '、']
# whisper推理参数WHISPER_PROMPT_ZH = "以下是简体中文普通话的句子。" MAX_LENTH_ZH = 3
WHISPER_PROMPT_EN = "The following is an English sentence."MAX_LENGTH_EN= 3
WHISPER_MODEL = 'medium-q5_0'
12345678910111213141516171819[project]name = "trans"version = "0.1.0"description = "Add your description here"readme = "README.md"requires-python = ">=3.11"dependencies = [ "av>=14.2.0", "librosa>=0.11.0", "numpy>=2.1.3", "onnxruntime>=1.21.0", "pyaudio>=0.2.14", "setuptools>=78.1.0", "soundfile>=0.13.1", "torch>=2.6.0", "tqdm>=4.67.1", "websocket-client>=1.8.0", "websockets>=15.0.1",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114# This file was autogenerated by uv via the following command:# uv pip compile pyproject.toml -o requirements.txtaudioread==3.0.1 # via librosaav==14.3.0 # via trans (pyproject.toml)certifi==2025.1.31 # via requestscffi==1.17.1 # via soundfilecharset-normalizer==3.4.1 # via requestscoloredlogs==15.0.1 # via onnxruntimedecorator==5.2.1 # via librosafilelock==3.18.0 # via torchflatbuffers==25.2.10 # via onnxruntimefsspec==2025.3.2 # via torchhumanfriendly==10.0 # via coloredlogsidna==3.10 # via requestsjinja2==3.1.6 # via torchjoblib==1.4.2 # via # librosa # scikit-learnlazy-loader==0.4 # via librosalibrosa==0.11.0 # via trans (pyproject.toml)llvmlite==0.44.0 # via numbamarkupsafe==3.0.2 # via jinja2mpmath==1.3.0 # via sympymsgpack==1.1.0 # via librosanetworkx==3.4.2 # via torchnumba==0.61.0 # via librosanumpy==2.1.3 # via # trans (pyproject.toml) # librosa # numba # onnxruntime # scikit-learn # scipy # soundfile # soxronnxruntime==1.21.0 # via trans (pyproject.toml)packaging==24.2 # via # lazy-loader # onnxruntime # poochplatformdirs==4.3.7 # via poochpooch==1.8.2 # via librosaprotobuf==6.30.2 # via onnxruntimepyaudio==0.2.14 # via trans (pyproject.toml)pycparser==2.22 # via cffirequests==2.32.3 # via poochscikit-learn==1.6.1 # via librosascipy==1.15.2 # via # librosa # scikit-learnsetuptools==78.1.0 # via trans (pyproject.toml)soundfile==0.13.1 # via # trans (pyproject.toml) # librosasoxr==0.5.0.post1 # via librosasympy==1.13.1 # via # onnxruntime # torchthreadpoolctl==3.6.0 # via scikit-learntorch==2.6.0 # via trans (pyproject.toml)tqdm==4.67.1 # via trans (pyproject.toml)typing-extensions==4.13.1 # via # librosa # torchurllib3==2.3.0 # via requestswebsocket-client==1.8.0 # via trans (pyproject.toml)websockets==15.0.1 # via trans (pyproject.toml)
llama-cpp-pythonpywhispercpp1 unmodified line23456781 unmodified lineclient = TranscriptionClient("localhost",9000,lang="zh",save_output_recording=False, # Only used for microphone input, False by Defaultoutput_recording_filename="./output_recording.wav", # Only used for microphone input1 unmodified line23456781 unmodified lineclient = TranscriptionClient("localhost",9090,lang="zh",save_output_recording=False, # Only used for microphone input, False by Defaultoutput_recording_filename="./output_recording.wav", # Only used for microphone input
12
from .transcription import TranscriptionServer123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324117 unmodified lines442443444445446447101 unmodified lines54955055155255355455555655755851 unmodified lines610611612613614615616617import functoolsimport jsonimport loggingimport pathlibimport threadingimport timefrom enum import Enumfrom typing import List, Optionalimport librosaimport numpy as npimport soundfilefrom pywhispercpp.model import Modelfrom websockets.exceptions import ConnectionClosedfrom websockets.sync.server import servefrom transcribe.vad import VoiceActivityDetectorlogging.basicConfig(level=logging.INFO)class ClientManager:def __init__(self, max_clients=4, max_connection_time=600):"""Initializes the ClientManager with specified limits on client connections and connection durations.Args:max_clients (int, optional): The maximum number of simultaneous client connections allowed. Defaults to 4.max_connection_time (int, optional): The maximum duration (in seconds) a client can stay connected. Defaultsto 600 seconds (10 minutes)."""self.clients = {}self.start_times = {}self.max_clients = max_clientsself.max_connection_time = max_connection_timedef add_client(self, websocket, client):"""Adds a client and their connection start time to the tracking dictionaries.Args:websocket: The websocket associated with the client to add.client: The client object to be added and tracked."""self.clients[websocket] = clientself.start_times[websocket] = time.time()def get_client(self, websocket):"""Retrieves a client associated with the given websocket.Args:websocket: The websocket associated with the client to retrieve.Returns:The client object if found, False otherwise."""if websocket in self.clients:return self.clients[websocket]return Falsedef remove_client(self, websocket):"""Removes a client and their connection start time from the tracking dictionaries. Performs cleanup on theclient if necessary.Args:websocket: The websocket associated with the client to be removed."""client = self.clients.pop(websocket, None)if client:client.cleanup()self.start_times.pop(websocket, None)def get_wait_time(self):"""Calculates the estimated wait time for new clients based on the remaining connection times of current clients.Returns:The estimated wait time in minutes for new clients to connect. Returns 0 if there are available slots."""wait_time = Nonefor start_time in self.start_times.values():current_client_time_remaining = self.max_connection_time - (time.time() - start_time)if wait_time is None or current_client_time_remaining < wait_time:wait_time = current_client_time_remainingreturn wait_time / 60 if wait_time is not None else 0def is_server_full(self, websocket, options):"""Checks if the server is at its maximum client capacity and sends a wait message to the client if necessary.Args:websocket: The websocket of the client attempting to connect.options: A dictionary of options that may include the client's unique identifier.Returns:True if the server is full, False otherwise."""if len(self.clients) >= self.max_clients:wait_time = self.get_wait_time()response = {"uid": options["uid"], "status": "WAIT", "message": wait_time}websocket.send(json.dumps(response))return Truereturn Falsedef is_client_timeout(self, websocket):"""Checks if a client has exceeded the maximum allowed connection time and disconnects them if so, issuing a warning.Args:websocket: The websocket associated with the client to check.Returns:True if the client's connection time has exceeded the maximum limit, False otherwise."""elapsed_time = time.time() - self.start_times[websocket]if elapsed_time >= self.max_connection_time:self.clients[websocket].disconnect()logging.warning(f"Client with uid '{self.clients[websocket].client_uid}' disconnected due to overtime.")return Truereturn Falseclass BackendType(Enum):PYWHISPERCPP = "pywhispercpp"@staticmethoddef valid_types() -> List[str]:return [backend_type.value for backend_type in BackendType]@staticmethoddef is_valid(backend: str) -> bool:return backend in BackendType.valid_types()def is_pywhispercpp(self) -> bool:return self == BackendType.PYWHISPERCPPclass TranscriptionServer:RATE = 16000def __init__(self):self.client_manager = Noneself.no_voice_activity_chunks = 0self.single_model = Falsedef initialize_client(self, websocket, options):client: Optional[ServeClientBase] = Noneif self.backend.is_pywhispercpp():client = ServeClientWhisperCPP(websocket,language=options["language"],client_uid=options["uid"],single_model=self.single_model,)logging.info("Running pywhispercpp backend.")if client is None:raise ValueError(f"Backend type {self.backend.value} not recognised or not handled.")self.client_manager.add_client(websocket, client)def get_audio_from_websocket(self, websocket):"""Receives audio buffer from websocket and creates a numpy array out of it.Args:websocket: The websocket to receive audio from.Returns:A numpy array containing the audio."""frame_data = websocket.recv()if frame_data == b"END_OF_AUDIO":return Falsereturn np.frombuffer(frame_data, dtype=np.float32)def handle_new_connection(self, websocket):try:logging.info("New client connected")options = websocket.recv()options = json.loads(options)if self.client_manager is None:max_clients = options.get('max_clients', 4)max_connection_time = options.get('max_connection_time', 600)self.client_manager = ClientManager(max_clients, max_connection_time)if self.client_manager.is_server_full(websocket, options):websocket.close()return False # Indicates that the connection should not continueif self.backend.is_pywhispercpp():self.vad_detector = VoiceActivityDetector(frame_rate=self.RATE)self.initialize_client(websocket, options)return Trueexcept json.JSONDecodeError:logging.error("Failed to decode JSON from client")return Falseexcept ConnectionClosed:logging.info("Connection closed by client")return Falseexcept Exception as e:logging.error(f"Error during new connection initialization: {str(e)}")return Falsedef process_audio_frames(self, websocket):frame_np = self.get_audio_from_websocket(websocket)client = self.client_manager.get_client(websocket)# TODO Vad has some problem, it will be blocking process loop# if frame_np is False:# if self.backend.is_pywhispercpp():# client.set_eos(True)# return False# if self.backend.is_pywhispercpp():# voice_active = self.voice_activity(websocket, frame_np)# if voice_active:# self.no_voice_activity_chunks = 0# client.set_eos(False)# if self.use_vad and not voice_active:# return Trueclient.add_frames(frame_np)return Truedef recv_audio(self,websocket,backend: BackendType = BackendType.PYWHISPERCPP):self.backend = backendif not self.handle_new_connection(websocket):returntry:while not self.client_manager.is_client_timeout(websocket):if not self.process_audio_frames(websocket):breakexcept ConnectionClosed:logging.info("Connection closed by client")except Exception as e:logging.error(f"Unexpected error: {str(e)}")finally:if self.client_manager.get_client(websocket):self.cleanup(websocket)websocket.close()del websocketdef run(self,host,port=9090,backend="pywhispercpp"):"""Run the transcription server.Args:host (str): The host address to bind the server.port (int): The port number to bind the server."""if not BackendType.is_valid(backend):raise ValueError(f"{backend} is not a valid backend type. Choose backend from {BackendType.valid_types()}")with serve(functools.partial(self.recv_audio,backend=BackendType(backend),),host,port) as server:server.serve_forever()def voice_activity(self, websocket, frame_np):"""Evaluates the voice activity in a given audio frame and manages the state of voice activity detection.This method uses the configured voice activity detection (VAD) model to assess whether the given audio framecontains speech. If the VAD model detects no voice activity for more than three consecutive frames,it sets an end-of-speech (EOS) flag for the associated client. This method aims to efficiently managespeech detection to improve subsequent processing steps.Args:websocket: The websocket associated with the current client. Used to retrieve the client objectfrom the client manager for state management.frame_np (numpy.ndarray): The audio frame to be analyzed. This should be a NumPy array containingthe audio data for the current frame.Returns:bool: True if voice activity is detected in the current frame, False otherwise. When returning Falseafter detecting no voice activity for more than three consecutive frames, it also triggers theend-of-speech (EOS) flag for the client."""if not self.vad_detector(frame_np):self.no_voice_activity_chunks += 1if self.no_voice_activity_chunks > 3:client = self.client_manager.get_client(websocket)if not client.eos:client.set_eos(True)time.sleep(0.1) # Sleep 100m; wait some voice activity.return Falsereturn Truedef cleanup(self, websocket):"""Cleans up resources associated with a given client's websocket.Args:websocket: The websocket associated with the client to be cleaned up."""if self.client_manager.get_client(websocket):self.client_manager.remove_client(websocket)class ServeClientBase(object):RATE = 16000SERVER_READY = "SERVER_READY"117 unmodified linessegments = self.transcript.copy()if last_segment is not None:segments = segments + [last_segment]return segmentsdef get_audio_chunk_duration(self, input_bytes):101 unmodified lines"""Instantiates a new model, sets it as the transcriber and does warmup if desired."""model = 'medium-q5_0'here = pathlib.Path(__file__)models_dir = f'{here.parent.parent / "moyoyo_asr_models"}'self.transcriber = Model(model=model, models_dir=models_dir)if warmup:self.warmup()51 unmodified linesprompt = '以下是简体中文普通话的句子。'else:prompt = 'The following is an English sentence.'segments = self.transcriber.transcribe(mel, language='zh', initial_prompt=prompt, print_progress=False)text = []for segment in segments:content = segment.text1234567891011121314151617117 unmodified lines135136137138139140141101 unmodified lines24324424524624724824925051 unmodified lines302303304305306307308309310311312313314315316import jsonimport loggingimport pathlibimport threadingimport timeimport configimport librosaimport numpy as npimport soundfilefrom pywhispercpp.model import Modellogging.basicConfig(level=logging.INFO)class ServeClientBase(object):RATE = 16000SERVER_READY = "SERVER_READY"117 unmodified linessegments = self.transcript.copy()if last_segment is not None:segments = segments + [last_segment]logging.info(f"{segments}")return segmentsdef get_audio_chunk_duration(self, input_bytes):101 unmodified lines"""Instantiates a new model, sets it as the transcriber and does warmup if desired."""self.transcriber = Model(model=config.WHISPER_MODEL, models_dir=config.MODEL_DIR)if warmup:self.warmup()51 unmodified linesprompt = '以下是简体中文普通话的句子。'else:prompt = 'The following is an English sentence.'segments = self.transcriber.transcribe(mel,language=self.language,initial_prompt=prompt,token_timestamps=True,# max_len=max_len,print_progress=False)text = []for segment in segments:content = segment.text
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316import logging import timeimport functoolsimport jsonimport loggingimport timefrom enum import Enumfrom typing import List, Optionalimport numpy as npfrom .base import ServeClientBase, ServeClientWhisperCPPfrom .whispercpp import PyWhiperCppServefrom ..vad import VoiceActivityDetectorfrom websockets.exceptions import ConnectionClosedfrom websockets.sync.server import serve
logging.basicConfig(level=logging.INFO)
class ClientManager: def __init__(self, max_clients=4, max_connection_time=600): """ Initializes the ClientManager with specified limits on client connections and connection durations.
Args: max_clients (int, optional): The maximum number of simultaneous client connections allowed. Defaults to 4. max_connection_time (int, optional): The maximum duration (in seconds) a client can stay connected. Defaults to 600 seconds (10 minutes). """ self.clients = {} self.start_times = {} self.max_clients = max_clients self.max_connection_time = max_connection_time
def add_client(self, websocket, client): """ Adds a client and their connection start time to the tracking dictionaries.
Args: websocket: The websocket associated with the client to add. client: The client object to be added and tracked. """ self.clients[websocket] = client self.start_times[websocket] = time.time()
def get_client(self, websocket): """ Retrieves a client associated with the given websocket.
Args: websocket: The websocket associated with the client to retrieve.
Returns: The client object if found, False otherwise. """ if websocket in self.clients: return self.clients[websocket] return False
def remove_client(self, websocket): """ Removes a client and their connection start time from the tracking dictionaries. Performs cleanup on the client if necessary.
Args: websocket: The websocket associated with the client to be removed. """ client = self.clients.pop(websocket, None) if client: client.cleanup() self.start_times.pop(websocket, None)
def get_wait_time(self): """ Calculates the estimated wait time for new clients based on the remaining connection times of current clients.
Returns: The estimated wait time in minutes for new clients to connect. Returns 0 if there are available slots. """ wait_time = None for start_time in self.start_times.values(): current_client_time_remaining = self.max_connection_time - (time.time() - start_time) if wait_time is None or current_client_time_remaining < wait_time: wait_time = current_client_time_remaining return wait_time / 60 if wait_time is not None else 0
def is_server_full(self, websocket, options): """ Checks if the server is at its maximum client capacity and sends a wait message to the client if necessary.
Args: websocket: The websocket of the client attempting to connect. options: A dictionary of options that may include the client's unique identifier.
Returns: True if the server is full, False otherwise. """ if len(self.clients) >= self.max_clients: wait_time = self.get_wait_time() response = {"uid": options["uid"], "status": "WAIT", "message": wait_time} websocket.send(json.dumps(response)) return True return False
def is_client_timeout(self, websocket): """ Checks if a client has exceeded the maximum allowed connection time and disconnects them if so, issuing a warning.
Args: websocket: The websocket associated with the client to check.
Returns: True if the client's connection time has exceeded the maximum limit, False otherwise. """ elapsed_time = time.time() - self.start_times[websocket] if elapsed_time >= self.max_connection_time: self.clients[websocket].disconnect() logging.warning(f"Client with uid '{self.clients[websocket].client_uid}' disconnected due to overtime.") return True return False
class BackendType(Enum): PYWHISPERCPP = "pywhispercpp"
@staticmethod def valid_types() -> List[str]: return [backend_type.value for backend_type in BackendType]
@staticmethod def is_valid(backend: str) -> bool: return backend in BackendType.valid_types()
def is_pywhispercpp(self) -> bool: return self == BackendType.PYWHISPERCPP
class TranscriptionServer: RATE = 16000
def __init__(self): self.client_manager = None self.no_voice_activity_chunks = 0 self.single_model = False
def initialize_client( self, websocket, options ): client: Optional[ServeClientBase] = None
if self.backend.is_pywhispercpp(): client = PyWhiperCppServe( websocket, language=options["language"], client_uid=options["uid"], ) logging.info("Running pywhispercpp backend.")
if client is None: raise ValueError(f"Backend type {self.backend.value} not recognised or not handled.")
self.client_manager.add_client(websocket, client)
def get_audio_from_websocket(self, websocket): """ Receives audio buffer from websocket and creates a numpy array out of it.
Args: websocket: The websocket to receive audio from.
Returns: A numpy array containing the audio. """ frame_data = websocket.recv() if frame_data == b"END_OF_AUDIO": return False return np.frombuffer(frame_data, dtype=np.float32)
def handle_new_connection(self, websocket): try: logging.info("New client connected") options = websocket.recv() options = json.loads(options)
if self.client_manager is None: max_clients = options.get('max_clients', 4) max_connection_time = options.get('max_connection_time', 600) self.client_manager = ClientManager(max_clients, max_connection_time)
if self.client_manager.is_server_full(websocket, options): websocket.close() return False # Indicates that the connection should not continue
if self.backend.is_pywhispercpp(): self.vad_detector = VoiceActivityDetector(frame_rate=self.RATE)
self.initialize_client(websocket, options)
return True except json.JSONDecodeError: logging.error("Failed to decode JSON from client") return False except ConnectionClosed: logging.info("Connection closed by client") return False except Exception as e: logging.error(f"Error during new connection initialization: {str(e)}") return False
def process_audio_frames(self, websocket): frame_np = self.get_audio_from_websocket(websocket) client = self.client_manager.get_client(websocket)
# TODO Vad has some problem, it will be blocking process loop # if frame_np is False: # if self.backend.is_pywhispercpp(): # client.set_eos(True) # return False
# if self.backend.is_pywhispercpp(): # voice_active = self.voice_activity(websocket, frame_np) # if voice_active: # self.no_voice_activity_chunks = 0 # client.set_eos(False) # if self.use_vad and not voice_active: # return True
client.add_frames(frame_np) return True
def recv_audio(self, websocket, backend: BackendType = BackendType.PYWHISPERCPP):
self.backend = backend if not self.handle_new_connection(websocket): return
try: while not self.client_manager.is_client_timeout(websocket): if not self.process_audio_frames(websocket): break except ConnectionClosed: logging.info("Connection closed by client") except Exception as e: logging.error(f"Unexpected error: {str(e)}") finally: if self.client_manager.get_client(websocket): self.cleanup(websocket) websocket.close() del websocket
def run(self, host, port=9090, backend="pywhispercpp"): """ Run the transcription server.
Args: host (str): The host address to bind the server. port (int): The port number to bind the server. """
if not BackendType.is_valid(backend): raise ValueError(f"{backend} is not a valid backend type. Choose backend from {BackendType.valid_types()}")
with serve( functools.partial( self.recv_audio, backend=BackendType(backend), ), host, port ) as server: server.serve_forever()
def voice_activity(self, websocket, frame_np): """ Evaluates the voice activity in a given audio frame and manages the state of voice activity detection.
This method uses the configured voice activity detection (VAD) model to assess whether the given audio frame contains speech. If the VAD model detects no voice activity for more than three consecutive frames, it sets an end-of-speech (EOS) flag for the associated client. This method aims to efficiently manage speech detection to improve subsequent processing steps.
Args: websocket: The websocket associated with the current client. Used to retrieve the client object from the client manager for state management. frame_np (numpy.ndarray): The audio frame to be analyzed. This should be a NumPy array containing the audio data for the current frame.
Returns: bool: True if voice activity is detected in the current frame, False otherwise. When returning False after detecting no voice activity for more than three consecutive frames, it also triggers the end-of-speech (EOS) flag for the client. """ if not self.vad_detector(frame_np): self.no_voice_activity_chunks += 1 if self.no_voice_activity_chunks > 3: client = self.client_manager.get_client(websocket) if not client.eos: client.set_eos(True) time.sleep(0.1) # Sleep 100m; wait some voice activity. return False return True
def cleanup(self, websocket): """ Cleans up resources associated with a given client's websocket.
Args: websocket: The websocket associated with the client to be cleaned up. """ if self.client_manager.get_client(websocket): self.client_manager.remove_client(websocket)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
from .base import ServeClientBasefrom pywhispercpp.model import Modelimport soundfilefrom concurrent.futures import ProcessPoolExecutor as Poolimport numpy as npfrom logging import getLoggerfrom difflib import SequenceMatcherimport collectionsimport configimport timeimport jsonimport threading
logger = getLogger("Pywhisper")
class TripleTextBuffer: def __init__(self, size=2): self.history = collections.deque(maxlen=size)
def _clean(self): self.history.clear() def add_entry(self, text, index): """ text: 文本 index: 当前buffer的相对下标 数组索引 """ self.history.append((text, index))
def get_final_index(self, similarity_threshold=0.7): """根据文本变化,返回可靠的标点的buffer的位置下标""" if len(self.history) < 2: return None
# 获取三次的文本 text1, _ = self.history[0] text2, idx2 = self.history[1] # text3, idx3 = self.history[2]
# 计算变化程度 sim_12 = self.text_similarity(text1, text2) # print("比较: ", text1, text2," => ", sim_12) # sim_23 = self.text_similarity(text2, text3) if sim_12 >= similarity_threshold: self._clean() return idx2 return None
@staticmethod def text_similarity(text1, text2): return SequenceMatcher(None, text1, text2).ratio()
class SegmentManager: def __init__(self) -> None: self._commited_segments = [] # 确定后的段落 self._commited_short_sentences = [] # 确定后的序列 self._temp_string = "" # 存储当前临时的文本字符串,直到以句号结尾
def handle(self, string): self._temp_string = string return self @property def short_sentence(self) -> str: return "".join(self._commited_short_sentences) @property def segment(self): return self._commited_segments[-1] if len(self._commited_segments) > 0 else "" def get_seg_id(self): return len(self._commited_segments) @property def string(self): return self._temp_string def commit_short_sentence(self): """将临时字符串 提交到临时短句""" self._commited_short_sentences.append(self._temp_string) self._temp_string = "" def commit_segment(self): """将短句 合并 到长句中""" self._commited_segments.append(self.short_sentence) self._commited_short_sentences = []
def commit(self, is_end_sentence=False): """ 当需要切掉的音频部分的时候,将句子提交到短句队列中,并移除临时字符串 当完成一个整句的时候提交到段落中 """ self.commit_short_sentence() if is_end_sentence: self.commit_segment()
class PywhisperInference:
model = None
@classmethod def initializer(cls, warmup=True): models_dir = config.MODEL_DIR.as_posix() cls.model = Model( model=config.WHISPER_MODEL, models_dir=models_dir, print_realtime=False, print_progress=False, print_timestamps=False, ) if warmup: cls.warmup() @classmethod def warmup(cls, warmup_steps=1): mel, _, = soundfile.read("assets/jfk.flac") for _ in range(warmup_steps): cls.model.transcribe(mel, print_progress=False)
@staticmethod def config_language(language): if language == "zh": return config.MAX_LENTH_ZH, config.WHISPER_PROMPT_ZH elif language == "en": return config.MAX_LENTH_ZH, config.WHISPER_PROMPT_ZH raise ValueError(f"Unsupported language : {language}")
@classmethod def inference(cls, audio_buffer, language): max_len, prompt = cls.config_language(language) audio_buffer = np.frombuffer(audio_buffer, dtype=np.float32) return cls.model.transcribe( audio_buffer, initial_prompt=prompt, language=language, token_timestamps=True, max_len=max_len )
class PyWhiperCppServe(ServeClientBase): def __init__(self, websocket, language=None, client_uid=None,): super().__init__(client_uid, websocket) self.language = language # 设置观察字符串 对比上下次的文字来判断字符串的输出是否固定 self._text_buffer = TripleTextBuffer() # 存储转录数据 self._segment_manager = SegmentManager() self.lock = threading.Lock() self.frames_np = None self.sample_rate = 16000
self._pool = Pool( max_workers=1, initializer=PywhisperInference.initializer)
logger.info('Create a thread to process audio.') self.trans_thread = threading.Thread(target=self.speech_to_text) self.trans_thread.start()
self.websocket.send(json.dumps({ "uid": self.client_uid, "message": self.SERVER_READY, "backend": "pywhispercpp" }))
def add_frames(self, frame_np): with self.lock: if self.frames_np is None: self.frames_np = frame_np.copy() else: self.frames_np = np.append(self.frames_np,frame_np)
def update_audio_buffer(self, last_offset): with self.lock: self.frames_np = self.frames_np[last_offset:]
def transcribe_audio(self, audio_buffer): """ Transcribe the audio chunk and send the results to the client.
Args: audio_buffer (np.array): The audio chunk to transcribe. """ fut = self._pool.submit( PywhisperInference.inference, audio_buffer.tobytes(), self.language) return fut.result()
def _segments_split(self, segments, audio_buffer: np.ndarray): """根据左边第一个标点符号来将序列拆分成 观察段 和 剩余部分""" left_watch_sequences = [] left_watch_idx = 0 right_watch_sequences = [] is_end = False
if (len(audio_buffer) / self.sample_rate) < 10: # 低于10s 使用短句符号比如逗号作为判断依据 markers = config.PAUSE_END_MARKERS is_end = False else: # 使用句号 问好等长句结尾符号作为判断 markers = config.SENTENCE_END_MARKERS is_end = True
for idx, seg in enumerate(segments): left_watch_sequences.append(seg) if seg.text in markers: seg_index = int(seg.t1 / 100 * self.sample_rate) rest_buffer_duration = (len(audio_buffer) - seg_index) / self.sample_rate # is_end = any(i in seg.text for i in config.SENTENCE_END_MARKERS) right_watch_sequences = segments[min(idx+1, len(segments)):] if rest_buffer_duration >= 1.5: left_watch_idx = seg_index break return left_watch_idx, left_watch_sequences, right_watch_sequences, is_end def analysis_segments(self, segments, audio_buffer: np.ndarray): # 找到第一个标点符号作为锚点 左边为确认段,右边为观察段, # 当左边确认后,右边段才会进入观察 # 当左边确认后,会从缓冲区中删除对应的buffer,减少下次输入的数据量 left_watch_idx, left_watch_sequences, right_watch_sequences, is_end_sentence = self._segments_split(segments, audio_buffer) left_watch_string = "".join(i.text for i in left_watch_sequences) right_watch_string = "".join(i.text for i in right_watch_sequences) if left_watch_idx != 0: # 将观察字符串临时存储 self._text_buffer.add_entry(left_watch_string, left_watch_idx) audio_cut_index = self._text_buffer.get_final_index() if audio_cut_index: return audio_cut_index, left_watch_string, right_watch_string, is_end_sentence return None, left_watch_string, right_watch_string, is_end_sentence
def speech_to_text(self): while True: if self.exit: logger.info("Exiting speech to text thread") self._pool.shutdown(wait=False, cancel_futures=True) break if self.frames_np is None: time.sleep(0.02) # wait for any audio to arrive continue
audio_buffer = self.get_audio_chunk_for_processing() # logger.info(f"[pywhispercpp:] Processing audio with duration: {len(audio_buffer)}") # segments = self.transcribe_audio(audio_buffer) try: logger.info(f"[pywhispercpp:] Processing audio with duration: {len(audio_buffer)}") segments = self.transcribe_audio(audio_buffer) except KeyboardInterrupt: break except Exception as e: logger.error(f"[ERROR]: {e}") else: self.handle_transcription_output(segments, audio_buffer) def handle_transcription_output(self, segments, audio_buffer): texts = "".join(i.text for i in segments) self._segment_manager.handle(texts) # 分析句子 last_cut_index, left_string, right_string, is_end_sentence = self.analysis_segments(segments, audio_buffer) # print(last_cut_index, left_string, right_string, is_end_sentence) if last_cut_index: self.update_audio_buffer(last_cut_index) # 句子或者短句的提交 self._segment_manager.handle(left_string).commit(is_end_sentence) self._segment_manager.handle(right_string) if is_end_sentence and last_cut_index: message = self._segment_manager.segment seg_id = self._segment_manager.get_seg_id() - 1 # elapsed_time = time.time() - start_time # formatted_time = f"{int(elapsed_time // 60):02}:{int(elapsed_time % 60):02}:{(elapsed_time % 1) * 1000:03.0f}" print(seg_id, message,) print(seg_id + 1, self._segment_manager.string,) else: seg_id = self._segment_manager.get_seg_id() message = self._segment_manager.short_sentence + self._segment_manager.string # print(self._segment_manager.__dict__) # elapsed_time = time.time() - start_time # formatted_time = f"{int(elapsed_time // 60):02}:{int(elapsed_time % 60):02}:{(elapsed_time % 1) * 1000:03.0f}" print(seg_id, message) def send_to_client(self, data_dict): content = { "uid": self.client_uid, **data_dict } try: self.websocket.send( json.dumps(content) ) except Exception as e: logger.error(f"[ERROR]: Sending data to client: {e}")
def get_audio_chunk_for_processing(self): if self.frames_np.shape[0] >= self.sample_rate * 1: return self.frames_np.copy() # 计算需要填充的样本数 padding_length = self.sample_rate * 1 - len(self.frames_np) # 创建静音填充(零值) silence = np.zeros(padding_length + int(0.01 * self.sample_rate), dtype=np.float32) # 拼接原始音频和静音填充 padded_audio = np.concatenate([silence, self.frames_np]) return padded_audio.copy()
def cleanup(self): logger.info("start shut down worker pool.") self._pool.shutdown(wait=False, cancel_futures=True) logger.info("shut down worker pool success.") return super().cleanup()#.gitignore-1
.python-versionAdded+1
config.pyAdded+19
pyproject.tomlAdded+19
requirements.txtAdded+114
run_client.py+1-1
transcribe/__pycache__/__init__.cpython-311.pycDeleted
transcribe/__pycache__/client.cpython-311.pycDeleted
transcribe/__pycache__/server.cpython-311.pycDeleted
transcribe/__pycache__/utils.cpython-311.pycDeleted
transcribe/__pycache__/vad.cpython-311.pycDeleted
transcribe/server/__init__.pyAdded+2
transcribe/server.py → transcribe/server/base.pyRenamed+14-315
transcribe/server/transcription.pyAdded+316
transcribe/server/whispercpp.pyAdded+326
uv.lockAdded