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"
david commited on
Commit ·
0c38083
1
Parent(s): dfb349e
update code for readability
Browse filesFiles changed (4)
- config.py+4
- main.py+2-2
- transcribe/strategy.py+246-119
- transcribe/whisper_llm_serve.py+243-195
16 unmodified lines17181920212216 unmodified lines# 标点SENTENCE_END_MARKERS = ['.', '!', '?', '。', '!', '?', ';', ';', ':', ':']PAUSE_END_MARKERS = [',', ',', '、']sentence_end_chars = ''.join([re.escape(char) for char in SENTENCE_END_MARKERS])SENTENCE_END_PATTERN = re.compile(f'[{sentence_end_chars}]')16 unmodified lines1718192021222324252616 unmodified lines# 标点SENTENCE_END_MARKERS = ['.', '!', '?', '。', '!', '?', ';', ';', ':', ':']PAUSE_END_MARKERS = [',', ',', '、']# 合并所有标点ALL_MARKERS = SENTENCE_END_MARKERS + PAUSE_END_MARKERS# 构造正则表达式字符类REGEX_MARKERS = re.compile(r'[' + re.escape(''.join(ALL_MARKERS)) + r']')sentence_end_chars = ''.join([re.escape(char) for char in SENTENCE_END_MARKERS])SENTENCE_END_PATTERN = re.compile(f'[{sentence_end_chars}]')
12345650 unmodified lines57585960616263from fastapi import FastAPI, WebSocket, WebSocketDisconnectfrom urllib.parse import urlparse, parse_qslfrom transcribe.whisper_llm_serve import PyWhiperCppServefrom uuid import uuid1from logging import getLoggerimport numpy as np50 unmodified linesasync def translate(websocket: WebSocket):query_parameters_dict = websocket.query_paramsfrom_lang, to_lang = query_parameters_dict.get('from'), query_parameters_dict.get('to')client = PyWhiperCppServe(websocket,pipe,language="en",12345650 unmodified lines57585960616263from fastapi import FastAPI, WebSocket, WebSocketDisconnectfrom urllib.parse import urlparse, parse_qslfrom transcribe.whisper_llm_serve import WhisperTranscriptionServicefrom uuid import uuid1from logging import getLoggerimport numpy as np50 unmodified linesasync def translate(websocket: WebSocket):query_parameters_dict = websocket.query_paramsfrom_lang, to_lang = query_parameters_dict.get('from'), query_parameters_dict.get('to')client = WhisperTranscriptionService(websocket,pipe,language="en",
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153from logging import getLoggerfrom difflib import SequenceMatcherimport collectionsimport configimport numpy as npfrom itertools import chainlogger = getLogger("Stragegy")class TripleTextBuffer:def __init__(self, size=2):self.history = collections.deque(maxlen=size)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.history.clear()return idx2return None@staticmethoddef 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 = stringreturn self@propertydef short_sentence(self) -> str:return "".join(self._commited_short_sentences)@propertydef segment(self):return self._commited_segments[-1] if len(self._commited_segments) > 0 else ""def get_seg_id(self):return len(self._commited_segments)@propertydef string(self):return self._temp_stringdef 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()def segement_merge(segments):"""根据标点符号分整句"""sequences = []temp_seq = []for seg in segments:temp_seq.append(seg)if any([mk in seg.text for mk in config.SENTENCE_END_MARKERS]):sequences.append(temp_seq.copy())temp_seq = []if temp_seq:sequences.append(temp_seq)return sequencesdef segments_split(segments, audio_buffer: np.ndarray, sample_rate=16000):"""根据左边第一个标点符号来将序列拆分成 观察段 和 剩余部分"""left_watch_sequences = []left_watch_idx = 0right_watch_sequences = []is_end = Falseif (len(audio_buffer) / sample_rate) < 12:# 低于12s 使用短句符号比如逗号作为判断依据markers = config.PAUSE_END_MARKERS + config.SENTENCE_END_MARKERSis_end = Falsefor idx, seg in enumerate(segments):# print('>>>>>>>>>>>>>>>> seg : ', seg)left_watch_sequences.append(seg)if seg.text and seg.text[-1] in markers:seg_index = int(seg.t1 / 100 * sample_rate)# rest_buffer_duration = (len(audio_buffer) - seg_index) / sample_rateis_end = config.SENTENCE_END_PATTERN.search(seg.text)right_watch_sequences = segments[min(idx+1, len(segments)):]# if rest_buffer_duration >= 1.5:left_watch_idx = seg_indexbreakreturn left_watch_idx, left_watch_sequences, right_watch_sequences, is_enddef sequences_split(segments, audio_buffer: np.ndarray, sample_rate=16000):# 长句 保留最后两句即可left_watch_sequences = []right_watch_sequences = []left_watch_idx = 0is_end = Falsesequences = segement_merge(segments)if len(sequences) > 2:logger.info(f"buffer clip via sequence, current length: {len(sequences)}")is_end = Trueleft_watch_sequences = chain(*sequences[:-2])right_watch_sequences = chain(*sequences[-2:])last_sequence_segment = sequences[-3]last_segment = last_sequence_segment[-1]left_watch_idx = int(last_segment.t1 / 100 * sample_rate)return left_watch_idx, left_watch_sequences, right_watch_sequences, is_end123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280import reimport collectionsimport loggingfrom difflib import SequenceMatcherfrom itertools import chainfrom dataclasses import dataclassfrom typing import List, Tuple, Optional, Deque, Any, Iteratorfrom config import SENTENCE_END_MARKERS, ALL_MARKERS,SENTENCE_END_PATTERN,REGEX_MARKERSimport numpy as nplogger = logging.getLogger("TranscriptionStrategy")@dataclassclass TranscriptSegment:"""表示一个转录片段,包含文本和时间信息"""text: strt0: float # 开始时间(百分之一秒)t1: float # 结束时间(百分之一秒)class TextStabilityBuffer:"""通过比较连续文本样本的相似度来确定转录文本的稳定性。当连续样本的相似度超过阈值时,认为文本已稳定。"""def __init__(self, max_history: int = 2):self.history: Deque[Tuple[str, int]] = collections.deque(maxlen=max_history)def add_entry(self, text: str, index: int) -> None:"""添加新的文本和索引到历史记录中Args:text: 文本内容index: 当前buffer的相对下标"""self.history.append((text, index))def get_stable_index(self, similarity_threshold: float = 0.7) -> Optional[int]:"""根据文本相似度,判断文本是否稳定,返回稳定文本的索引Args:similarity_threshold: 相似度阈值,超过此值认为文本稳定Returns:稳定文本的索引,如果没有找到稳定文本则返回None"""if len(self.history) < 2:return Nonetext1, _ = self.history[0]text2, idx2 = self.history[1]similarity = self._calculate_similarity(text1, text2)if similarity >= similarity_threshold:self.history.clear()return idx2return None@staticmethoddef _calculate_similarity(text1: str, text2: str) -> float:"""计算两段文本的相似度"""return SequenceMatcher(None, text1, text2).ratio()class TranscriptionManager:"""管理转录文本的分级结构:临时字符串 -> 短句 -> 完整段落|-- 已确认文本 --|-- 观察窗口 --|-- 新输入 --|"""def __init__(self):self._committed_segments: List[str] = [] # 确认的完整段落self._committed_sentences: List[str] = [] # 确认的短句self._temp_string: str = "" # 临时字符串缓冲@propertydef current_sentence(self) -> str:"""当前已确认的短句组合"""return "".join(self._committed_sentences)@propertydef latest_segment(self) -> str:"""最新确认的完整段落"""return self._committed_segments[-1] if self._committed_segments else ""@propertydef segment_count(self) -> int:"""已确认的段落数量"""return len(self._committed_segments)@propertydef sentence_length(self) -> int:"""当前短句的总字符长度"""return sum(len(s) for s in self._committed_sentences)def update_temp(self, text: str) -> 'TranscriptionManager':"""更新临时字符串"""self._temp_string = textreturn selfdef commit_sentence(self) -> None:"""将临时字符串提交到短句列表"""if self._temp_string:self._committed_sentences.append(self._temp_string)self._temp_string = ""def commit_segment(self, is_end_of_sentence: bool = False) -> None:"""提交当前内容到适当的层级Args:is_end_of_sentence: 是否为完整句子的结束"""self.commit_sentence()if is_end_of_sentence and self._committed_sentences:self._committed_segments.append(self.current_sentence)self._committed_sentences = []def get_all_text(self) -> str:"""获取所有已提交的文本"""all_segments = self._committed_segments.copy()if self.current_sentence:all_segments.append(self.current_sentence)if self._temp_string:all_segments.append(self._temp_string)return "\n".join(all_segments)class TranscriptionSplitter:"""负责根据语音和文本特征拆分转录片段"""@staticmethoddef group_by_sentences(segments: List[TranscriptSegment]) -> List[List[TranscriptSegment]]:"""将片段按照完整句子分组"""sequences = []temp_seq = []for seg in segments:temp_seq.append(seg)if any(marker in seg.text for marker in SENTENCE_END_MARKERS):sequences.append(temp_seq.copy())temp_seq = []if temp_seq:sequences.append(temp_seq)return sequences@staticmethoddef split_by_punctuation(segments: List[TranscriptSegment],audio_buffer: np.ndarray,sample_rate: int = 16000) -> Tuple[int, List[TranscriptSegment], List[TranscriptSegment], bool]:"""根据标点符号将片段分为左侧(已确认)和右侧(待确认)Returns:(分割索引, 左侧片段, 右侧片段, 是否为句子结束)"""left_segments = []right_segments = []split_index = 0is_sentence_end = False# 短音频使用所有标点符号作为分割依据buffer_duration = len(audio_buffer) / sample_ratemarkers = ALL_MARKERS if buffer_duration < 12 else SENTENCE_END_MARKERSfor idx, seg in enumerate(segments):left_segments.append(seg)if seg.text and seg.text[-1] in markers:split_index = int(seg.t1 / 100 * sample_rate)is_sentence_end = bool(SENTENCE_END_PATTERN.search(seg.text))right_segments = segments[min(idx+1, len(segments)):]breakreturn split_index, left_segments, right_segments, is_sentence_end@staticmethoddef split_by_sequences(segments: List[TranscriptSegment],audio_buffer: np.ndarray,sample_rate: int = 16000) -> Tuple[int, Iterator[TranscriptSegment], Iterator[TranscriptSegment], bool]:"""对于长文本,按照句子组保留最新的两句Returns:(分割索引, 左侧片段, 右侧片段, 是否为句子结束)"""sequences = TranscriptionSplitter.group_by_sentences(segments)if len(sequences) > 2:logger.info(f"Buffer clip via sequence, current length: {len(sequences)}")left_segments = chain(*sequences[:-2])right_segments = chain(*sequences[-2:])# 确定切分点last_sequence = sequences[-3]last_segment = last_sequence[-1]split_index = int(last_segment.t1 / 100 * sample_rate)return split_index, left_segments, right_segments, Truereturn 0, iter([]), iter(segments), Falseclass TranscriptionStabilizer:"""转录结果稳定器,负责确认和管理转录片段"""def __init__(self, sample_rate: int = 16000):self.manager = TranscriptionManager()self.stability_buffer = TextStabilityBuffer(max_history=2)self.sample_rate = sample_ratedef process_segments(self, segments: List[TranscriptSegment]) -> Tuple[Optional[int], bool]:"""处理转录片段,确认稳定的文本Args:segments: 转录片段列表Returns:(音频分割点索引, 是否达到足够长度需要换行)"""# 查找第一个包含标点的片段作为分割点split_index = Nonestable_segments = []for idx, seg in enumerate(segments):stable_segments.append(seg)if REGEX_MARKERS.search(seg.text):split_index = int(seg.t1 / 100 * self.sample_rate)stable_idx = min(idx + 1, len(segments))breakif split_index: # 找到标点,确认标点前的内容stable_text = self._join_segment_text(segments[:stable_idx])self.manager.update_temp(stable_text).commit_sentence()# 更新剩余文本remaining_text = self._join_segment_text(segments[stable_idx:])self.manager.update_temp(remaining_text)else:# 没有找到标点,全部作为临时文本self.manager.update_temp(self._join_segment_text(segments))# 检查是否达到换行标准should_linebreak = self.manager.sentence_length >= 20return split_index, should_linebreakdef check_stability(self, text: str, index: int) -> Optional[int]:"""检查文本是否稳定Args:text: 当前文本index: 当前索引Returns:如果文本稳定,返回稳定的索引;否则返回None"""self.stability_buffer.add_entry(text, index)return self.stability_buffer.get_stable_index()def commit_segment(self, is_end_of_sentence: bool) -> None:"""提交转录片段"""self.manager.commit_segment(is_end_of_sentence)@staticmethoddef _join_segment_text(segments: List[TranscriptSegment], separator: str = "") -> str:"""连接多个片段的文本"""return separator.join(seg.text for seg in segments)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261import numpy as npfrom logging import getLoggerimport asynciofrom .utils import save_to_waveimport timeimport jsonimport threadingfrom .server import ServeClientBaseimport queueimport collectionsfrom api_model import TransResult, Messagefrom .utils import log_blockfrom .translatepipes import TranslatePipesfrom .strategy import TripleTextBuffer, SegmentManager, segments_split, sequences_splitlogger = getLogger("TranslatorApp")class PyWhiperCppServe(ServeClientBase):def __init__(self, websocket, pipe:TranslatePipes,language=None, dst_lang=None, client_uid=None,):super().__init__(client_uid, websocket)self.language = languageself.dst_lang = dst_lang # 目标翻译语言# 设置观察字符串 对比上下次的文字来判断字符串的输出是否固定self._text_buffer = TripleTextBuffer()# 存储转录数据self._segment_manager = SegmentManager()self._translate_pipes = pipeself.lock = threading.Lock()self.frames_np = Noneself._frame_queue = queue.Queue()self.sample_rate = 16000self.send_ready_state()self._translate_thread_stop = threading.Event()self._frame_to_queue_thread_stop = threading.Event()self.translate_thread = self.run_in_thread(self.speech_to_text)self.frame_to_queue_thread = self.run_in_thread(self.get_frame_from_queue)self.text_sep = ""def run_in_thread(self, func):t = threading.Thread(target=func)t.daemon = Truet.start()return tdef send_ready_state(self):self.websocket.send(json.dumps({"uid": self.client_uid,"message": self.SERVER_READY,"backend": "pywhispercpp"}))def set_lang(self, src_lang, dst_lang):self.language = src_langself.dst_lang = dst_langself.text_sep = "" if self.language == "zh" else " "def add_frames(self, frame_np):self._frame_queue.put(frame_np)def vad_merge(self):with self.lock:frame = self.frames_np.copy()item = self._translate_pipes.voice_detect(frame.tobytes())frame_np = np.frombuffer(item.audio, dtype=np.float32)self.frames_np = frame_np.copy()def get_frame_from_queue(self,):while not self._frame_to_queue_thread_stop.is_set():try:frame_np = self._frame_queue.get(timeout=0.1)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)except queue.Empty:passdef 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."""log_block("Audio buffer length", f"{audio_buffer.shape[0]/self.sample_rate:.2f}", "s")start_time = time.perf_counter()item = self._translate_pipes.transcrible(audio_buffer.tobytes(), self.language)segments = item.segmentslog_block("Whisper transcrible out", f"{''.join(seg.text for seg in segments)}", "")log_block("Whisper transcrible time", f"{(time.perf_counter() - start_time):.3f}", "s")return segmentsdef translate_text(self, text):"""translate the text to dst lang"""# return "sample english"log_block("LLM translate input", f"{text}")start_time = time.perf_counter()ret = self._translate_pipes.translate(text, self.language, self.dst_lang)translated_text = ret.translate_contentlog_block("LLM translate time", f"{(time.perf_counter() - start_time):.3f}", "s")log_block("LLM translate out", f"{translated_text}")return translated_textdef analysis_segments(self, segments, audio_buffer: np.ndarray):# 找到第一个标点符号作为锚点 左边为确认段,右边为观察段,# 当左边确认后,右边段才会进入观察# 当左边确认后,会从缓冲区中删除对应的buffer,减少下次输入的数据量left_watch_idx, left_watch_sequences, right_watch_sequences, is_end_sentence = segments_split(segments, audio_buffer)left_watch_string = self.text_sep.join(i.text for i in left_watch_sequences)right_watch_string = self.text_sep.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# 整句消除 后两句之前的内容left_watch_idx, left_watch_sequences, right_watch_sequences, is_end_sentence = sequences_split(segments, audio_buffer)left_watch_string = self.text_sep.join(i.text for i in left_watch_sequences)right_watch_string = self.text_sep.join(i.text for i in right_watch_sequences)if left_watch_idx != 0:return left_watch_idx, left_watch_string, right_watch_string, is_end_sentencereturn None, left_watch_string, right_watch_string, is_end_sentencedef speech_to_text(self):c = 0while not self._translate_thread_stop.is_set():if self.exit:logger.info("Exiting speech to text thread")breakif self.frames_np is None :time.sleep(0.02) # wait for any audio to arrivelogger.info("waiting for client data...")continueaudio_buffer = self.get_audio_chunk_for_processing()if audio_buffer is None:time.sleep(0.02) # wait for any audio to arrivecontinue# c+= 1# name = f"dev-{c}.wav"# save_to_wave(name, audio_buffer)# try:logger.info(f"Audio buffer length: {len(audio_buffer) / self.sample_rate:.2f}s")segments = self.transcribe_audio(audio_buffer)for tran_result in self.handle_transcription_output(segments, audio_buffer):self.send_to_client(tran_result)# except KeyboardInterrupt:# break# except Exception as e:# logger.error(f"{e}")# if (time_delay := (1 - audio_duration)) > 0:# time.sleep(time_delay)def handle_transcription_output(self, segments, audio_buffer):texts = self.text_sep.join(i.text for i in segments)if not len(texts):returnself._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)# 句子或者短句的提交log_block("Whisper string lock ", f"{left_string}",)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.segmentseg_id = self._segment_manager.get_seg_id() - 1# logger.info(f"{seg_id}, {message}")yield TransResult(seg_id=seg_id,context=message,from_=self.language,to=self.dst_lang,tran_content=self.translate_text(message),partial=False)if self._segment_manager.string.strip():message = self._segment_manager.string.strip()# logger.info(f"{seg_id + 1}, {message}")yield TransResult(seg_id=seg_id+1,context=self._segment_manager.string,from_=self.language,to=self.dst_lang,tran_content=self.translate_text(message),)else:seg_id = self._segment_manager.get_seg_id()message = self._segment_manager.short_sentence + self._segment_manager.string# logger.info(f"{seg_id}, {message}")yield TransResult(seg_id=seg_id,context=message,from_=self.language,to=self.dst_lang,tran_content=self.translate_text(message),)def send_to_client(self, data:TransResult):try:coro = self.websocket.send_text(Message(result=data, request_id=self.client_uid).model_dump_json(by_alias=True))asyncio.run(coro)except RuntimeError as e:self.stop()returnexcept Exception as e:logger.error(e)def get_audio_chunk_for_processing(self):self.vad_merge()silence_audio = np.zeros((self.sample_rate+1000,), dtype=np.float32)frames = self.frames_np.copy()# 添加对非常短音频的处理if len(frames) <= 100:# 对于极短的音频段(<=100帧),直接返回空音频self.update_audio_buffer(len(frames))return Noneelif len(frames) < self.sample_rate:silence_audio[-len(frames):] = framesreturn silence_audio.copy()return frames.copy()def stop(self):self._translate_thread_stop.set()self._frame_to_queue_thread_stop.set()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309import asyncioimport jsonimport queueimport threadingimport timefrom logging import getLoggerfrom typing import List, Optional, Iterator, Tuple, Anyimport numpy as npfrom api_model import TransResult, Messagefrom .server import ServeClientBasefrom .utils import log_block, save_to_wavefrom .translatepipes import TranslatePipesfrom .strategy import TextStabilityBuffer, TranscriptionManager, TranscriptionSplitter, TranscriptSegmentlogger = getLogger("TranscriptionService")class WhisperTranscriptionService(ServeClientBase):"""Whisper语音转录服务类,处理音频流转录和翻译"""def __init__(self, websocket, pipe: TranslatePipes, language=None, dst_lang=None, client_uid=None):super().__init__(client_uid, websocket)self.source_language = language # 源语言self.target_language = dst_lang # 目标翻译语言# 转录结果稳定性管理self._text_stability_buffer = TextStabilityBuffer()self._transcription_manager = TranscriptionManager()self._translate_pipe = pipe# 音频处理相关self.sample_rate = 16000self.frames_np = Noneself.lock = threading.Lock()self._frame_queue = queue.Queue()# 文本分隔符,根据语言设置self.text_separator = self._get_text_separator(language)# 发送就绪状态self.send_ready_state()# 启动处理线程self._translate_thread_stop = threading.Event()self._frame_processing_thread_stop = threading.Event()self.translate_thread = self._start_thread(self._transcription_processing_loop)self.frame_processing_thread = self._start_thread(self._frame_processing_loop)def _start_thread(self, target_function) -> threading.Thread:"""启动守护线程执行指定函数"""thread = threading.Thread(target=target_function)thread.daemon = Truethread.start()return threaddef _get_text_separator(self, language: str) -> str:"""根据语言返回适当的文本分隔符"""return "" if language == "zh" else " "def send_ready_state(self) -> None:"""发送服务就绪状态消息"""self.websocket.send(json.dumps({"uid": self.client_uid,"message": self.SERVER_READY,"backend": "whisper_transcription"}))def set_language(self, source_lang: str, target_lang: str) -> None:"""设置源语言和目标语言"""self.source_language = source_langself.target_language = target_langself.text_separator = self._get_text_separator(source_lang)def add_audio_frames(self, frame_np: np.ndarray) -> None:"""添加音频帧到处理队列"""self._frame_queue.put(frame_np)def _frame_processing_loop(self) -> None:"""从队列获取音频帧并合并到缓冲区"""while not self._frame_processing_thread_stop.is_set():try:frame_np = self._frame_queue.get(timeout=0.1)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)except queue.Empty:passdef _apply_voice_activity_detection(self) -> None:"""应用语音活动检测来优化音频缓冲区"""with self.lock:if self.frames_np is not None:frame = self.frames_np.copy()processed_audio = self._translate_pipe.voice_detect(frame.tobytes())self.frames_np = np.frombuffer(processed_audio.audio, dtype=np.float32).copy()def _update_audio_buffer(self, offset: int) -> None:"""从音频缓冲区中移除已处理的部分"""with self.lock:if self.frames_np is not None and offset > 0:self.frames_np = self.frames_np[offset:]def _get_audio_for_processing(self) -> Optional[np.ndarray]:"""准备用于处理的音频块"""# 应用VAD处理self._apply_voice_activity_detection()# 没有音频帧if self.frames_np is None:return Noneframes = self.frames_np.copy()# 音频过短时的处理if len(frames) <= 100:# 极短音频段,清空并返回Noneself._update_audio_buffer(len(frames))return Noneelif len(frames) < self.sample_rate:# 不足一秒的音频,补充静音silence_audio = np.zeros((self.sample_rate + 1000,), dtype=np.float32)silence_audio[-len(frames):] = framesreturn silence_audio.copy()return frames.copy()def _transcribe_audio(self, audio_buffer: np.ndarray) -> List[TranscriptSegment]:"""转录音频并返回转录片段"""log_block("Audio buffer length", f"{audio_buffer.shape[0]/self.sample_rate:.2f}", "s")start_time = time.perf_counter()result = self._translate_pipe.transcrible(audio_buffer.tobytes(), self.source_language)segments = result.segmentslog_block("Whisper transcription output", f"{''.join(seg.text for seg in segments)}", "")log_block("Whisper transcription time", f"{(time.perf_counter() - start_time):.3f}", "s")return segmentsdef _translate_text(self, text: str) -> str:"""将文本翻译为目标语言"""if not text.strip():return ""log_block("Translation input", f"{text}")start_time = time.perf_counter()result = self._translate_pipe.translate(text, self.source_language, self.target_language)translated_text = result.translate_contentlog_block("Translation time", f"{(time.perf_counter() - start_time):.3f}", "s")log_block("Translation output", f"{translated_text}")return translated_textdef _analyze_segments(self, segments: List[TranscriptSegment], audio_buffer: np.ndarray) -> Tuple[Optional[int], str, str, bool]:"""分析转录片段,确定稳定部分和需要继续观察的部分Returns:(分割索引, 左侧稳定文本, 右侧观察文本, 是否为句子结束)"""# 尝试基于标点符号进行分割left_idx, left_segments, right_segments, is_end = TranscriptionSplitter.split_by_punctuation(segments, audio_buffer, self.sample_rate)left_text = self.text_separator.join(seg.text for seg in left_segments)right_text = self.text_separator.join(seg.text for seg in right_segments)# 如果找到分割点,检查左侧文本稳定性if left_idx != 0:self._text_stability_buffer.add_entry(left_text, left_idx)stable_idx = self._text_stability_buffer.get_stable_index()if stable_idx:return stable_idx, left_text, right_text, is_end# 如果基于标点的方法未找到稳定点,尝试基于句子序列的方法left_idx, left_segments, right_segments, is_end = TranscriptionSplitter.split_by_sequences(segments, audio_buffer, self.sample_rate)if left_idx != 0:left_text = self.text_separator.join(seg.text for seg in left_segments)right_text = self.text_separator.join(seg.text for seg in right_segments)return left_idx, left_text, right_text, is_end# 如果都没有找到分割点return None, left_text, right_text, is_enddef _transcription_processing_loop(self) -> None:"""主转录处理循环"""while not self._translate_thread_stop.is_set():if self.exit:logger.info("Exiting transcription thread")break# 等待音频数据if self.frames_np is None:time.sleep(0.02)logger.info("Waiting for audio data...")continue# 获取音频块进行处理audio_buffer = self._get_audio_for_processing()if audio_buffer is None:time.sleep(0.02)continuetry:logger.info(f"Processing audio buffer: {len(audio_buffer)/self.sample_rate:.2f}s")segments = self._transcribe_audio(audio_buffer)# 处理转录结果并发送到客户端for result in self._process_transcription_results(segments, audio_buffer):self._send_result_to_client(result)except Exception as e:logger.error(f"Error processing audio: {e}")def _process_transcription_results(self, segments: List[TranscriptSegment], audio_buffer: np.ndarray) -> Iterator[TransResult]:"""处理转录结果,生成翻译结果Returns:TransResult对象的迭代器"""# 合并所有片段的文本full_text = self.text_separator.join(seg.text for seg in segments)if not full_text:return# 更新转录管理器中的临时文本self._transcription_manager.update_temp(full_text)# 分析片段,确定稳定部分和需要继续观察的部分cut_index, stable_text, remaining_text, is_sentence_end = self._analyze_segments(segments, audio_buffer)# 如果找到稳定的分割点if cut_index:# 更新音频缓冲区,移除已处理部分self._update_audio_buffer(cut_index)# 提交稳定的文本log_block("Stable transcription", f"{stable_text}")self._transcription_manager.update_temp(stable_text).commit_segment(is_sentence_end)self._transcription_manager.update_temp(remaining_text)# 如果是句子结束,发送完整句子的翻译结果if is_sentence_end:segment_text = self._transcription_manager.latest_segmentsegment_id = self._transcription_manager.segment_count - 1# 生成已确认句子的翻译结果yield TransResult(seg_id=segment_id,context=segment_text,from_=self.source_language,to=self.target_language,tran_content=self._translate_text(segment_text),partial=False)# 如果还有剩余部分,生成临时翻译结果if self._transcription_manager.current_sentence.strip():yield TransResult(seg_id=segment_id + 1,context=self._transcription_manager.current_sentence,from_=self.source_language,to=self.target_language,tran_content=self._translate_text(self._transcription_manager.current_sentence.strip()),partial=True)else:# 没有找到稳定点,发送当前所有内容的临时翻译结果segment_id = self._transcription_manager.segment_countcurrent_text = self._transcription_manager.current_sentence + self._transcription_manager.update_temp(remaining_text)._temp_stringyield TransResult(seg_id=segment_id,context=current_text,from_=self.source_language,to=self.target_language,tran_content=self._translate_text(current_text),partial=True)def _send_result_to_client(self, result: TransResult) -> None:"""发送翻译结果到客户端"""try:message = Message(result=result, request_id=self.client_uid).model_dump_json(by_alias=True)coro = self.websocket.send_text(message)asyncio.run(coro)except RuntimeError:self.stop()except Exception as e:logger.error(f"Error sending result to client: {e}")def stop(self) -> None:"""停止所有处理线程并清理资源"""self._translate_thread_stop.set()self._frame_processing_thread_stop.set()logger.info(f"Stopping transcription service for client: {self.client_uid}")
config.py+4
main.py+2-2
transcribe/strategy.py+246-119
transcribe/whisper_llm_serve.py+243-195