Ai_GirlFriend/test_asr_endpoint.py
2026-03-03 19:06:01 +08:00

89 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
测试修复后的 ASR 端点
"""
import requests
import os
from dotenv import load_dotenv
# 加载环境变量
load_dotenv()
def test_asr_endpoint():
"""测试 ASR 端点"""
try:
# 创建一个测试音频文件
import wave
import numpy as np
# 生成3秒的测试音频16kHz, 单声道)
sample_rate = 16000
duration = 3
frequency = 440 # A4音符
t = np.linspace(0, duration, sample_rate * duration, False)
audio_data = np.sin(2 * np.pi * frequency * t) * 0.3
audio_data = (audio_data * 32767).astype(np.int16)
# 保存为WAV文件
with wave.open('test_audio.wav', 'wb') as wav_file:
wav_file.setnchannels(1) # 单声道
wav_file.setsampwidth(2) # 16-bit
wav_file.setframerate(sample_rate) # 16kHz
wav_file.writeframes(audio_data.tobytes())
print("✅ 创建测试音频文件")
# 测试ASR端点
url = "http://192.168.1.141:30101/voice/call/asr"
# 获取token如果需要
token = os.getenv('TEST_TOKEN', '')
headers = {}
if token:
headers['Authorization'] = f'Bearer {token}'
print(f"🚀 测试 ASR 端点: {url}")
with open('test_audio.wav', 'rb') as f:
files = {'audio': ('test_audio.wav', f, 'audio/wav')}
print("📤 发送请求...")
response = requests.post(url, files=files, headers=headers, timeout=60)
print(f"📊 响应状态码: {response.status_code}")
print(f"📋 响应内容: {response.text}")
if response.status_code == 200:
data = response.json()
if data.get('code') == 1 and data.get('data', {}).get('text'):
print(f"✅ ASR 成功: {data['data']['text']}")
return True
else:
print(f"⚠️ ASR 响应格式异常: {data}")
return True # 仍然算成功,因为没有超时
else:
print(f"❌ ASR 请求失败: {response.status_code}")
return False
except requests.exceptions.Timeout:
print("❌ 请求超时")
return False
except Exception as e:
print(f"❌ 测试失败: {e}")
return False
finally:
# 清理测试文件
try:
os.remove('test_audio.wav')
except:
pass
if __name__ == "__main__":
print("🚀 开始测试修复后的 ASR 端点...")
if test_asr_endpoint():
print("🎉 ASR 端点测试成功!")
else:
print("💥 ASR 端点测试失败!")