Call the Token Plan speech recognition API to transcribe audio into text.
Audio file or URL: $ARGUMENTS
## Steps
1. Confirm that the input is a local audio path or a publicly accessible URL. The audio must be no longer than 5 minutes. For format and size limits, see the audio specifications in the QwenCloud speech recognition documentation.
2. Install dependencies: pip install requests
3. Use the Bash tool to run the following Python script. The script reads TOKEN_PLAN_API_KEY first, then ANTHROPIC_AUTH_TOKEN. Do not display the key or write it to files or logs.
```python
import base64
import json
import os
import sys
from pathlib import Path
import requests
MIME_TYPES = {
"aac": "audio/aac",
"amr": "audio/amr",
"flac": "audio/flac",
"m4a": "audio/mp4",
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"opus": "audio/opus",
"wav": "audio/wav",
"webm": "audio/webm",
}
source = sys.argv[1]
key = os.environ.get("TOKEN_PLAN_API_KEY") or os.environ.get("ANTHROPIC_AUTH_TOKEN")
if not key:
raise SystemExit("Token Plan API Key not found.")
if source.startswith(("http://", "https://")):
audio_data = source
audio_format = Path(source.split("?")[0]).suffix.lstrip(".").lower() or "mp3"
else:
path = Path(source)
audio_format = path.suffix.lstrip(".").lower()
mime_type = MIME_TYPES.get(audio_format, f"audio/{audio_format}")
audio_data = f"data:{mime_type};base64,{base64.b64encode(path.read_bytes()).decode()}"
payload = {
"model": "qwen-audio-3.0-asr-flash",
"input": {
"messages": [
{
"role": "user",
"content": [
{"type": "input_audio", "input_audio": {"data": audio_data}}
],
}
]
},
"parameters": {"format": audio_format},
}
response = requests.post(
"https://token-plan.maas.qwencloudapi.com/api/v1/services/aigc/multimodal-generation/generation",
headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
json=payload,
timeout=300,
)
if not response.ok:
raise SystemExit(f"{response.status_code}: {response.text}")
result = response.json()
print(result.get("text") or json.dumps(result, ensure_ascii=False, indent=2))
```
4. Output the transcription. If detailed results are needed, show the `sentence` and `words` fields in the response JSON.