重构API格式为纯OpenAI官方whisper API标准,实现自动语言检测#1
Conversation
Co-authored-by: fqscfqj <[email protected]>
Co-authored-by: fqscfqj <[email protected]>
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the API to be fully compatible with OpenAI's Whisper API format while implementing automatic language detection for 25 supported languages. The changes enhance the language processing pipeline to intelligently detect audio language when not explicitly specified and return standardized error responses for unsupported languages.
- Implements automatic language detection using audio analysis and text-based language identification
- Adds OpenAI-compatible error response format for unsupported languages
- Enhances verbose JSON responses to include detected language information
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| app.py | Core logic changes for automatic language detection, error handling, and response formatting |
| README.md | Documentation updates explaining the 25 supported languages and auto-detection features |
| API_TEST_EXAMPLES.py | New test examples demonstrating API behavior with language detection scenarios |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| except Exception as _e: | ||
| print(f"[{unique_id}] 自动语言拒绝阶段异常,已忽略: {_e}") | ||
| print(f"[{unique_id}] 自动语言检测阶段异常,默认使用英语: {_e}") | ||
| detected_language = "en" |
There was a problem hiding this comment.
The default language 'en' is hardcoded in multiple places (lines 1195, 1200, 1203, 1207, 1488). Consider defining a constant DEFAULT_LANGUAGE = 'en' at the module level to improve maintainability and make it easier to change if needed.
| print(f"[{unique_id}] 自动检测到语言: {detected_language}") | ||
| elif ENABLE_AUTO_LANGUAGE_REJECTION: | ||
| # 检测到不支持的语言且启用了自动拒绝 | ||
| return jsonify({ |
There was a problem hiding this comment.
The error response structure is duplicated from the earlier validation logic (around line 1100). Consider extracting this into a helper function like create_unsupported_language_error(language) to reduce code duplication and ensure consistency.
| "code": "unsupported_language" | ||
| } | ||
| }), 400 | ||
| if det_primary: |
There was a problem hiding this comment.
The condition if det_primary: will be true for empty strings that become truthy after .strip().lower().split('-')[0]. This could lead to incorrect behavior if the detected language is an empty string. Consider using if det_primary and det_primary.strip(): for safer validation.
| if det_primary: | |
| if det_primary and det_primary.strip(): |
本PR实现了完全兼容OpenAI Whisper API的语言处理机制,支持25种语言的自动检测和转写。
主要改进
🌍 自动语言检测
当请求中未指定
language参数时,系统现在会:📝 OpenAI兼容的错误响应
对于不支持的语言,现在返回标准的OpenAI格式错误:
{ "error": { "message": "Unsupported language: zh", "type": "invalid_request_error", "param": "language", "code": "unsupported_language" } }🗣️ 支持的25种语言
Bulgarian (bg), Croatian (hr), Czech (cs), Danish (da), Dutch (nl), English (en), Estonian (et), Finnish (fi), French (fr), German (de), Greek (el), Hungarian (hu), Italian (it), Latvian (lv), Lithuanian (lt), Maltese (mt), Polish (pl), Portuguese (pt), Romanian (ro), Slovak (sk), Slovenian (sl), Spanish (es), Swedish (sv), Russian (ru), Ukrainian (uk)
使用示例
自动语言检测
显式指定语言
不支持的语言处理
配置选项
ENABLE_AUTO_LANGUAGE_REJECTION: 控制是否拒绝不支持的语言(默认true)LID_CLIP_SECONDS: 用于语言检测的音频片段长度(默认45秒)兼容性
完全向后兼容现有API调用,同时新增了自动语言检测功能。当未指定语言时,系统会智能检测并在verbose_json响应中返回检测结果。
测试
添加了
API_TEST_EXAMPLES.py展示各种使用场景和预期行为,包含详细的测试用例和配置说明。修复了问题描述中的所有要求:
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.