鱼C论坛

 找回密码
 立即注册
查看: 258|回复: 1

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

[复制链接]
发表于 2024-12-12 10:07:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import pyaudio
  2. import dashscope
  3. from dashscope.audio.tts_v2 import *


  4. from http import HTTPStatus
  5. from dashscope import Generation

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


  9. class Callback(ResultCallback):
  10.     _player = None
  11.     _stream = None

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

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

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

  22.     def on_close(self):
  23.         print("websocket is closed.")
  24.         # stop player
  25.         self._stream.stop_stream()
  26.         self._stream.close()
  27.         self._player.terminate()

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

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


  33. def synthesizer_with_llm(text):
  34.     callback = Callback()
  35.     synthesizer = SpeechSynthesizer(
  36.         model=model,
  37.         voice=voice,
  38.         format=AudioFormat.PCM_22050HZ_MONO_16BIT,
  39.         callback=callback,
  40.     )

  41.     messages = [{"role": "user", "content": f"""请朗读这些句子,跟原文保持绝对一致,不要添加任何评论,翻译。:{text}""" }]
  42.                  
  43.                  
  44.     responses = Generation.call(
  45.         model="qwen-turbo",
  46.         messages=messages,
  47.         result_format="message",  # set result format as 'message'
  48.         stream=True,  # enable stream output
  49.         incremental_output=True,  # enable incremental output
  50.     )
  51.     for response in responses:
  52.         if response.status_code == HTTPStatus.OK:
  53.             print(response.output.choices[0]["message"]["content"], end="")
  54.             synthesizer.streaming_call(response.output.choices[0]["message"]["content"])
  55.         else:
  56.             print(
  57.                 "Request id: %s, Status code: %s, error code: %s, error message: %s"
  58.                 % (
  59.                     response.request_id,
  60.                     response.status_code,
  61.                     response.code,
  62.                     response.message,
  63.                 )
  64.             )
  65.     synthesizer.streaming_complete()
  66.     # print('requestId: ', synthesizer.get_last_request_id())


  67. if __name__ == "__main__":
  68.     text1 = """

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

  71.                  
  72.             """
  73.     synthesizer_with_llm(text1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-12-12 10:07:38 | 显示全部楼层
要去除冗余的屏幕输出,可以通过将不必要的 `print` 语句注释掉或删除来实现。以下是去掉一些 `print` 语句的简化代码:

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-7 16:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表