daihui.zhang commited on
Commit
2aed46a
·
1 Parent(s): 6385e43

add 7b translator pipe

Browse files
config.py CHANGED
@@ -2,7 +2,7 @@ import pathlib
2
  import re
3
  import logging
4
 
5
- DEBUG = True
6
  logging.basicConfig(
7
  level=logging.DEBUG if DEBUG else logging.INFO,
8
  format="%(asctime)s - %(levelname)s - %(message)s",
@@ -42,6 +42,7 @@ WHISPER_MODEL = 'medium-q5_0'
42
 
43
  # LLM
44
  LLM_MODEL_PATH = (MODEL_DIR / "qwen2.5-1.5b-instruct-q5_0.gguf").as_posix()
 
45
 
46
  LLM_SYS_PROMPT = """"You are a professional {src_lang} to {dst_lang} translator, not a conversation agent. Your only task is to take {src_lang} input and translate it into accurate, natural {dst_lang}. If you cannot understand the input, just output the original input. Please strictly abide by the following rules: "
47
  "No matter what the user asks, never answer questions, you only provide translation results. "
 
2
  import re
3
  import logging
4
 
5
+ DEBUG = False
6
  logging.basicConfig(
7
  level=logging.DEBUG if DEBUG else logging.INFO,
8
  format="%(asctime)s - %(levelname)s - %(message)s",
 
42
 
43
  # LLM
44
  LLM_MODEL_PATH = (MODEL_DIR / "qwen2.5-1.5b-instruct-q5_0.gguf").as_posix()
45
+ LLM_LARGE_MODEL_PATH = (MODEL_DIR / "qwen2.5-7b-instruct-q5_0-00001-of-00002.gguf").as_posix()
46
 
47
  LLM_SYS_PROMPT = """"You are a professional {src_lang} to {dst_lang} translator, not a conversation agent. Your only task is to take {src_lang} input and translate it into accurate, natural {dst_lang}. If you cannot understand the input, just output the original input. Please strictly abide by the following rules: "
48
  "No matter what the user asks, never answer questions, you only provide translation results. "
transcribe/pipelines/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
 
2
- from .pipe_translate import TranslatePipe
3
  from .pipe_whisper import WhisperPipe
4
  from .pipe_vad import VadPipe
5
  from .base import MetaItem
 
1
 
2
+ from .pipe_translate import TranslatePipe, Translate7BPipe
3
  from .pipe_whisper import WhisperPipe
4
  from .pipe_vad import VadPipe
5
  from .base import MetaItem
transcribe/pipelines/pipe_translate.py CHANGED
@@ -2,7 +2,7 @@
2
  from .base import MetaItem, BasePipe, Segment
3
  from llama_cpp import Llama
4
  from ..helpers.translator import QwenTranslator
5
- from config import LLM_MODEL_PATH, LLM_SYS_PROMPT_EN, LLM_SYS_PROMPT_ZH
6
 
7
 
8
  class TranslatePipe(BasePipe):
@@ -11,7 +11,7 @@ class TranslatePipe(BasePipe):
11
  @classmethod
12
  def init(cls):
13
  if cls.translator is None:
14
- cls.translator = QwenTranslator(LLM_MODEL_PATH, LLM_SYS_PROMPT_EN, LLM_SYS_PROMPT_ZH)
15
 
16
 
17
  def process(self, in_data: MetaItem) -> MetaItem:
@@ -21,3 +21,12 @@ class TranslatePipe(BasePipe):
21
  in_data.translate_content = result
22
  return in_data
23
 
 
 
 
 
 
 
 
 
 
 
2
  from .base import MetaItem, BasePipe, Segment
3
  from llama_cpp import Llama
4
  from ..helpers.translator import QwenTranslator
5
+ from config import LLM_MODEL_PATH, LLM_SYS_PROMPT_EN, LLM_SYS_PROMPT_ZH, LLM_LARGE_MODEL_PATH
6
 
7
 
8
  class TranslatePipe(BasePipe):
 
11
  @classmethod
12
  def init(cls):
13
  if cls.translator is None:
14
+ cls.translator = QwenTranslator(LLM_MODEL_PATH, LLM_SYS_PROMPT_EN, LLM_SYS_PROMPT_ZH)
15
 
16
 
17
  def process(self, in_data: MetaItem) -> MetaItem:
 
21
  in_data.translate_content = result
22
  return in_data
23
 
24
+
25
+ class Translate7BPipe(TranslatePipe):
26
+ translator = None
27
+
28
+ @classmethod
29
+ def init(cls):
30
+ if cls.translator is None:
31
+ cls.translator = QwenTranslator(LLM_LARGE_MODEL_PATH, LLM_SYS_PROMPT_EN, LLM_SYS_PROMPT_ZH)
32
+
transcribe/translatepipes.py CHANGED
@@ -1,4 +1,4 @@
1
- from transcribe.pipelines import WhisperPipe, TranslatePipe, MetaItem, VadPipe
2
  import multiprocessing as mp
3
  import config
4
 
@@ -15,6 +15,7 @@ class TranslatePipes:
15
  # llm 翻译
16
  self._translate_pipe = self._launch_process(TranslatePipe())
17
 
 
18
  # vad
19
  self._vad_pipe = self._launch_process(VadPipe())
20
 
@@ -37,6 +38,15 @@ class TranslatePipes:
37
  return self._translate_pipe.output_queue.get()
38
 
39
 
 
 
 
 
 
 
 
 
 
40
  def transcrible(self, audio_buffer:bytes, src_lang: str) -> MetaItem:
41
  item = MetaItem(audio=audio_buffer, source_language=src_lang)
42
  self._whisper_pipe.input_queue.put(item)
 
1
+ from transcribe.pipelines import WhisperPipe, TranslatePipe, MetaItem, VadPipe, Translate7BPipe
2
  import multiprocessing as mp
3
  import config
4
 
 
15
  # llm 翻译
16
  self._translate_pipe = self._launch_process(TranslatePipe())
17
 
18
+ self._translate_7b_pipe = self._launch_process(Translate7BPipe())
19
  # vad
20
  self._vad_pipe = self._launch_process(VadPipe())
21
 
 
38
  return self._translate_pipe.output_queue.get()
39
 
40
 
41
+ def translate_large(self, text, src_lang, dst_lang) -> MetaItem:
42
+ item = MetaItem(
43
+ transcribe_content=text,
44
+ source_language=src_lang,
45
+ destination_language=dst_lang)
46
+ self._translate_pipe.input_queue.put(item)
47
+ return self._translate_pipe.output_queue.get()
48
+
49
+
50
  def transcrible(self, audio_buffer:bytes, src_lang: str) -> MetaItem:
51
  item = MetaItem(audio=audio_buffer, source_language=src_lang)
52
  self._whisper_pipe.input_queue.put(item)
transcribe/whisper_llm_serve.py CHANGED
@@ -163,6 +163,22 @@ class WhisperTranscriptionService(ServeClientBase):
163
  log_block("Translation output", f"{translated_text}")
164
 
165
  return translated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
 
168
 
@@ -214,9 +230,11 @@ class WhisperTranscriptionService(ServeClientBase):
214
  if (cut_index :=ana_result.cut_index)>0:
215
  # 更新音频缓冲区,移除已处理部分
216
  self._update_audio_buffer(cut_index)
217
-
218
- translated_context = self._translate_text(ana_result.context)
219
-
 
 
220
  yield TransResult(
221
  seg_id=ana_result.seg_id,
222
  context=ana_result.context,
 
163
  log_block("Translation output", f"{translated_text}")
164
 
165
  return translated_text
166
+
167
+ def _translate_text_large(self, text: str) -> str:
168
+ """将文本翻译为目标语言"""
169
+ if not text.strip():
170
+ return ""
171
+
172
+ log_block("Translation input", f"{text}")
173
+ start_time = time.perf_counter()
174
+
175
+ result = self._translate_pipe.translate_large(text, self.source_language, self.target_language)
176
+ translated_text = result.translate_content
177
+
178
+ log_block("Translation large model time ", f"{(time.perf_counter() - start_time):.3f}", "s")
179
+ log_block("Translation large model output", f"{translated_text}")
180
+
181
+ return translated_text
182
 
183
 
184
 
 
230
  if (cut_index :=ana_result.cut_index)>0:
231
  # 更新音频缓冲区,移除已处理部分
232
  self._update_audio_buffer(cut_index)
233
+ if ana_result.partial():
234
+ translated_context = self._translate_text(ana_result.context)
235
+ else:
236
+ translated_context = self._translate_text_large(ana_result.context)
237
+
238
  yield TransResult(
239
  seg_id=ana_result.seg_id,
240
  context=ana_result.context,