127 lines
3.8 KiB
Python
127 lines
3.8 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
模拟前端发送 ASR 请求
|
||
|
|
"""
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
sys.path.append('.')
|
||
|
|
|
||
|
|
import requests
|
||
|
|
import base64
|
||
|
|
import wave
|
||
|
|
import struct
|
||
|
|
import math
|
||
|
|
import logging
|
||
|
|
|
||
|
|
# 设置日志
|
||
|
|
logging.basicConfig(level=logging.INFO)
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
def create_test_mp3_like_data():
|
||
|
|
"""创建模拟 MP3 数据(实际上是简单的音频数据)"""
|
||
|
|
# 创建一些音频数据来模拟前端录音
|
||
|
|
sample_rate = 16000
|
||
|
|
duration = 2 # 2 秒
|
||
|
|
frequency = 300 # 低频音,模拟人声
|
||
|
|
|
||
|
|
# 生成音频样本
|
||
|
|
samples = []
|
||
|
|
for i in range(sample_rate * duration):
|
||
|
|
t = i / sample_rate
|
||
|
|
# 生成复合波形,模拟语音
|
||
|
|
sample1 = math.sin(2 * math.pi * frequency * t)
|
||
|
|
sample2 = 0.5 * math.sin(2 * math.pi * frequency * 2 * t)
|
||
|
|
sample3 = 0.3 * math.sin(2 * math.pi * frequency * 3 * t)
|
||
|
|
|
||
|
|
# 添加包络,模拟语音的动态变化
|
||
|
|
envelope = math.exp(-t * 0.5) * (1 + 0.5 * math.sin(2 * math.pi * 2 * t))
|
||
|
|
|
||
|
|
combined = (sample1 + sample2 + sample3) * envelope
|
||
|
|
sample_int = int(16000 * combined)
|
||
|
|
sample_int = max(-32767, min(32767, sample_int))
|
||
|
|
samples.append(sample_int)
|
||
|
|
|
||
|
|
# 转换为字节数据(模拟 MP3 编码后的数据)
|
||
|
|
audio_bytes = bytearray()
|
||
|
|
for sample in samples:
|
||
|
|
audio_bytes.extend(struct.pack('<h', sample))
|
||
|
|
|
||
|
|
logger.info(f"创建模拟音频数据,大小: {len(audio_bytes)} 字节")
|
||
|
|
return bytes(audio_bytes)
|
||
|
|
|
||
|
|
def test_health_endpoint():
|
||
|
|
"""测试健康检查端点"""
|
||
|
|
url = "http://127.0.0.1:30101/health"
|
||
|
|
|
||
|
|
try:
|
||
|
|
response = requests.get(url, timeout=10)
|
||
|
|
logger.info(f"健康检查响应状态码: {response.status_code}")
|
||
|
|
logger.info(f"健康检查响应内容: {response.text}")
|
||
|
|
return response.status_code == 200
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"健康检查失败: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
def test_frontend_asr_request():
|
||
|
|
"""测试前端 ASR 请求"""
|
||
|
|
|
||
|
|
# 创建测试音频数据
|
||
|
|
audio_data = create_test_mp3_like_data()
|
||
|
|
|
||
|
|
# 转换为 base64
|
||
|
|
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
||
|
|
logger.info(f"Base64 编码长度: {len(audio_base64)}")
|
||
|
|
|
||
|
|
# 准备请求数据
|
||
|
|
request_data = {
|
||
|
|
'audio_data': audio_base64,
|
||
|
|
'format': 'mp3'
|
||
|
|
}
|
||
|
|
|
||
|
|
# 发送请求到后端
|
||
|
|
url = "http://127.0.0.1:30101/voice/call/asr"
|
||
|
|
headers = {
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Authorization': 'Bearer test_token' # 使用测试 token
|
||
|
|
}
|
||
|
|
|
||
|
|
logger.info(f"发送 ASR 请求到: {url}")
|
||
|
|
|
||
|
|
try:
|
||
|
|
response = requests.post(url, json=request_data, headers=headers, timeout=60)
|
||
|
|
|
||
|
|
logger.info(f"响应状态码: {response.status_code}")
|
||
|
|
logger.info(f"响应内容: {response.text}")
|
||
|
|
|
||
|
|
if response.status_code == 200:
|
||
|
|
result = response.json()
|
||
|
|
logger.info(f"✅ ASR 请求成功")
|
||
|
|
logger.info(f"识别结果: {result}")
|
||
|
|
return True
|
||
|
|
else:
|
||
|
|
logger.error(f"❌ ASR 请求失败: {response.status_code}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
except requests.exceptions.Timeout:
|
||
|
|
logger.error("❌ 请求超时")
|
||
|
|
return False
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"❌ 请求异常: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
logger.info("开始测试后端连接...")
|
||
|
|
|
||
|
|
# 先测试健康检查
|
||
|
|
if not test_health_endpoint():
|
||
|
|
logger.error("💥 后端健康检查失败")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
logger.info("开始测试前端 ASR 请求...")
|
||
|
|
success = test_frontend_asr_request()
|
||
|
|
|
||
|
|
if success:
|
||
|
|
logger.info("🎉 前端 ASR 请求测试成功")
|
||
|
|
else:
|
||
|
|
logger.error("💥 前端 ASR 请求测试失败")
|
||
|
|
sys.exit(1)
|