67 lines
1.8 KiB
Python
67 lines
1.8 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
测试语音识别服务
|
||
|
|
"""
|
||
|
|
|
||
|
|
import requests
|
||
|
|
|
||
|
|
# 测试健康检查
|
||
|
|
def test_health():
|
||
|
|
print("测试健康检查...")
|
||
|
|
try:
|
||
|
|
response = requests.get('http://localhost:5000/api/speech/health')
|
||
|
|
print(f"状态码: {response.status_code}")
|
||
|
|
print(f"响应: {response.json()}")
|
||
|
|
return response.status_code == 200
|
||
|
|
except Exception as e:
|
||
|
|
print(f"错误: {str(e)}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
# 测试语音识别(需要有音频文件)
|
||
|
|
def test_recognize(audio_file):
|
||
|
|
print(f"\n测试语音识别: {audio_file}")
|
||
|
|
try:
|
||
|
|
with open(audio_file, 'rb') as f:
|
||
|
|
files = {'audio': f}
|
||
|
|
data = {'referenceText': '你好世界'}
|
||
|
|
|
||
|
|
response = requests.post(
|
||
|
|
'http://localhost:5000/api/speech/recognize',
|
||
|
|
files=files,
|
||
|
|
data=data
|
||
|
|
)
|
||
|
|
|
||
|
|
print(f"状态码: {response.status_code}")
|
||
|
|
print(f"响应: {response.json()}")
|
||
|
|
return response.status_code == 200
|
||
|
|
except Exception as e:
|
||
|
|
print(f"错误: {str(e)}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
print("=" * 50)
|
||
|
|
print("语音服务测试")
|
||
|
|
print("=" * 50)
|
||
|
|
print()
|
||
|
|
|
||
|
|
# 1. 测试健康检查
|
||
|
|
if test_health():
|
||
|
|
print("✓ 健康检查通过")
|
||
|
|
else:
|
||
|
|
print("✗ 健康检查失败")
|
||
|
|
exit(1)
|
||
|
|
|
||
|
|
# 2. 测试语音识别(如果有测试音频)
|
||
|
|
import os
|
||
|
|
test_audio = './test.wav'
|
||
|
|
if os.path.exists(test_audio):
|
||
|
|
if test_recognize(test_audio):
|
||
|
|
print("✓ 语音识别测试通过")
|
||
|
|
else:
|
||
|
|
print("✗ 语音识别测试失败")
|
||
|
|
else:
|
||
|
|
print(f"\n提示: 创建 {test_audio} 文件可测试语音识别")
|
||
|
|
|
||
|
|
print("\n所有测试完成!")
|