Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Merge upstream and harden FunASR connector

LauraGPTd5778a3ce982
diff --cc config/env.transcription.example
index c9b14200,94b33533..92418fd7
--- a/config/env.transcription.example
+++ b/config/env.transcription.example
@@@ -62,7 -62,7 +62,7 @@@ GPT5_VERBOSITY=mediu
# =============================================================================
# CONNECTOR SELECTION (Auto-detected if not set)
# =============================================================================
- # Options: openai_whisper, openai_transcribe, asr_endpoint, mistral, vibevoice, assemblyai, funasr
-# Options: openai_whisper, openai_transcribe, asr_endpoint, mistral, vibevoice, mossland, assemblyai
++# Options: openai_whisper, openai_transcribe, asr_endpoint, mistral, vibevoice, funasr, mossland, assemblyai
# Leave empty to auto-detect based on other settings
# TRANSCRIPTION_CONNECTOR=
@@@ -162,43 -162,28 +162,63 @@@ TRANSCRIPTION_MODEL=gpt-4o-transcribe-d
# If your vLLM server requires authentication:
# TRANSCRIPTION_API_KEY=your_api_key
+# =============================================================================
+# FUNASR CONFIGURATION (Alibaba Cloud)
+# =============================================================================
+# Alibaba Cloud FunASR (DAMO Academy speech recognition) provides high-accuracy
+# transcription with speaker diarization at very low cost. Requires S3-compatible
+# storage for passing pre-signed audio URLs to the API.
+#
+# Docs: https://help.aliyun.com/zh/model-studio/fun-asr-recorded-speech-recognition-restful-api
+#
+# TRANSCRIPTION_CONNECTOR=funasr
+# ASR_BASE_URL=https://dashscope.aliyuncs.com/api/v1
+# ASR_API_KEY=sk-your-aliyun-key
+# ASR_MODEL=paraformer-v2 # 'paraformer-v2' (low cost) or 'fun-asr'
+# ASR_TIMEOUT=1800 # Transcription timeout in seconds (default 30min)
+# ASR_DIARIZE=false # Speaker diarization (default false)
+# ASR_LANGUAGE_HINTS=zh,en # Language hints, comma-separated (default zh,en)
+#
+# --- S3 Storage (Required for FunASR) ---
- # FunASR requires audio files to be uploaded to S3 and passed as pre-signed URLs.
++# FunASR requires audio files to be available through pre-signed S3 URLs.
++# S3_ENDPOINT_URL must be publicly reachable by Alibaba Cloud DashScope; do not
++# use a VPC-only endpoint here. Speakr creates signed URLs per transcription task
++# and does not persist them in the database.
+# S3_BUCKET_NAME=your-bucket
+# S3_ENDPOINT_URL=https://oss-cn-shanghai.aliyuncs.com
+# S3_REGION=cn-shanghai
+# S3_ACCESS_KEY_ID=your-access-key-id
+# S3_SECRET_ACCESS_KEY=your-secret-access-key
- #
- # --- Optional: Aliyun OSS internal endpoint ---
- # Using the internal endpoint within the same VPC eliminates egress bandwidth charges.
- # S3_INTRANET_ENDPOINT_URL=https://oss-cn-shanghai-internal.aliyuncs.com
- #
+# --- FunASR-specific parameters (FUNASR_* prefix) ---
+# FUNASR_POLL_INTERVAL=10 # Seconds between status polls (default 10s)
+# FUNASR_DISFLUENCY_REMOVAL=false # Filter filler words (default false)
+# FUNASR_TIMESTAMP_ALIGNMENT=false # Timestamp alignment (default false)
+# FUNASR_SPEAKER_COUNT=2 # Speaker count hint, 2-100
+# FUNASR_CHANNEL_ID=0 # Audio channel indices, comma-separated
+# FUNASR_VOCABULARY_ID= # Hot-word vocabulary ID
+
+ # =============================================================================
+ # MOSI / MOSSLAND MOSS CONFIGURATION (Hosted Cloud API)
+ # =============================================================================
+ # MOSS provides single-speaker transcription and multi-speaker diarization.
+ # Diarized audio stays in one provider task so speaker identities remain stable;
+ # Speakr app-level chunking is disabled for this connector. The connector uses
+ # SSE first, then resumes a stalled SSE task through the documented task API.
+ # It sends one fixed 131072-token output ceiling for long-form transcription.
+ #
+ # Privacy: recordings are uploaded to the third-party MOSI/Mossland service at
+ # api.mosi.cn. They are not processed entirely on your self-hosted Speakr host.
+ # Create an API key at https://studio.mosi.cn/app/api-keys.
+ #
+ # TRANSCRIPTION_CONNECTOR=mossland
+ # TRANSCRIPTION_API_KEY=your_mosi_api_key
+ # TRANSCRIPTION_MODEL=moss-transcribe-diarize
+ # Available transcription models are read on demand from GET /v1/models; the
+ # two documented model IDs remain available if model discovery is offline.
+ #
+ # Optional API-compatible proxy or alternate endpoint:
+ # TRANSCRIPTION_BASE_URL=https://api.mosi.cn
+
# =============================================================================
# ASSEMBLYAI CONFIGURATION (Cloud)
# =============================================================================
diff --cc src/services/transcription/connectors/alibaba_funasr.py
index f2e87921,00000000..3f8faf5c
mode 100644,000000..100644
--- a/src/services/transcription/connectors/alibaba_funasr.py
+++ b/src/services/transcription/connectors/alibaba_funasr.py
@@@ -1,605 -1,0 +1,594 @@@
+"""
+Alibaba FunASR connector
+
+Supports Alibaba Cloud FunASR (DAMO Academy speech recognition) service.
+Docs: https://help.aliyun.com/zh/model-studio/fun-asr-recorded-speech-recognition-restful-api
+"""
+
++import json
+import logging
+import os
- import httpx
- import json
+import time
- from typing import Dict, Any, Set, Optional
- from urllib.parse import urlparse, urlunparse
++from typing import Any, Dict, Optional, Set
++from urllib.parse import urlsplit, urlunsplit
++
++import httpx
+
+from ..base import (
+ BaseTranscriptionConnector,
+ TranscriptionCapability,
+ TranscriptionRequest,
+ TranscriptionResponse,
+ TranscriptionSegment,
+ ConnectorSpecifications,
+)
+from ..exceptions import TranscriptionError, ConfigurationError, ProviderError
+
+logger = logging.getLogger(__name__)
+
+
++def _redact_url(url: str) -> str:
++ """Remove query credentials before a URL is written to logs."""
++ parsed = urlsplit(url)
++ return urlunsplit((parsed.scheme, parsed.netloc, parsed.path, '', ''))
++
++
+class AlibabaFunASRConnector(BaseTranscriptionConnector):
+ """Alibaba Cloud FunASR connector."""
+
+ CAPABILITIES: Set[TranscriptionCapability] = {
+ TranscriptionCapability.DIARIZATION,
+ TranscriptionCapability.TIMESTAMPS,
+ TranscriptionCapability.LANGUAGE_DETECTION,
+ TranscriptionCapability.SPEAKER_COUNT_CONTROL,
+ }
+ PROVIDER_NAME = "funasr"
+
+ SPECIFICATIONS = ConnectorSpecifications(
+ max_file_size_bytes=None,
+ max_duration_seconds=None,
+ handles_chunking_internally=True, # FunASR handles its own chunking
+ )
+
+ def __init__(self, config: Dict[str, Any]):
+ """
+ Initialize Alibaba FunASR connector.
+
+ Args:
+ config: Configuration dict with keys:
+ - base_url: FunASR API base URL (required)
+ - api_key: API key (required)
+ - model: Model name (default: "fun-asr")
+ - timeout: Transcription timeout in seconds (default: 1800)
+ - poll_interval: Seconds between status polls (default: 10)
+ - diarize: Enable speaker diarization (default: False)
+ - disfluency_removal_enabled: Filter filler words (default: False)
+ - timestamp_alignment_enabled: Timestamp alignment (default: False)
+ - speaker_count: Speaker count hint (2-100)
+ - vocabulary_id: Hot-word ID
+ - language_hints: Language hint array (default: ["zh","en"])
+ - channel_id: Channel indices (default: None = [0])
+ """
+ base_url = config.get('base_url', '')
+ api_key = config.get('api_key', '')
+ model = config.get('model', 'fun-asr')
+
+ self.base_url = base_url.rstrip('/') if base_url else ''
+ self.api_key = api_key
+ self.model = model
+ self._config_timeout = config.get('timeout', 1800)
+ self.poll_interval = config.get('poll_interval', 10)
+ self.default_diarize = config.get('diarize', False)
+ self.disfluency_removal_enabled = config.get('disfluency_removal_enabled', False)
+ self.timestamp_alignment_enabled = config.get('timestamp_alignment_enabled', False)
+ self.speaker_count = config.get('speaker_count', None)
+ self.vocabulary_id = config.get('vocabulary_id', None)
+ self.language_hints = config.get('language_hints', ['zh', 'en'])
+ self.channel_id = config.get('channel_id', None)
+
+ super().__init__(config)
+
+ def _validate_config(self) -> None:
+ """Validate required configuration."""
+ if not self.base_url:
+ raise ConfigurationError(
+ "base_url is required for Alibaba FunASR connector. "
+ "Set ASR_BASE_URL environment variable, "
+ "or provide base_url in config."
+ )
+ if not self.api_key:
+ raise ConfigurationError(
+ "api_key is required for Alibaba FunASR connector. "
+ "Set ASR_API_KEY environment variable, "
+ "or provide api_key in config."
+ )
+
+ @property
+ def timeout(self):
+ """Get timeout with env-var override, falling back to config default."""
+ env_timeout = os.environ.get('ASR_TIMEOUT') or os.environ.get('asr_timeout_seconds')
+ if env_timeout:
+ try:
+ return int(env_timeout)
+ except (ValueError, TypeError):
+ pass
+
+ return self._config_timeout
+
+ def _poll_task_result(self, task_id: str, headers: Dict[str, str]) -> Dict[str, Any]:
+ """
+ Poll async task until completion or timeout.
+
+ Args:
+ task_id: Task ID from the async submission
+ headers: Request headers (Authorization, etc.)
+
+ Returns:
+ Task result data on success
+ """
+ task_url = f"{self.base_url}/tasks/{task_id}"
+
- max_attempts = int(self.timeout / self.poll_interval)
++ max_attempts = max(1, int(self.timeout / self.poll_interval))
+
+ logger.info(f"Polling task result, task_id: {task_id}")
+
+ with httpx.Client() as client:
+ for attempt in range(max_attempts):
+ try:
+ if attempt % 10 == 0:
+ logger.info(f"Poll attempt {attempt + 1}/{max_attempts}")
+
+ response = client.get(task_url, headers=headers, timeout=30.0)
+ response.raise_for_status()
+
+ data = response.json()
+ task_status = data.get('output', {}).get('task_status', 'UNKNOWN')
+
+ if attempt % 10 == 0:
+ logger.info(f"Task status: {task_status}")
+
+ if task_status == 'SUCCEEDED':
+ logger.info("Task completed successfully")
+ return data
- elif task_status == 'FAILED':
++ if task_status == 'FAILED':
+ output = data.get('output', {})
+ error_code = output.get('code', 'UNKNOWN_ERROR')
+ error_msg = output.get('message', 'Task failed')
+
+ logger.debug(f"FunASR full response: {json.dumps(data, indent=2)}")
+ logger.error(f"FunASR error code: {error_code}, message: {error_msg}")
+
+ if error_code == 'ASR_RESPONSE_HAVE_NO_WORDS':
+ raise TranscriptionError(
+ "No speech detected in audio file. Please check:\n"
+ "1. Audio contains clear speech\n"
+ "2. Audio volume is adequate\n"
+ "3. Audio format is correct (WAV, 16kHz, mono recommended)\n"
+ "4. Try with a known-good sample audio"
+ )
+
+ if error_code == 'SUCCESS_WITH_NO_VALID_FRAGMENT':
+ logger.warning(f"FunASR task succeeded but no valid audio fragments: {error_msg}")
+ return {
+ 'output': {
+ 'task_status': 'SUCCEEDED',
+ 'results': [],
+ 'message': error_msg
+ }
+ }
+
+ raise ProviderError(
+ f"FunASR task failed: {error_msg} (code: {error_code})",
+ provider=self.PROVIDER_NAME,
+ status_code=response.status_code
+ )
- elif task_status in ('PENDING', 'RUNNING'):
++ if task_status in ('PENDING', 'RUNNING'):
+ time.sleep(self.poll_interval)
+ else:
+ logger.warning(f"Unknown task status: {task_status}")
+ time.sleep(self.poll_interval)
+
- except httpx.HTTPStatusError as e:
- logger.error(f"Task query failed, status: {e.response.status_code}")
- if attempt < max_attempts - 1:
- time.sleep(self.poll_interval)
- else:
- raise
- except Exception as e:
++ except (httpx.HTTPError, ValueError) as e:
+ logger.error(f"Task query error: {e}")
+ if attempt < max_attempts - 1:
+ time.sleep(self.poll_interval)
+ else:
+ raise
+
+ raise TranscriptionError(f"Task polling timed out, task_id: {task_id}")
+
+ def transcribe(self, request: TranscriptionRequest) -> TranscriptionResponse:
+ """
+ Transcribe audio using Alibaba Cloud FunASR.
+
+ Args:
+ request: Standardized transcription request
+
+ Returns:
+ TranscriptionResponse with segments and speaker info
+ """
+ try:
+ url = f"{self.base_url}/services/audio/asr/transcription"
+
+ headers = {
+ 'Authorization': f'Bearer {self.api_key}',
+ 'Content-Type': 'application/json',
+ 'X-DashScope-Async': 'enable'
+ }
+
+ payload = {
+ 'model': self.model
+ }
+
+ # File URLs injected via extra_options (set up by caller before invoking)
+ funasr_file_urls = (request.extra_options or {}).get('funasr_file_urls') or []
+ if funasr_file_urls:
+ payload['input'] = {
+ 'file_urls': [funasr_file_urls[0]]
+ }
+ else:
+ logger.error("FunASR requires file URL but none provided")
+ raise TranscriptionError(
+ "FunASR requires file URL. Please upload file to S3 first."
+ )
+
+ # Build parameters dict
+ parameters = {}
+ if self.default_diarize:
+ parameters['diarization_enabled'] = True
+ if self.disfluency_removal_enabled:
+ parameters['disfluency_removal_enabled'] = True
+ if self.timestamp_alignment_enabled:
+ parameters['timestamp_alignment_enabled'] = True
+ if self.speaker_count:
+ parameters['speaker_count'] = self.speaker_count
+ if self.vocabulary_id:
+ parameters['vocabulary_id'] = self.vocabulary_id
+ if self.language_hints:
+ parameters['language_hints'] = self.language_hints
+ if self.channel_id:
+ parameters['channel_id'] = self.channel_id
+ if parameters:
+ payload['parameters'] = parameters
+
+ timeout = httpx.Timeout(
+ None,
+ connect=60.0,
+ read=float(self.timeout),
+ write=float(self.timeout),
+ pool=None
+ )
+
- logger.info(f"Sending FunASR request to {url}, payload: {json.dumps(payload, indent=2)[:200]}...")
++ logger.info(
++ "Sending FunASR request to %s (model=%s, files=%d)",
++ url,
++ self.model,
++ len(funasr_file_urls),
++ )
+
+ with httpx.Client() as client:
+ response = client.post(url, json=payload, headers=headers, timeout=timeout)
+ logger.info(f"FunASR request completed, status: {response.status_code}")
+ response.raise_for_status()
+
+ response_text = response.text
+ try:
+ task_data = response.json()
+ except Exception as json_err:
+ if response_text.strip().startswith('<'):
+ logger.error(f"FunASR returned HTML error page (status {response.status_code})")
+ raise ProviderError(
+ "FunASR service returned an HTML error page",
+ provider=self.PROVIDER_NAME,
+ status_code=response.status_code
+ )
+ else:
+ raise ProviderError(
+ f"FunASR service returned invalid response: {json_err}",
+ provider=self.PROVIDER_NAME,
+ status_code=response.status_code
+ )
+
+ task_id = task_data.get('output', {}).get('task_id')
+ if not task_id:
+ logger.error(f"No task_id in response: {task_data}")
+ raise ProviderError(
+ "FunASR did not return a task_id",
+ provider=self.PROVIDER_NAME,
+ status_code=response.status_code
+ )
+
+ logger.info(f"Async task submitted, task_id: {task_id}")
+
+ result_data = self._poll_task_result(task_id, headers)
+
+ output = result_data.get('output', {})
+ results = output.get('results', [])
+
+ if not results:
+ raise TranscriptionError("FunASR returned empty results")
+
+ subtask_status = results[0].get('subtask_status', 'UNKNOWN')
+ if subtask_status == 'FAILED':
+ error_code = results[0].get('code', 'UNKNOWN')
+ error_msg = results[0].get('message', 'Subtask failed')
+ raise TranscriptionError(f"FunASR subtask failed: {error_msg} (code: {error_code})")
+
+ transcription_url = results[0].get('transcription_url')
+ if not transcription_url:
+ raise TranscriptionError("FunASR response missing transcription_url")
+
- logger.info(f"Downloading transcription result: {transcription_url[:100]}...")
++ logger.info(
++ "Downloading transcription result: %s",
++ _redact_url(transcription_url),
++ )
+ transcription_data = self._download_transcription_result(transcription_url)
+
+ return self._parse_transcription_data(transcription_data)
+
+ except httpx.HTTPStatusError as e:
+ logger.error(f"FunASR request failed, status {e.response.status_code}")
+ error_detail = ""
+ try:
+ error_response = e.response.json()
+ error_detail = f": {json.dumps(error_response)}"
+ except Exception:
+ error_detail = f": {e.response.text[:200]}"
+
+ raise ProviderError(
+ f"FunASR request failed, status {e.response.status_code}{error_detail}",
+ provider=self.PROVIDER_NAME,
+ status_code=e.response.status_code
+ ) from e
+
+ except httpx.TimeoutException as e:
+ logger.error(f"FunASR request timed out ({self.timeout}s)")
+ raise TranscriptionError(f"FunASR request timed out ({self.timeout}s)") from e
+
++ except TranscriptionError:
++ raise
+ except Exception as e:
+ error_msg = str(e)
+ logger.error(f"FunASR transcription failed: {error_msg}")
+ raise TranscriptionError(f"FunASR transcription failed: {error_msg}") from e
+
+ def _download_transcription_result(self, transcription_url: str) -> Dict[str, Any]:
+ """
+ Download transcription result from the URL returned by FunASR.
+
+ Args:
+ transcription_url: URL to the transcription result JSON
+
+ Returns:
+ Transcription result data
+ """
+ try:
- logger.info(f"Downloading transcription result: {transcription_url[:100]}...")
++ logger.info(
++ "Downloading transcription result: %s",
++ _redact_url(transcription_url),
++ )
+
+ with httpx.Client(timeout=30.0) as client:
+ response = client.get(transcription_url)
+ response.raise_for_status()
+
+ result_data = response.json()
+ logger.info(f"Transcription result downloaded successfully, size: {len(str(result_data))} chars")
+ return result_data
+
+ except httpx.HTTPStatusError as e:
+ logger.error(f"Transcription result download failed, status: {e.response.status_code}")
+ raise ProviderError(
+ f"Transcription result download failed, status: {e.response.status_code}",
+ provider=self.PROVIDER_NAME,
+ status_code=e.response.status_code
+ ) from e
++ except TranscriptionError:
++ raise
+ except Exception as e:
+ logger.error(f"Transcription result download error: {e}")
+ raise TranscriptionError(f"Transcription result download failed: {e}") from e
+
+ def _parse_transcription_data(self, data: Dict[str, Any]) -> TranscriptionResponse:
+ """
+ Parse actual transcription result downloaded from transcription_url.
+
+ The data format (per Aliyun docs):
+ {
+ "file_url": "...",
+ "transcripts": [{
+ "channel_id": 0,
+ "text": "...",
+ "sentences": [{
+ "begin_time": 100,
+ "end_time": 3820,
+ "text": "...",
+ "sentence_id": 1,
+ "speaker_id": 0,
+ "words": [...]
+ }]
+ }]
+ }
+ """
+ try:
+ segments = []
+ speakers = set()
+ full_text_parts = []
+
+ transcripts = data.get('transcripts', [])
+ logger.info(f"Parsing transcription data, transcripts count: {len(transcripts)}")
+
+ for transcript in transcripts:
+ sentences = transcript.get('sentences', [])
+ for sentence in sentences:
+ text = sentence.get('text', '').strip()
+ if not text:
+ continue
+
+ speaker_num = sentence.get('speaker_id', 0)
+ speaker_id = f"SPEAKER_{speaker_num}"
+
+ # Aliyun timestamps are in milliseconds, convert to seconds
+ begin_ms = sentence.get('begin_time', 0)
+ end_ms = sentence.get('end_time', 0)
+ try:
+ start_time = float(begin_ms) / 1000.0
+ except (ValueError, TypeError):
+ start_time = 0.0
+ try:
+ end_time = float(end_ms) / 1000.0
+ except (ValueError, TypeError):
+ end_time = 0.0
+
+ segment = TranscriptionSegment(
+ text=text,
+ speaker=speaker_id,
+ start_time=start_time,
+ end_time=end_time,
+ )
+ segments.append(segment)
+ speakers.add(speaker_id)
+ full_text_parts.append(f"[{speaker_id}]: {text}")
+
+ full_text = '\n'.join(full_text_parts) if full_text_parts else ''
+
+ logger.info(f"Parse complete: {len(segments)} segments, {len(speakers)} speakers")
+
+ return TranscriptionResponse(
+ text=full_text,
+ segments=segments,
+ speakers=sorted(list(speakers)),
+ language=None,
+ provider=self.PROVIDER_NAME,
+ model=self.model,
+ raw_response=data
+ )
+
+ except Exception as e:
+ logger.error(f"Failed to parse FunASR response: {e}")
+ raise TranscriptionError(f"Failed to parse FunASR response: {e}") from e
+
+ @classmethod
+ def get_config_schema(cls) -> Dict[str, Any]:
+ """Return JSON schema for connector configuration."""
+ return {
+ "type": "object",
+ "required": ["base_url", "api_key"],
+ "properties": {
+ "base_url": {
+ "type": "string",
+ "description": "FunASR base URL (e.g. https://dashscope.aliyuncs.com/api/v1)"
+ },
+ "api_key": {
+ "type": "string",
+ "description": "FunASR API key"
+ },
+ "model": {
+ "type": "string",
+ "default": "fun-asr",
+ "description": "Model name"
+ },
+ "timeout": {
+ "type": "integer",
+ "default": 1800,
+ "description": "Request timeout in seconds"
+ },
+ "diarize": {
+ "type": "boolean",
+ "default": True,
+ "description": "Enable speaker diarization"
+ },
+ "disfluency_removal_enabled": {
+ "type": "boolean",
+ "default": True,
+ "description": "Filter filler words"
+ },
+ "timestamp_alignment_enabled": {
+ "type": "boolean",
+ "default": False,
+ "description": "Enable timestamp alignment"
+ },
+ "language_hints": {
+ "type": "array",
+ "items": {"type": "string"},
+ "default": [],
+ "description": "Language hint array"
+ }
+ }
+ }
+
+
+def prepare_funasr_file_url(recording) -> tuple:
+ """Prepare S3 file URL for FunASR transcription.
+
- Uploads the recording to S3-compatible storage and returns a pre-signed
- URL suitable for passing to the FunASR API.
++ Return a short-lived URL suitable for passing to the FunASR API. Existing
++ S3 objects are signed directly; local recordings are copied to a staging
++ object only when needed. Signed URLs are never persisted.
+
+ Returns:
+ tuple: (bool, list) - (success, file URLs)
+ """
+ from flask import current_app
- from src.config.app_config import S3_INTRANET_ENDPOINT_URL
-
+ from src.services.storage import get_storage_service
+ from src.services.storage.locator import parse_locator
+
+ storage = get_storage_service()
+ if not storage.s3:
+ raise ConfigurationError(
+ "FunASR requires S3 storage to be configured. "
+ "Please set S3_BUCKET_NAME, S3_ENDPOINT_URL, S3_ACCESS_KEY_ID, "
+ "and S3_SECRET_ACCESS_KEY."
+ )
+
- from urllib.parse import urlparse, parse_qs
- import re
- import datetime
-
- if recording.bucket_urls:
- # Check if the pre-signed URL is still valid (not expired)
- _now = datetime.datetime.utcnow()
- _any_valid = False
- for _url in recording.bucket_urls:
- _parsed = urlparse(_url)
- _params = parse_qs(_parsed.query)
- _date_str = _params.get('X-Amz-Date', [None])[0]
- _expires = _params.get('X-Amz-Expires', [None])[0]
- if _date_str and _expires:
- try:
- _sig_time = datetime.datetime.strptime(_date_str, '%Y%m%dT%H%M%SZ')
- if (_now - _sig_time).total_seconds() < int(_expires):
- _any_valid = True
- break
- except (ValueError, TypeError):
- _any_valid = True
- break
- else:
- _any_valid = True
- break
- if _any_valid:
- current_app.logger.info(f"Recording {recording.id} bucket_urls still valid, reusing")
- return True, recording.bucket_urls
- else:
- current_app.logger.info(f"Recording {recording.id} bucket_urls expired, regenerating")
-
+ locator_str = storage.maybe_normalize_local_legacy_locator(recording.audio_path)
+ if not locator_str:
+ locator_str = storage.build_local_locator_from_path(recording.audio_path)
+
+ if not locator_str:
+ current_app.logger.warning(f"Audio path not found for recording {recording.id}")
+ return False, []
+
+ locator_parsed = parse_locator(locator_str)
+ if not locator_parsed:
+ return False, []
+
+ if not storage.exists(locator_str):
+ current_app.logger.warning(f"Audio not found for recording {recording.id}")
+ return False, []
+
- from os.path import splitext
- from werkzeug.utils import secure_filename
-
- _orig_name = recording.original_filename or f"recording_{recording.id}"
- _stem, _orig_ext = splitext(_orig_name)
- _mime_ext_map = {'audio/mpeg': '.mp3', 'audio/flac': '.flac', 'audio/opus': '.opus', 'audio/wav': '.wav'}
- _ext = _mime_ext_map.get(recording.mime_type, _orig_ext)
- safe_filename = secure_filename(_stem + _ext)
- object_key = f"funasr/{recording.id}/{safe_filename}"
- s3_locator = parse_locator(storage.s3.build_locator(object_key))
-
- if not storage.s3.exists(s3_locator):
- current_app.logger.info(f"Uploading recording {recording.id} to S3: {object_key}")
- with storage.materialize(locator_str) as materialized:
- storage.s3.upload_local_file(
- materialized.local_path,
++ if locator_parsed.scheme == 's3':
++ s3_locator = locator_parsed
++ else:
++ from os.path import splitext
++
++ from werkzeug.utils import secure_filename
++
++ original_name = recording.original_filename or f"recording_{recording.id}"
++ stem, original_ext = splitext(original_name)
++ mime_extensions = {
++ 'audio/mpeg': '.mp3',
++ 'audio/flac': '.flac',
++ 'audio/opus': '.opus',
++ 'audio/wav': '.wav',
++ }
++ extension = mime_extensions.get(recording.mime_type, original_ext)
++ safe_filename = secure_filename(stem + extension)
++ object_key = f"funasr/{recording.id}/{safe_filename}"
++ s3_locator = parse_locator(storage.s3.build_locator(object_key))
++
++ if not storage.s3.exists(s3_locator):
++ current_app.logger.info(
++ "Uploading recording %s to FunASR staging object %s",
++ recording.id,
+ object_key,
- content_type=recording.mime_type or 'application/octet-stream',
+ )
++ with storage.materialize(locator_str) as materialized:
++ storage.s3.upload_local_file(
++ materialized.local_path,
++ object_key,
++ content_type=recording.mime_type or 'application/octet-stream',
++ )
+
+ url = storage.s3.presign_get_url(s3_locator, expires_seconds=86400)
-
- if S3_INTRANET_ENDPOINT_URL:
- from src.config.app_config import S3_ENDPOINT_URL
- url = url.replace(S3_ENDPOINT_URL, S3_INTRANET_ENDPOINT_URL)
-
- from src.database import db
- recording.bucket_urls = [url]
- db.session.flush()
+ current_app.logger.info(f"FunASR URL ready for recording {recording.id}")
+ return True, [url]
diff --cc src/tasks/processing.py
index 6bbbba6f,2533b378..4b0eb1c8
--- a/src/tasks/processing.py
+++ b/src/tasks/processing.py
@@@ -2038,20 -2038,7 +2038,17 @@@ def transcribe_with_connector(app_conte
current_app.logger.info(f"Chunked transcription completed: {len(transcription_text)} characters")
else:
# Build the transcription request for single file
- audio_file = open(actual_filepath, 'rb')
- try:
- # FunASR S3 URL preparation.
- # Injected via extra_options so the base
- # TranscriptionRequest stays upstream-clean.
+ with open(actual_filepath, 'rb') as audio_file:
++ # FunASR requires an externally reachable object URL.
+ extra_options = {}
+ if recording:
+ from src.services.transcription import get_registry
+ if get_registry().get_active_connector_name() == 'funasr':
+ from src.services.transcription.connectors.alibaba_funasr import prepare_funasr_file_url
+ success, urls = prepare_funasr_file_url(recording)
+ if success and urls:
+ extra_options['funasr_file_urls'] = urls
+
request = TranscriptionRequest(
audio_file=audio_file,
filename=actual_filename,
@@@ -2063,17 -2050,10 +2060,15 @@@
prompt=initial_prompt,
hotwords=hotwords,
model=transcription_model,
+ extra_options=extra_options,
)
- current_app.logger.info(f"Transcribing with connector: diarize={should_diarize}, language={language}, funasr_urls={bool(extra_options.get('funasr_file_urls'))}")
- try:
- response = connector.transcribe(request)
- finally:
- # 确保文件在transcribe调用后关闭
- audio_file.close()
- except Exception as e:
- raise
- current_app.logger.info(f"Transcribing with connector: diarize={should_diarize}, language={language}")
++ current_app.logger.info(
++ "Transcribing with connector: diarize=%s, language=%s",
++ should_diarize,
++ language,
++ )
+ response = connector.transcribe(request)
# Store the result
if response.segments and response.has_diarization():
diff --cc tests/test_funasr.py
index 7557307d,00000000..a0b63e43
mode 100644,000000..100644
--- a/tests/test_funasr.py
+++ b/tests/test_funasr.py
@@@ -1,95 -1,0 +1,281 @@@
- """Smoke test for FunASR connector URL injection (standalone, no src.app)."""
++"""Tests for the Alibaba Cloud FunASR connector."""
+import sys, os
+sys.path.insert(0, os.getcwd())
+
++from contextlib import contextmanager
++from types import SimpleNamespace
+from unittest.mock import patch, MagicMock
++
++import pytest
++from flask import Flask
++
+from src.services.transcription.base import TranscriptionRequest
- from src.services.transcription.connectors.alibaba_funasr import AlibabaFunASRConnector
- from src.services.transcription.exceptions import TranscriptionError
++from src.services.transcription.connectors.alibaba_funasr import (
++ AlibabaFunASRConnector,
++ prepare_funasr_file_url,
++)
++from src.services.transcription.exceptions import ProviderError, TranscriptionError
+import io
+
+
+def test_funasr_connector_reads_urls_from_extra_options():
+ """AlibabaFunASRConnector reads funasr_file_urls from extra_options."""
+ captured = {}
+
+ def _fake_post(url, **kwargs):
+ if 'json' in kwargs:
+ captured['payload'] = kwargs['json']
+ resp = MagicMock()
+ resp.status_code = 200
- resp.text = '{"output": {"task_id": "tid", "task_status": "SUCCEEDED", "results": []}}'
- resp.json.return_value = {"output": {"task_id": "tid", "task_status": "SUCCEEDED", "results": []}}
++ result = {
++ "output": {
++ "task_id": "tid",
++ "task_status": "SUCCEEDED",
++ "results": [{
++ "subtask_status": "SUCCEEDED",
++ "transcription_url": "https://signed.example/result.json",
++ }],
++ }
++ }
++ resp.text = '{}'
++ resp.json.return_value = result
+ resp.raise_for_status.return_value = None
+ return resp
+
+ connector = AlibabaFunASRConnector(
+ config={'base_url': 'https://dashscope.aliyuncs.com/api/v1', 'api_key': 'sk-test'}
+ )
+ req = TranscriptionRequest(
+ audio_file=io.BytesIO(b'x'),
+ filename='x.mp3',
+ extra_options={'funasr_file_urls': ['https://signed.example/x.mp3?X-Amz-Sig=abc']},
+ )
+
+ with patch('httpx.Client') as client_cls:
+ client_instance = MagicMock()
+ client_instance.__enter__ = MagicMock(return_value=client_instance)
+ client_instance.__exit__ = MagicMock(return_value=False)
+ client_instance.post = MagicMock(side_effect=_fake_post)
+ client_instance.get = MagicMock(side_effect=_fake_post)
+ client_cls.return_value = client_instance
+
+ connector.transcribe(req)
+
+ assert captured['payload']['input']['file_urls'] == ['https://signed.example/x.mp3?X-Amz-Sig=abc']
+ print('PASS: test_funasr_connector_reads_urls_from_extra_options')
+
+
+def test_funasr_connector_raises_when_no_url():
+ """If extra_options has no funasr_file_urls, connector raises clearly."""
+ connector = AlibabaFunASRConnector(
+ config={'base_url': 'https://dashscope.aliyuncs.com/api/v1', 'api_key': 'sk-test'}
+ )
+ req = TranscriptionRequest(audio_file=io.BytesIO(b'x'), filename='x.mp3')
+
+ raised = False
+ try:
+ connector.transcribe(req)
+ except TranscriptionError as exc:
+ raised = True
+ msg = str(exc).lower()
+ assert 'file url' in msg or 's3' in msg, f'Unexpected error: {exc}'
+
+ assert raised, 'Expected TranscriptionError'
+ print('PASS: test_funasr_connector_raises_when_no_url')
+
+
+def test_transcription_request_has_no_file_urls_field():
+ """Bug C guard: TranscriptionRequest must not carry a fork-only file_urls."""
+ import dataclasses
+ field_names = {f.name for f in dataclasses.fields(TranscriptionRequest)}
+ assert 'file_urls' not in field_names, (
+ "TranscriptionRequest.file_urls pollutes the upstream interface; "
+ "use extra_options['funasr_file_urls'] instead."
+ )
+ print('PASS: test_transcription_request_has_no_file_urls_field')
+
+
+def test_transcription_request_extra_options_pass_through():
+ """extra_options is the supported passthrough."""
+ req = TranscriptionRequest(
+ audio_file=io.BytesIO(b'x'),
+ filename='x.mp3',
+ extra_options={'funasr_file_urls': ['https://signed.example/x.mp3']},
+ )
+ assert req.extra_options['funasr_file_urls'] == ['https://signed.example/x.mp3']
+ print('PASS: test_transcription_request_extra_options_pass_through')
+
+
++def test_terminal_provider_failure_is_not_retried():
++ """A terminal task failure must return immediately instead of polling again."""
++ response = MagicMock(status_code=200)
++ response.raise_for_status.return_value = None
++ response.json.return_value = {
++ 'output': {
++ 'task_status': 'FAILED',
++ 'code': 'INVALID_PARAMETER',
++ 'message': 'bad request',
++ }
++ }
++ client = MagicMock()
++ client.__enter__.return_value = client
++ client.__exit__.return_value = False
++ client.get.return_value = response
++ connector = AlibabaFunASRConnector({
++ 'base_url': 'https://dashscope.aliyuncs.com/api/v1',
++ 'api_key': 'sk-test',
++ 'timeout': 20,
++ 'poll_interval': 10,
++ })
++
++ with patch('httpx.Client', return_value=client), patch('time.sleep') as sleep:
++ with pytest.raises(ProviderError, match='INVALID_PARAMETER'):
++ connector._poll_task_result('task-1', {'Authorization': 'Bearer sk-test'})
++
++ assert client.get.call_count == 1
++ sleep.assert_not_called()
++
++
++def test_transcribe_preserves_provider_error_type():
++ """Provider metadata must survive the connector's public error boundary."""
++ response = MagicMock(status_code=200, text='{}')
++ response.raise_for_status.return_value = None
++ response.json.return_value = {'output': {'task_id': 'task-1'}}
++ client = MagicMock()
++ client.__enter__.return_value = client
++ client.__exit__.return_value = False
++ client.post.return_value = response
++ connector = AlibabaFunASRConnector({
++ 'base_url': 'https://dashscope.aliyuncs.com/api/v1',
++ 'api_key': 'sk-test',
++ })
++ request = TranscriptionRequest(
++ audio_file=io.BytesIO(b'x'),
++ filename='x.wav',
++ extra_options={'funasr_file_urls': ['https://signed.example/x.wav']},
++ )
++ provider_error = ProviderError(
++ 'task rejected', provider='funasr', status_code=400
++ )
++
++ with patch('httpx.Client', return_value=client), patch.object(
++ connector, '_poll_task_result', side_effect=provider_error
++ ):
++ with pytest.raises(ProviderError) as exc_info:
++ connector.transcribe(request)
++
++ assert exc_info.value.provider == 'funasr'
++ assert exc_info.value.status_code == 400
++
++
++def test_presigned_urls_are_not_written_to_logs(caplog):
++ """Signed query credentials must not be included in informational logs."""
++ signed_audio_url = (
++ 'https://signed.example/x.wav?X-Amz-Signature=audio-secret'
++ )
++ signed_result_url = (
++ 'https://signed.example/result.json?X-Amz-Signature=result-secret'
++ )
++ response = MagicMock(status_code=200, text='{}')
++ response.raise_for_status.return_value = None
++ response.json.return_value = {'output': {'task_id': 'task-1'}}
++ client = MagicMock()
++ client.__enter__.return_value = client
++ client.__exit__.return_value = False
++ client.post.return_value = response
++ connector = AlibabaFunASRConnector({
++ 'base_url': 'https://dashscope.aliyuncs.com/api/v1',
++ 'api_key': 'sk-test',
++ })
++ request = TranscriptionRequest(
++ audio_file=io.BytesIO(b'x'),
++ filename='x.wav',
++ extra_options={'funasr_file_urls': [signed_audio_url]},
++ )
++ result = {'output': {'results': [{
++ 'subtask_status': 'SUCCEEDED',
++ 'transcription_url': signed_result_url,
++ }]}}
++ parsed = {'transcripts': [{'sentences': [{
++ 'text': 'hello', 'begin_time': 0, 'end_time': 1000,
++ }]}]}
++
++ with caplog.at_level('INFO'), patch('httpx.Client', return_value=client), patch.object(
++ connector, '_poll_task_result', return_value=result
++ ), patch.object(
++ connector, '_download_transcription_result', return_value=parsed
++ ):
++ connector.transcribe(request)
++
++ assert 'audio-secret' not in caplog.text
++ assert 'result-secret' not in caplog.text
++
++
++def _fake_storage(audio_locator='local://recordings/source.wav'):
++ s3 = MagicMock()
++ s3.build_locator.return_value = 's3://bucket/funasr/1/source.wav'
++ s3.exists.return_value = False
++ s3.presign_get_url.return_value = (
++ 'https://signed.example/source.wav?X-Amz-Signature=secret'
++ )
++ storage = MagicMock(s3=s3)
++ storage.maybe_normalize_local_legacy_locator.return_value = audio_locator
++ storage.exists.return_value = True
++
++ @contextmanager
++ def materialize(_locator):
++ yield SimpleNamespace(local_path='/tmp/source.wav')
++
++ storage.materialize.side_effect = materialize
++ return storage
++
++
++def test_prepare_local_audio_does_not_persist_signed_url():
++ """Ephemeral object-store credentials stay inside the active task."""
++ storage = _fake_storage()
++ recording = SimpleNamespace(
++ id=1,
++ audio_path='local://recordings/source.wav',
++ original_filename='source.wav',
++ mime_type='audio/wav',
++ )
++
++ with Flask(__name__).app_context(), patch(
++ 'src.services.storage.get_storage_service', return_value=storage
++ ):
++ success, urls = prepare_funasr_file_url(recording)
++
++ assert success is True
++ assert urls == [storage.s3.presign_get_url.return_value]
++ assert not hasattr(recording, 'bucket_urls')
++
++
++def test_prepare_existing_s3_audio_without_duplicate_upload():
++ """An existing S3 locator is signed directly instead of copied again."""
++ original_locator = 's3://bucket/recordings/source.wav'
++ storage = _fake_storage(audio_locator=original_locator)
++ recording = SimpleNamespace(
++ id=1,
++ audio_path=original_locator,
++ original_filename='source.wav',
++ mime_type='audio/wav',
++ )
++
++ with Flask(__name__).app_context(), patch(
++ 'src.services.storage.get_storage_service', return_value=storage
++ ):
++ success, urls = prepare_funasr_file_url(recording)
++
++ assert success is True
++ assert urls == [storage.s3.presign_get_url.return_value]
++ storage.s3.upload_local_file.assert_not_called()
++ signed_locator = storage.s3.presign_get_url.call_args.args[0]
++ assert signed_locator.raw == original_locator
++
++
+if __name__ == '__main__':
+ test_transcription_request_has_no_file_urls_field()
+ test_transcription_request_extra_options_pass_through()
+ test_funasr_connector_reads_urls_from_extra_options()
+ test_funasr_connector_raises_when_no_url()
+ print('\nAll 4 standalone FunASR tests passed.')