Replace Web Speech API with MediaRecorder upload to POST /api/speech/transcribe, calling SenseVoice via the existing GJL API credentials. Co-authored-by: Cursor <cursoragent@cursor.com>
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""
|
|
Speech transcription API — forwards audio to SiliconFlow ASR.
|
|
"""
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
from app.auth_jwt import get_current_user
|
|
from app.errors import UnauthorizedError, ValidationError, ServiceUnavailableError
|
|
from app.services.speech_asr import SpeechAsrService
|
|
|
|
speech_bp = Blueprint('speech', __name__)
|
|
|
|
|
|
@speech_bp.route('/transcribe', methods=['POST'])
|
|
def transcribe_audio():
|
|
user = get_current_user()
|
|
if not user:
|
|
raise UnauthorizedError('Unauthorized')
|
|
|
|
uploaded = request.files.get('audio') or request.files.get('file')
|
|
if not uploaded or not uploaded.filename:
|
|
raise ValidationError('audio file is required')
|
|
|
|
audio_bytes = uploaded.read()
|
|
filename = uploaded.filename or 'recording.webm'
|
|
content_type = uploaded.content_type or ''
|
|
|
|
try:
|
|
SpeechAsrService.validate_upload(filename, len(audio_bytes))
|
|
except ValueError as exc:
|
|
raise ValidationError(str(exc)) from exc
|
|
|
|
try:
|
|
text, model = SpeechAsrService.transcribe(audio_bytes, filename, content_type)
|
|
except RuntimeError as exc:
|
|
raise ServiceUnavailableError(str(exc)) from exc
|
|
|
|
return jsonify({
|
|
'text': text,
|
|
'model': model,
|
|
})
|