test_kuaishou_kling.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """测试快手可灵AI工具
  2. 用法:
  3. uv run python tests/test_kuaishou_kling.py # Health check
  4. uv run python tests/test_kuaishou_kling.py --text2image # 测试文生图
  5. uv run python tests/test_kuaishou_kling.py --text2video # 测试文生视频
  6. uv run python tests/test_kuaishou_kling.py --lip-sync # 测试对口型
  7. """
  8. import argparse
  9. import json
  10. import sys
  11. import httpx
  12. BASE_URL = "http://127.0.0.1:8001"
  13. def check_connection():
  14. try:
  15. httpx.get(f"{BASE_URL}/health", timeout=3)
  16. except httpx.ConnectError:
  17. print(f"ERROR: Cannot connect to {BASE_URL}")
  18. print("Please start the service first:")
  19. print(" uv run python -m tool_agent")
  20. sys.exit(1)
  21. def test_health():
  22. print("=== Health Check ===")
  23. resp = httpx.get(f"{BASE_URL}/health")
  24. print(f" Status : {resp.status_code}")
  25. print(f" Body : {json.dumps(resp.json(), ensure_ascii=False, indent=4)}")
  26. assert resp.status_code == 200
  27. print(" [PASS]")
  28. def test_text2image():
  29. print("=== Test Text to Image ===")
  30. print(" Calling kuaishou_kling...")
  31. try:
  32. resp = httpx.post(f"{BASE_URL}/select_tool", json={
  33. "tool_id": "kuaishou_kling",
  34. "params": {
  35. "biz_type": "aiImage",
  36. "prompt": "cute cat playing in garden",
  37. "aspect_ratio": "16:9",
  38. "image_count": 2
  39. }
  40. }, timeout=120)
  41. print(f" Status : {resp.status_code}")
  42. data = resp.json()
  43. if data["status"] == "success":
  44. result = data["result"]
  45. print(f" task_id: {result.get('task_id')}")
  46. print(" [PASS]")
  47. else:
  48. print(f" ERROR : {data.get('error')}")
  49. print(" [FAIL]")
  50. except Exception as e:
  51. print(f" ERROR : {e}")
  52. print(" [FAIL]")
  53. def test_text2video():
  54. print("=== Test Text to Video ===")
  55. print(" Calling kuaishou_kling...")
  56. try:
  57. resp = httpx.post(f"{BASE_URL}/select_tool", json={
  58. "tool_id": "kuaishou_kling",
  59. "params": {
  60. "biz_type": "aiVideo",
  61. "prompt": "ocean waves at sunset",
  62. "aspect_ratio": "16:9"
  63. }
  64. }, timeout=300)
  65. print(f" Status : {resp.status_code}")
  66. data = resp.json()
  67. if data["status"] == "success":
  68. print(f" task_id: {data['result'].get('task_id')}")
  69. print(" [PASS]")
  70. else:
  71. print(f" ERROR : {data.get('error')}")
  72. print(" [FAIL]")
  73. except Exception as e:
  74. print(f" ERROR : {e}")
  75. print(" [FAIL]")
  76. def test_lip_sync():
  77. print("=== Test AI Lip Sync ===")
  78. print(" Calling kuaishou_kling...")
  79. try:
  80. resp = httpx.post(f"{BASE_URL}/select_tool", json={
  81. "tool_id": "kuaishou_kling",
  82. "params": {
  83. "biz_type": "aiLipSync",
  84. "mode": "text2video",
  85. "text": "Hello world"
  86. }
  87. }, timeout=300)
  88. print(f" Status : {resp.status_code}")
  89. data = resp.json()
  90. if data["status"] == "success":
  91. print(" [PASS]")
  92. else:
  93. print(f" ERROR : {data.get('error')}")
  94. print(" [FAIL]")
  95. except Exception as e:
  96. print(f" ERROR : {e}")
  97. print(" [FAIL]")
  98. def main():
  99. parser = argparse.ArgumentParser(description="Kuaishou Kling AI Tool Test")
  100. parser.add_argument("--text2image", action="store_true")
  101. parser.add_argument("--text2video", action="store_true")
  102. parser.add_argument("--lip-sync", action="store_true")
  103. args = parser.parse_args()
  104. print(f"Target: {BASE_URL}\n")
  105. check_connection()
  106. test_health()
  107. ran_any = False
  108. if args.text2image:
  109. print()
  110. test_text2image()
  111. ran_any = True
  112. if args.text2video:
  113. print()
  114. test_text2video()
  115. ran_any = True
  116. if args.lip_sync:
  117. print()
  118. test_lip_sync()
  119. ran_any = True
  120. if not ran_any:
  121. print("\nNo test specified. Available options:")
  122. parser.print_help()
  123. print("\n=== DONE ===")
  124. if __name__ == "__main__":
  125. main()