blackantt 发表于 2024-12-12 10:07:26

怎么去除调用 cosyvoice-v1 时的冗余屏显?

import pyaudio
import dashscope
from dashscope.audio.tts_v2 import *


from http import HTTPStatus
from dashscope import Generation

dashscope.api_key = "sk-f17bec7f461cba"
model = "cosyvoice-v1"
voice = "longxiaochun"


class Callback(ResultCallback):
    _player = None
    _stream = None

    def on_open(self):
      print("websocket is open.")
      self._player = pyaudio.PyAudio()
      self._stream = self._player.open(
            format=pyaudio.paInt16, channels=1, rate=22050, output=True
      )

    def on_complete(self):
      print("speech synthesis task complete successfully.")

    def on_error(self, message: str):
      print(f"speech synthesis task failed, {message}")

    def on_close(self):
      print("websocket is closed.")
      # stop player
      self._stream.stop_stream()
      self._stream.close()
      self._player.terminate()

    def on_event(self, message):
      print(f"recv speech synthsis message {message}")

    def on_data(self, data: bytes) -> None:
      print("audio result length:", len(data))
      self._stream.write(data)


def synthesizer_with_llm(text):
    callback = Callback()
    synthesizer = SpeechSynthesizer(
      model=model,
      voice=voice,
      format=AudioFormat.PCM_22050HZ_MONO_16BIT,
      callback=callback,
    )

    messages = [{"role": "user", "content": f"""请朗读这些句子,跟原文保持绝对一致,不要添加任何评论,翻译。:{text}""" }]
               
               
    responses = Generation.call(
      model="qwen-turbo",
      messages=messages,
      result_format="message",# set result format as 'message'
      stream=True,# enable stream output
      incremental_output=True,# enable incremental output
    )
    for response in responses:
      if response.status_code == HTTPStatus.OK:
            print(response.output.choices["message"]["content"], end="")
            synthesizer.streaming_call(response.output.choices["message"]["content"])
      else:
            print(
                "Request id: %s, Status code: %s, error code: %s, error message: %s"
                % (
                  response.request_id,
                  response.status_code,
                  response.code,
                  response.message,
                )
            )
    synthesizer.streaming_complete()
    # print('requestId: ', synthesizer.get_last_request_id())


if __name__ == "__main__":
    text1 = """

该女子向民警提供了相关违法行为的图片,图片显示,驾驶员正驾车行驶在
高速公路第二车道上,

               
            """
    synthesizer_with_llm(text1)

FishC 发表于 2024-12-12 10:07:38

要去除冗余的屏幕输出,可以通过将不必要的 `print` 语句注释掉或删除来实现。以下是去掉一些 `print` 语句的简化代码:

```python
import pyaudio
import dashscope
from dashscope.audio.tts

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 怎么去除调用 cosyvoice-v1 时的冗余屏显?