Files
syops/backend/app/api/speech.py
T

41 lines
1.2 KiB
Python

"""
Speech transcription API — forwards audio to SiliconFlow ASR.
"""
from flask import Blueprint, jsonify, request
from app.core.auth.jwt import get_current_user
from app.core.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,
})