Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

重构API格式为纯OpenAI官方whisper API标准,实现自动语言检测#1

Merged
fqscfqj merged 4 commits into
mainfrom
copilot/fix-1c86f6f4-040f-47af-8867-8499851d5fec
Aug 22, 2025
Merged

重构API格式为纯OpenAI官方whisper API标准,实现自动语言检测#1
fqscfqj merged 4 commits into
mainfrom
copilot/fix-1c86f6f4-040f-47af-8867-8499851d5fec

Conversation

Copilot AI commented Aug 22, 2025

Copy link
Copy Markdown
Contributor

本PR实现了完全兼容OpenAI Whisper API的语言处理机制,支持25种语言的自动检测和转写。

主要改进

🌍 自动语言检测

当请求中未指定 language 参数时,系统现在会:

  • 自动提取音频前45秒进行快速转写
  • 使用langdetect库分析文本内容检测语言
  • 如果检测到支持的语言,自动使用该语言进行完整转写
  • 在响应中返回检测到的实际语言代码

📝 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)

使用示例

自动语言检测

curl -X POST "http://localhost:5092/v1/audio/transcriptions" \
  -F "file=@spanish_audio.wav" \
  -F "response_format=verbose_json"
# 返回: {"language": "es", "text": "Hola mundo", ...}

显式指定语言

curl -X POST "http://localhost:5092/v1/audio/transcriptions" \
  -F "[email protected]" \
  -F "language=en"

不支持的语言处理

curl -X POST "http://localhost:5092/v1/audio/transcriptions" \
  -F "[email protected]" \
  -F "language=zh"
# 返回: 400错误,标准OpenAI格式

配置选项

  • ENABLE_AUTO_LANGUAGE_REJECTION: 控制是否拒绝不支持的语言(默认true)
  • LID_CLIP_SECONDS: 用于语言检测的音频片段长度(默认45秒)

兼容性

完全向后兼容现有API调用,同时新增了自动语言检测功能。当未指定语言时,系统会智能检测并在verbose_json响应中返回检测结果。

测试

添加了API_TEST_EXAMPLES.py展示各种使用场景和预期行为,包含详细的测试用例和配置说明。

修复了问题描述中的所有要求:

  • ✅ 重构为纯OpenAI官方whisper API格式
  • ✅ 支持25种语言列表验证
  • ✅ 不支持语言时返回OpenAI格式错误
  • ✅ 未传入language时自动识别语言并转写

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] 重构api格式,以纯openai官方whisper api为准。目前模型兼容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), It... 重构API格式为纯OpenAI官方whisper API标准,实现自动语言检测 Aug 22, 2025
Copilot AI requested a review from fqscfqj August 22, 2025 09:40
@fqscfqj
fqscfqj marked this pull request as ready for review August 22, 2025 09:54
Copilot AI review requested due to automatic review settings August 22, 2025 09:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread app.py
except Exception as _e:
print(f"[{unique_id}] 自动语言拒绝阶段异常,已忽略: {_e}")
print(f"[{unique_id}] 自动语言检测阶段异常,默认使用英语: {_e}")
detected_language = "en"

Copilot AI Aug 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread app.py
print(f"[{unique_id}] 自动检测到语言: {detected_language}")
elif ENABLE_AUTO_LANGUAGE_REJECTION:
# 检测到不支持的语言且启用了自动拒绝
return jsonify({

Copilot AI Aug 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread app.py
"code": "unsupported_language"
}
}), 400
if det_primary:

Copilot AI Aug 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if det_primary:
if det_primary and det_primary.strip():

Copilot uses AI. Check for mistakes.
@fqscfqj
fqscfqj merged commit ebdcc84 into main Aug 22, 2025
9 checks passed
@fqscfqj
fqscfqj deleted the copilot/fix-1c86f6f4-040f-47af-8867-8499851d5fec branch August 25, 2025 16:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants