cosyvoice_debug.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """CosyVoice 测试 - 排查问题"""
  4. import requests, json, time, os
  5. API_KEY = "sk-c25679401ba24c749f53be86b0c9a7a6"
  6. API_URL = "https://dashscope.aliyuncs.com/api/v1/services/audio/tts/SpeechSynthesizer"
  7. OUTPUT_DIR = "test-results"
  8. os.makedirs(OUTPUT_DIR, exist_ok=True)
  9. def test(text, model, voice, label):
  10. payload = {
  11. "model": model,
  12. "input": {"text": text, "voice": voice, "format": "mp3", "sample_rate": 24000}
  13. }
  14. headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
  15. t0 = time.time()
  16. resp = requests.post(API_URL, headers=headers, json=payload, timeout=60)
  17. elapsed = time.time() - t0
  18. data = resp.json()
  19. status = "✅" if resp.status_code == 200 else "❌"
  20. code = data.get("code", "OK") if resp.status_code != 200 else "OK"
  21. msg = data.get("message", "") if resp.status_code != 200 else "success"
  22. print(f"{status} [{label}] HTTP {resp.status_code} | {code} | {msg[:80]} | {elapsed:.1f}s")
  23. if resp.status_code == 200:
  24. audio_url = data.get("output", {}).get("audio", {}).get("url", "")
  25. chars = data.get("usage", {}).get("characters", 0)
  26. print(f" 字符数: {chars}, 音频URL: {audio_url[:80]}...")
  27. # 下载
  28. ar = requests.get(audio_url, timeout=30)
  29. if ar.status_code == 200:
  30. fname = os.path.join(OUTPUT_DIR, f"test-{label}-{int(time.time())}.mp3")
  31. with open(fname, "wb") as f: f.write(ar.content)
  32. print(f" 已保存: {fname} ({len(ar.content)} bytes)")
  33. return resp.status_code == 200
  34. print("=" * 50)
  35. print(" CosyVoice 故障排查")
  36. print("=" * 50)
  37. # 尝试不同模型+音色组合
  38. short_text = "你好,这是一段测试文本。欢迎使用CosyVoice语音合成服务。"
  39. tests = [
  40. # model, voice, label
  41. ("cosyvoice-v3.5-flash", "longanyang", "v3.5-flash+longanyang"),
  42. ("cosyvoice-v3.5-plus", "longanyang", "v3.5-plus+longanyang"),
  43. ("cosyvoice-v3-flash", "longanyang", "v3-flash+longanyang"),
  44. ("cosyvoice-v3.5-flash", "longxiaoxia", "v3.5-flash+longxiaoxia"),
  45. ("cosyvoice-v1", "longanyang", "v1+longanyang"),
  46. ("cosyvoice-v3.5-flash", "longyu", "v3.5-flash+longyu"),
  47. ("cosyvoice-v3-flash", "longyu", "v3-flash+longyu"),
  48. ]
  49. for model, voice, label in tests:
  50. if test(short_text, model, voice, label):
  51. print(f" >>> 成功组合: model={model}, voice={voice}\n")
  52. break