97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
测试 DashScope ASR 功能
|
||
|
|
"""
|
||
|
|
|
||
|
|
import os
|
||
|
|
from dotenv import load_dotenv
|
||
|
|
|
||
|
|
# 加载环境变量
|
||
|
|
load_dotenv()
|
||
|
|
|
||
|
|
def test_dashscope_asr():
|
||
|
|
"""测试 DashScope ASR"""
|
||
|
|
try:
|
||
|
|
from dashscope.audio.asr import Transcription
|
||
|
|
import dashscope
|
||
|
|
|
||
|
|
api_key = os.getenv('DASHSCOPE_API_KEY')
|
||
|
|
if not api_key:
|
||
|
|
print("❌ 未配置 DASHSCOPE_API_KEY")
|
||
|
|
return False
|
||
|
|
|
||
|
|
dashscope.api_key = api_key
|
||
|
|
print(f"🔧 DashScope API Key: {api_key[:20]}...")
|
||
|
|
|
||
|
|
# 使用一个公开的测试音频 URL
|
||
|
|
test_url = "https://hello12312312.oss-cn-hangzhou.aliyuncs.com/voice_call/test_access.mp3"
|
||
|
|
|
||
|
|
print(f"🎵 测试 URL: {test_url}")
|
||
|
|
|
||
|
|
# 测试不同的模型
|
||
|
|
models = [
|
||
|
|
'paraformer-v2',
|
||
|
|
'paraformer-realtime-v2',
|
||
|
|
'whisper-1'
|
||
|
|
]
|
||
|
|
|
||
|
|
for model in models:
|
||
|
|
try:
|
||
|
|
print(f"\n🔍 测试模型: {model}")
|
||
|
|
|
||
|
|
task_response = Transcription.async_call(
|
||
|
|
model=model,
|
||
|
|
file_urls=[test_url]
|
||
|
|
)
|
||
|
|
|
||
|
|
print(f"📊 任务响应状态: {task_response.status_code}")
|
||
|
|
print(f"📋 任务响应: {task_response}")
|
||
|
|
|
||
|
|
if task_response.status_code == 200:
|
||
|
|
task_id = task_response.output.task_id
|
||
|
|
print(f"✅ 任务创建成功: {task_id}")
|
||
|
|
|
||
|
|
# 等待结果
|
||
|
|
print(f"⏳ 等待识别结果...")
|
||
|
|
result = Transcription.wait(task=task_id)
|
||
|
|
|
||
|
|
print(f"📊 识别结果状态: {result.status_code}")
|
||
|
|
print(f"📋 识别结果: {result}")
|
||
|
|
|
||
|
|
if result.status_code == 200:
|
||
|
|
print(f"✅ 模型 {model} 识别成功!")
|
||
|
|
return True
|
||
|
|
else:
|
||
|
|
print(f"❌ 模型 {model} 识别失败")
|
||
|
|
|
||
|
|
else:
|
||
|
|
print(f"❌ 模型 {model} 任务创建失败")
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ 模型 {model} 测试异常: {e}")
|
||
|
|
continue
|
||
|
|
|
||
|
|
return False
|
||
|
|
|
||
|
|
except ImportError:
|
||
|
|
print("❌ dashscope 模块未安装,请运行: pip install dashscope")
|
||
|
|
return False
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ 测试失败: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
def main():
|
||
|
|
print("🚀 开始测试 DashScope ASR...")
|
||
|
|
|
||
|
|
if test_dashscope_asr():
|
||
|
|
print("🎉 DashScope ASR 测试成功!")
|
||
|
|
else:
|
||
|
|
print("💥 DashScope ASR 测试失败!")
|
||
|
|
print("\n🔧 可能的原因:")
|
||
|
|
print("1. API Key 无效或余额不足")
|
||
|
|
print("2. 模型名称不正确")
|
||
|
|
print("3. 文件格式不支持")
|
||
|
|
print("4. 网络连接问题")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|