Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
feat: add Alibaba Cloud FunASR transcription connector with S3 OSS compatibility
ikelvingoea5a290daee0
diff --git a/config/env.transcription.example b/config/env.transcription.exampleindex 82942d85..c9b14200 100644--- a/config/env.transcription.example+++ b/config/env.transcription.example@@ -62,7 +62,7 @@ GPT5_VERBOSITY=medium# =============================================================================# CONNECTOR SELECTION (Auto-detected if not set)# =============================================================================-# Options: openai_whisper, openai_transcribe, asr_endpoint, mistral, vibevoice, assemblyai+# Options: openai_whisper, openai_transcribe, asr_endpoint, mistral, vibevoice, assemblyai, funasr# Leave empty to auto-detect based on other settings# TRANSCRIPTION_CONNECTOR=@@ -162,6 +162,43 @@ TRANSCRIPTION_MODEL=gpt-4o-transcribe-diarize# 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.+# 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+# =============================================================================# ASSEMBLYAI CONFIGURATION (Cloud)# =============================================================================diff --git a/src/api/recordings.py b/src/api/recordings.pyindex 769a1df7..39d86dd5 100644--- a/src/api/recordings.py+++ b/src/api/recordings.py@@ -23,7 +23,15 @@ from email.utils import encode_rfc2231from src.database import dbfrom src.models import *from src.utils import *-from src.config.app_config import ASR_MIN_SPEAKERS, ASR_MAX_SPEAKERS, ASR_DIARIZE, USE_NEW_TRANSCRIPTION_ARCHITECTURE+from src.config.app_config import ASR_BASE_URL, ASR_MIN_SPEAKERS, ASR_MAX_SPEAKERS, ASR_DIARIZE, USE_NEW_TRANSCRIPTION_ARCHITECTURE+from src.services.transcription import get_registry++def get_funasr_active():+ """Check if FunASR is the currently active connector."""+ try:+ return get_registry().get_active_connector_name() == 'funasr'+ except Exception:+ return False# Model validation + the full default-resolution chain now live in one shared@@ -167,6 +175,8 @@ def reindex_recording_chunks_async(recording_id):threading.Thread(target=_run, name=f"reindex-chunks-{recording_id}", daemon=True).start()+# Bucket URL expiration time in hours+BUCKET_URL_EXPIRY_HOURS = 24# Global helpers (will be injected from app)has_recording_access = None@@ -2792,6 +2802,9 @@ def upload_file():meeting_date = nowcurrent_app.logger.debug("No file date available, using current time")+ # Initialize bucket_urls for FunASR (populated below if active)+ bucket_urls = []+recording = Recording(audio_path=None,original_filename=original_filename,@@ -2812,6 +2825,7 @@ def upload_file():# regardless of the server's VIDEO_RETENTION setting. True when# the user explicitly toggled it or when VIDEO_RETENTION is off.keep_audio_only=effective_audio_only,+ bucket_urls=bucket_urls if bucket_urls else None)db.session.add(recording)db.session.flush() # Assign recording.id without committing transaction yet@@ -2828,6 +2842,15 @@ def upload_file():# always read from recording.audio_path via the storage facade.recording.audio_path = stored_object.locator+ # FunASR S3 URL preparation+ if get_funasr_active():+ from src.services.transcription.connectors.alibaba_funasr import prepare_funasr_file_url+ success, funasr_urls = prepare_funasr_file_url(recording)+ if success and funasr_urls:+ recording.bucket_urls = funasr_urls+ elif get_funasr_active():+ return jsonify({'error': 'FunASR requires S3 upload but it failed'}), 500+# Add tags to recording if selected (preserve order)for order, tag in enumerate(selected_tags, 1):new_association = RecordingTag(diff --git a/src/config/app_config.py b/src/config/app_config.pyindex 99f66238..f11abef0 100644--- a/src/config/app_config.py+++ b/src/config/app_config.py@@ -56,6 +56,10 @@ USE_ASR_ENDPOINT = os.environ.get('USE_ASR_ENDPOINT', 'false').lower() == 'true'ASR_BASE_URL = os.environ.get('ASR_BASE_URL')if ASR_BASE_URL:ASR_BASE_URL = ASR_BASE_URL.split('#')[0].strip()+ASR_API_KEY = os.environ.get('ASR_API_KEY', '')+if ASR_API_KEY:+ ASR_API_KEY = ASR_API_KEY.split('#')[0].strip()+ASR_MODEL = os.environ.get('ASR_MODEL', '')if USE_ASR_ENDPOINT:ASR_DIARIZE = os.environ.get('ASR_DIARIZE', 'true').lower() == 'true'@@ -98,11 +102,20 @@ S3_ENDPOINT_URL = os.environ.get('S3_ENDPOINT_URL', '').strip() or NoneS3_ACCESS_KEY_ID = os.environ.get('S3_ACCESS_KEY_ID', '').strip() or NoneS3_SECRET_ACCESS_KEY = os.environ.get('S3_SECRET_ACCESS_KEY', '').strip() or NoneS3_SESSION_TOKEN = os.environ.get('S3_SESSION_TOKEN', '').strip() or None-S3_USE_PATH_STYLE = os.environ.get('S3_USE_PATH_STYLE', 'false').lower() == 'true'-S3_VERIFY_SSL = os.environ.get('S3_VERIFY_SSL', 'true').lower() == 'true'S3_PRESIGN_TTL_SECONDS = int(os.environ.get('S3_PRESIGN_TTL_SECONDS', '900'))S3_PRESIGN_PUBLIC_TTL_SECONDS = int(os.environ.get('S3_PRESIGN_PUBLIC_TTL_SECONDS', '300'))+# Normalize S3 endpoint URLs: add https:// if protocol is missing+if S3_ENDPOINT_URL and not S3_ENDPOINT_URL.startswith(('http://', 'https://')):+ S3_ENDPOINT_URL = f'https://{S3_ENDPOINT_URL}'++# Aliyun OSS internal endpoint (optional, eliminates egress bandwidth charges)+S3_INTRANET_ENDPOINT_URL = os.environ.get('S3_INTRANET_ENDPOINT_URL', '').strip() or None+if S3_INTRANET_ENDPOINT_URL and not S3_INTRANET_ENDPOINT_URL.startswith(('http://', 'https://')):+ S3_INTRANET_ENDPOINT_URL = f'https://{S3_INTRANET_ENDPOINT_URL}'+S3_USE_PATH_STYLE = os.environ.get('S3_USE_PATH_STYLE', 'false').lower() == 'true'+S3_VERIFY_SSL = os.environ.get('S3_VERIFY_SSL', 'true').lower() == 'true'+# Unsupported codecs - comma-separated list of codecs to exclude from the default supported list# Useful when your transcription service doesn't support certain codecs (e.g., vllm doesn't support opus)# Example: AUDIO_UNSUPPORTED_CODECS=opus,vorbisdiff --git a/src/init_db.py b/src/init_db.pyindex a4e4d941..4d93100b 100644--- a/src/init_db.py+++ b/src/init_db.py@@ -669,6 +669,10 @@ def initialize_database(app):except Exception as e:app.logger.warning(f"Could not create composite index on webhook_delivery: {e}")+ # Add bucket_urls column for S3-compatible bucket storage+ if add_column_if_not_exists(engine, 'recording', 'bucket_urls', 'JSON'):+ app.logger.info("Added bucket_urls column to recording table")+# Add folder_id column to recording table for folders featureif add_column_if_not_exists(engine, 'recording', 'folder_id', 'INTEGER'):app.logger.info("Added folder_id column to recording table")diff --git a/src/models/recording.py b/src/models/recording.pyindex c3d7cf1b..868859e1 100644--- a/src/models/recording.py+++ b/src/models/recording.py@@ -67,6 +67,9 @@ class Recording(db.Model):# Substituted into {{name}} placeholders in the resolved summary prompt at summarisation time.prompt_variables = db.Column(db.JSON, nullable=True)+ # Bucket storage URLs (JSON list of URLs when file is stored in S3-compatible bucket)+ bucket_urls = db.Column(db.JSON, nullable=True)+# Effective transcription hints actually sent to the ASR for this recording,# captured at transcription time after tag/folder/user and admin-default# resolution. Persisted so the UI can show which hotwords/initial prompt were@@ -452,6 +455,7 @@ class Recording(db.Model):'shared_with_count': shared_with_count,'public_share_count': public_share_count,'keep_audio_only': self.keep_audio_only,+ 'bucket_urls': self.bucket_urls}# Only compute expensive HTML conversions when explicitly requesteddiff --git a/src/services/job_queue.py b/src/services/job_queue.pyindex f18d9d51..b6dc901b 100644--- a/src/services/job_queue.py+++ b/src/services/job_queue.py@@ -483,6 +483,13 @@ class FairJobQueue:if not audio_path:logger.error(f"Cannot run reprocess transcription job for recording {recording.id}: missing audio_path")raise ValueError(f"Missing audio_path for recording {recording.id}")++ # FunASR S3 URL preparation+ 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+ prepare_funasr_file_url(recording)+with storage.materialize(audio_path) as materialized:filepath = materialized.local_pathfilename_for_asr = recording.original_filename or os.path.basename(filepath)diff --git a/src/services/storage/s3.py b/src/services/storage/s3.pyindex 3d0bf498..588e5d10 100644--- a/src/services/storage/s3.py+++ b/src/services/storage/s3.py@@ -53,8 +53,12 @@ class S3StorageBackend:if self.session_token:client_kwargs['aws_session_token'] = self.session_token- addressing_style = 'path' if self.use_path_style else 'auto'- client_kwargs['config'] = Config(signature_version='s3v4', s3={'addressing_style': addressing_style})+ addressing_style = 'path' if self.use_path_style else 'virtual'+ client_kwargs['config'] = Config(+ signature_version='s3v4',+ s3={'addressing_style': addressing_style},+ request_checksum_calculation="when_required",+ )self._client = boto3.client(**client_kwargs)return self._client@@ -92,10 +96,10 @@ class S3StorageBackend:extra['ContentType'] = content_typeif metadata:extra['Metadata'] = metadata- if extra:- client.upload_file(local_path, self.bucket, key, ExtraArgs=extra)- else:- client.upload_file(local_path, self.bucket, key)+ # Use put_object (single PUT) instead of upload_file (multipart with chunked encoding)+ # Aliyun OSS rejects aws-chunked encoding used by boto3's multipart upload+ with open(local_path, 'rb') as body:+ client.put_object(Bucket=self.bucket, Key=key, Body=body, **extra)if delete_source:try:os.remove(local_path)diff --git a/src/services/transcription/__init__.py b/src/services/transcription/__init__.pyindex 4b409f4d..cd2a913e 100644--- a/src/services/transcription/__init__.py+++ b/src/services/transcription/__init__.py@@ -62,6 +62,7 @@ from .connectors import (OpenAIWhisperConnector,OpenAITranscribeConnector,ASREndpointConnector,+ AlibabaFunASRConnector,)__all__ = [@@ -95,4 +96,5 @@ __all__ = ['OpenAIWhisperConnector','OpenAITranscribeConnector','ASREndpointConnector',+ 'AlibabaFunASRConnector',]diff --git a/src/services/transcription/connectors/__init__.py b/src/services/transcription/connectors/__init__.pyindex b04d7714..6cb0ec93 100644--- a/src/services/transcription/connectors/__init__.py+++ b/src/services/transcription/connectors/__init__.py@@ -8,6 +8,8 @@ from .asr_endpoint import ASREndpointConnectorfrom .azure_openai_transcribe import AzureOpenAITranscribeConnectorfrom .mistral import MistralTranscriptionConnectorfrom .vibevoice import VibeVoiceTranscriptionConnector+from .alibaba_funasr import AlibabaFunASRConnector+from .mossland import MosslandTranscriptionConnector__all__ = ['OpenAIWhisperConnector',@@ -16,4 +18,6 @@ __all__ = ['AzureOpenAITranscribeConnector','MistralTranscriptionConnector','VibeVoiceTranscriptionConnector',+ 'AlibabaFunASRConnector',+ 'MosslandTranscriptionConnector',]diff --git a/src/services/transcription/connectors/alibaba_funasr.py b/src/services/transcription/connectors/alibaba_funasr.pynew file mode 100644index 00000000..f2e87921--- /dev/null+++ b/src/services/transcription/connectors/alibaba_funasr.py@@ -0,0 +1,605 @@+"""+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 logging+import os+import httpx+import json+import time+from typing import Dict, Any, Set, Optional+from urllib.parse import urlparse, urlunparse++from ..base import (+ BaseTranscriptionConnector,+ TranscriptionCapability,+ TranscriptionRequest,+ TranscriptionResponse,+ TranscriptionSegment,+ ConnectorSpecifications,+)+from ..exceptions import TranscriptionError, ConfigurationError, ProviderError++logger = logging.getLogger(__name__)+++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)++ 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':+ 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'):+ 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:+ 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]}...")++ 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]}...")+ 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 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]}...")++ 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 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.++ 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,+ 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 --git a/src/services/transcription/registry.py b/src/services/transcription/registry.pyindex 05e3d4d2..59864dde 100644--- a/src/services/transcription/registry.py+++ b/src/services/transcription/registry.py@@ -47,6 +47,8 @@ class ConnectorRegistry:from .connectors.azure_openai_transcribe import AzureOpenAITranscribeConnectorfrom .connectors.mistral import MistralTranscriptionConnectorfrom .connectors.vibevoice import VibeVoiceTranscriptionConnector+ from .connectors.alibaba_funasr import AlibabaFunASRConnector+ from .connectors.mossland import MosslandTranscriptionConnectorfrom .connectors.assemblyai import AssemblyAITranscriptionConnectorself.register('openai_whisper', OpenAIWhisperConnector)@@ -55,6 +57,8 @@ class ConnectorRegistry:self.register('azure_openai_transcribe', AzureOpenAITranscribeConnector)self.register('mistral', MistralTranscriptionConnector)self.register('vibevoice', VibeVoiceTranscriptionConnector)+ self.register('funasr', AlibabaFunASRConnector)+ self.register('mossland', MosslandTranscriptionConnector)self.register('assemblyai', AssemblyAITranscriptionConnector)def register(self, name: str, connector_class: Type[BaseTranscriptionConnector]):@@ -124,11 +128,12 @@ class ConnectorRegistry:Auto-detection priority:1. TRANSCRIPTION_CONNECTOR - explicit connector name- 2. ASR_BASE_URL is set - use ASR endpoint (smarter detection)+ 2. ASR_BASE_URL contains dashscope - use FunASR connector+ 3. ASR_BASE_URL is set - use ASR endpoint (smarter detection)- USE_ASR_ENDPOINT=true also works (backwards compat, with deprecation warning)- 3. TRANSCRIPTION_MODEL contains 'gpt-4o' - use OpenAI Transcribe- 4. TRANSCRIPTION_MODEL is set - use OpenAI Whisper with that model- 5. Default to OpenAI Whisper (whisper-1)+ 4. TRANSCRIPTION_MODEL contains 'gpt-4o' - use OpenAI Transcribe+ 5. TRANSCRIPTION_MODEL is set - use OpenAI Whisper with that model+ 6. Default to OpenAI Whisper (whisper-1)Returns:The initialized connector@@ -149,20 +154,24 @@ class ConnectorRegistry:"Set ASR_BASE_URL instead for auto-detection, or use TRANSCRIPTION_CONNECTOR=asr_endpoint")- # Priority 2: ASR endpoint - check ASR_BASE_URL or legacy flag- if asr_base_url or use_asr_flag:+ # Priority 2: FunASR - auto-detect from ASR_BASE_URL containing dashscope+ if asr_base_url and 'dashscope.aliyuncs.com' in asr_base_url:+ connector_name = 'funasr'+ logger.info("Auto-detected FunASR from ASR_BASE_URL")+ # Priority 3: Other ASR endpoint - check ASR_BASE_URL or legacy flag+ elif asr_base_url or use_asr_flag:connector_name = 'asr_endpoint'if asr_base_url:logger.info("Auto-detected ASR endpoint from ASR_BASE_URL")- # Priority 2.5: Azure OpenAI - check for Azure endpoint URL+ # Priority 3.5: Azure OpenAI - check for Azure endpoint URLelif self._is_azure_endpoint():connector_name = 'azure_openai_transcribe'logger.info("Auto-detected Azure OpenAI from TRANSCRIPTION_BASE_URL")- # Priority 3: Model-based detection+ # Priority 4: Model-based detectionelif transcription_model and 'gpt-4o' in transcription_model:connector_name = 'openai_transcribe'logger.info(f"Auto-detected OpenAI Transcribe from TRANSCRIPTION_MODEL={transcription_model}")- # Priority 4 & 5: OpenAI Whisper (with custom or default model)+ # Priority 5 & 6: OpenAI Whisper (with custom or default model)else:connector_name = 'openai_whisper'model = transcription_model or whisper_model or 'whisper-1'@@ -233,6 +242,7 @@ class ConnectorRegistry:return {'base_url': base_url,+ 'api_key': os.environ.get('ASR_API_KEY', ''),'timeout': self._get_asr_timeout(),'diarize': os.environ.get('ASR_DIARIZE', 'true').lower() == 'true','return_speaker_embeddings': os.environ.get('ASR_RETURN_SPEAKER_EMBEDDINGS', 'false').lower() == 'true'@@ -291,6 +301,55 @@ class ConnectorRegistry:'api_key': os.environ.get('TRANSCRIPTION_API_KEY', ''),}+ elif connector_name == 'funasr':+ base_url = os.environ.get('ASR_BASE_URL', '')+ if base_url:+ base_url = base_url.split('#')[0].strip()+ api_key = os.environ.get('ASR_API_KEY', '')+ model = os.environ.get('ASR_MODEL', 'fun-asr') or 'fun-asr'++ timeout = int(os.environ.get('ASR_TIMEOUT') or '1800')+ diarize = (os.environ.get('ASR_DIARIZE') or 'false').lower() == 'true'+ lang_str = os.environ.get('ASR_LANGUAGE_HINTS') or 'zh,en'+ language_hints = [l.strip() for l in lang_str.split(',') if l.strip()]++ # FunASR-specific params (only FUNASR_* prefix)+ poll_interval = float(os.environ.get('FUNASR_POLL_INTERVAL', '10'))+ disfluency = os.environ.get('FUNASR_DISFLUENCY_REMOVAL', 'false').lower() == 'true'+ timestamp_align = os.environ.get('FUNASR_TIMESTAMP_ALIGNMENT', 'false').lower() == 'true'+ speaker_count = os.environ.get('FUNASR_SPEAKER_COUNT', '').strip() or None+ vocabulary_id = os.environ.get('FUNASR_VOCABULARY_ID', '').strip() or None++ # channel_id: comma-separated integers+ channel_str = os.environ.get('FUNASR_CHANNEL_ID', '')+ channel_id = [int(c.strip()) for c in channel_str.split(',') if c.strip()] or None++ return {+ 'base_url': base_url,+ 'api_key': api_key,+ 'model': model,+ 'timeout': timeout,+ 'poll_interval': poll_interval,+ 'diarize': diarize,+ 'disfluency_removal_enabled': disfluency,+ 'timestamp_alignment_enabled': timestamp_align,+ 'speaker_count': int(speaker_count) if speaker_count else None,+ 'vocabulary_id': vocabulary_id,+ 'language_hints': language_hints,+ 'channel_id': channel_id,+ }++ elif connector_name == 'mossland':+ base_url = os.environ.get('TRANSCRIPTION_BASE_URL', '')+ if base_url:+ base_url = base_url.split('#')[0].strip()++ return {+ 'api_key': os.environ.get('TRANSCRIPTION_API_KEY', ''),+ 'base_url': base_url or 'https://api.mosi.cn',+ 'model': os.environ.get('TRANSCRIPTION_MODEL', 'moss-transcribe-diarize'),+ }+elif connector_name == 'assemblyai':# AssemblyAI's endpoint is fixed (api.assemblyai.com), so we do NOT# read the shared TRANSCRIPTION_BASE_URL here: a stale value leftdiff --git a/src/tasks/processing.py b/src/tasks/processing.pyindex 2533b378..6bbbba6f 100644--- a/src/tasks/processing.py+++ b/src/tasks/processing.py@@ -33,7 +33,7 @@ from src.config.app_config import AUDIO_COMPRESS_UPLOADS, AUDIO_CODEC, AUDIO_BITfrom src.audio_chunking import AudioChunkingService, ChunkProcessingError, ChunkingNotSupportedErrorfrom src.config.app_config import (ASR_DIARIZE, ASR_BASE_URL, ASR_RETURN_SPEAKER_EMBEDDINGS,- transcription_api_key, transcription_base_url, chunking_service, ENABLE_CHUNKING+ transcription_api_key, transcription_base_url, chunking_service, ENABLE_CHUNKING,)from src.file_exporter import export_recording, ENABLE_AUTO_EXPORTfrom src.services.transcription_tracking import transcription_tracker@@ -2038,7 +2038,20 @@ def transcribe_with_connector(app_context, recording_id, filepath, original_filecurrent_app.logger.info(f"Chunked transcription completed: {len(transcription_text)} characters")else:# Build the transcription request for single file- with open(actual_filepath, 'rb') as audio_file:+ audio_file = open(actual_filepath, 'rb')+ try:+ # FunASR S3 URL preparation.+ # Injected via extra_options so the base+ # TranscriptionRequest stays upstream-clean.+ 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,@@ -2050,10 +2063,17 @@ def transcribe_with_connector(app_context, recording_id, filepath, original_fileprompt=initial_prompt,hotwords=hotwords,model=transcription_model,+ extra_options=extra_options,)- current_app.logger.info(f"Transcribing with connector: diarize={should_diarize}, language={language}")- response = connector.transcribe(request)+ 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# Store the resultif response.segments and response.has_diarization():diff --git a/tests/test_funasr.py b/tests/test_funasr.pynew file mode 100644index 00000000..7557307d--- /dev/null+++ b/tests/test_funasr.py@@ -0,0 +1,95 @@+"""Smoke test for FunASR connector URL injection (standalone, no src.app)."""+import sys, os+sys.path.insert(0, os.getcwd())++from unittest.mock import patch, MagicMock+from src.services.transcription.base import TranscriptionRequest+from src.services.transcription.connectors.alibaba_funasr import AlibabaFunASRConnector+from src.services.transcription.exceptions import 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": []}}+ 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')+++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.')