Files
life-echo/api/app/features/evaluation/asr_service.py
Sully 105b50a277 merge dark mode and google OAuth (#35)
* feat(api): implement Google OAuth login and user management

- Added Google OpenID Connect login functionality, allowing users to authenticate using their Google accounts.
- Created new endpoints for Google login, including user registration and linking existing accounts.
- Introduced Google token verification logic and error handling for authentication failures.
- Updated environment configuration to include Google OAuth client IDs and verification settings.
- Enhanced user model to support OpenID and linked Google accounts.

This feature improves user experience by enabling seamless sign-in with Google, while maintaining security and integrity of user data.

* fix(auth): wire staging Google token verifier

* chore(deps): update expo to version 55.0.6 and adjust @expo/env dependency in pnpm-lock.yaml

* chore(deps): update Babel dependencies to version 7.29.7 in package-lock.json

* feat(auth): enhance phone login for China users

- Updated phone login functionality to support only mainland China (+86) mobile numbers.
- Added user prompts and descriptions for phone login, including confirmation and cancellation options.
- Adjusted translations for both English and Chinese to reflect the new phone login requirements.
- Updated Google OAuth client IDs in configuration files for production and staging environments.

* chore(deps): add peer flag to use-sync-external-store in package-lock.json

* chore(deps): add @emnapi/core and @emnapi/runtime to package-lock.json

* fix(app-expo): align Android native dependencies

* fix(app-expo): normalize lockfile for npm 10

* fix(config): update environment variable handling to use static access

- Introduced a static mapping for public environment variables to ensure proper access during the release bundle.
- Updated the `requirePublicEnv` and `optionalPublicEnv` functions to reference the new `PUBLIC_ENV` object instead of directly accessing `process.env`.
- Added comments to clarify the necessity of static access for certain environment variables.

* feat(app-expo): dark mode, FAQ i18n, eval ASR, and theme cleanup (#34)

* feat(app-expo): dark mode, FAQ i18n, version CI, and theme cleanup

Implement light/dark scene colors across chat, reading, and headers; remove
default/brand theme picker and ThemeVariablesProvider. Localize FAQ in-app,
fix dark-mode text visibility, and remove the unused /api/faqs endpoint.
Align About/version with Expo config and inject APP_VERSION in CI builds.

Also includes phone E164 auth/SMS updates, eval ASR page, and related API work.

* revert: remove phone E.164 changes from dark-mode branch

These auth/SMS internationalization updates were accidentally bundled into
the dark-mode commit; restore 11-digit CN phone flow and drop related API,
migration, and Expo UI work from this branch.

* fix: address PR review issues for dark mode and eval ASR

Use light foreground colors for sepia reading in dark mode, fix chat send
button contrast, stream-limit eval ASR uploads, restore LiveTester phone
validation, and remove unused AudioSegmenter code.

* fix(app-expo): improve chat send button contrast in light and dark mode

Add dedicated send button colors (accent fill in dark, primary fill in
light), use RNText to avoid NativeWind overrides, and restore dark labels
in light mode for readable composer actions.

---------

Co-authored-by: Kevin <kevin@brighteng.org>

---------

Co-authored-by: penghanyuan <penghanyuan@gmail.com>
Co-authored-by: Kevin <kevin@brighteng.org>
2026-06-09 11:14:36 +08:00

125 lines
3.2 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.
"""内部评测 ASR 转写服务。"""
from __future__ import annotations
from fastapi import UploadFile
from app.core.agent_logging import log_asr_transcript_result
from app.core.errors import ProviderError
from app.core.logging import get_logger
from app.features.evaluation.errors import EvaluationBadRequestError
from app.ports.asr import ASRProvider, ASRTranscriptionError
logger = get_logger(__name__)
MAX_AUDIO_BYTES = 100 * 1024 * 1024
_READ_CHUNK_BYTES = 1024 * 1024
_ALLOWED_FORMATS = frozenset(
{"m4a", "mp3", "wav", "aac", "amr", "ogg-opus", "speex", "silk", "pcm"}
)
_EXT_TO_FORMAT: dict[str, str] = {
"m4a": "m4a",
"mp4": "m4a",
"mp3": "mp3",
"wav": "wav",
"aac": "aac",
"amr": "amr",
"ogg": "ogg-opus",
"opus": "ogg-opus",
"webm": "ogg-opus",
"pcm": "pcm",
"speex": "speex",
"silk": "silk",
}
_CONTENT_TYPE_TO_FORMAT: dict[str, str] = {
"audio/mp4": "m4a",
"audio/x-m4a": "m4a",
"audio/m4a": "m4a",
"audio/mpeg": "mp3",
"audio/mp3": "mp3",
"audio/wav": "wav",
"audio/x-wav": "wav",
"audio/wave": "wav",
"audio/aac": "aac",
"audio/amr": "amr",
"audio/ogg": "ogg-opus",
"audio/webm": "ogg-opus",
}
def resolve_voice_format(
*,
explicit: str | None,
filename: str | None,
content_type: str | None,
) -> str:
if explicit:
fmt = explicit.strip().lower()
if fmt in _ALLOWED_FORMATS:
return fmt
raise EvaluationBadRequestError(f"不支持的音频格式:{fmt}")
if filename:
ext = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
if ext in _EXT_TO_FORMAT:
return _EXT_TO_FORMAT[ext]
if content_type:
base = content_type.split(";", 1)[0].strip().lower()
if base in _CONTENT_TYPE_TO_FORMAT:
return _CONTENT_TYPE_TO_FORMAT[base]
raise EvaluationBadRequestError(
"无法识别音频格式,请上传 m4a/mp3/wav 或在请求中指定 format 参数"
)
async def read_limited_upload(
file: UploadFile,
*,
max_bytes: int = MAX_AUDIO_BYTES,
) -> bytes:
chunks: list[bytes] = []
total = 0
while True:
chunk = await file.read(_READ_CHUNK_BYTES)
if not chunk:
break
total += len(chunk)
if total > max_bytes:
raise EvaluationBadRequestError("音频过大(上限 100MB")
chunks.append(chunk)
return b"".join(chunks)
class EvalAsrService:
def __init__(self, asr: ASRProvider) -> None:
self._asr = asr
async def transcribe(
self,
audio: bytes,
*,
voice_format: str,
source_label: str = "eval_asr",
) -> str:
if not audio:
raise EvaluationBadRequestError("音频为空")
if len(audio) > MAX_AUDIO_BYTES:
raise EvaluationBadRequestError("音频过大(上限 100MB")
try:
text = await self._asr.transcribe(audio, voice_format)
except ASRTranscriptionError as e:
raise ProviderError(str(e), provider="asr") from e
log_asr_transcript_result(
logger,
text=text or "",
conversation_id=None,
source=source_label,
)
return text or ""