All checks were successful
Deploy FluentGerman.ai / deploy (push) Successful in 53s
59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
"""FluentGerman.ai — Admin voice-to-instruction & voice API router."""
|
|
|
|
from fastapi import APIRouter, Depends, UploadFile, File
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.auth import require_admin, get_current_user
|
|
from app.config import get_settings
|
|
from app.database import get_db
|
|
from app.models import User
|
|
from app.schemas import VoiceConfigOut, VoiceInstructionRequest
|
|
from app.services.llm_service import summarize_instruction
|
|
from app.services.voice_service import synthesize, transcribe
|
|
from fastapi.responses import Response
|
|
|
|
router = APIRouter(prefix="/api/voice", tags=["voice"])
|
|
|
|
|
|
@router.get("/config", response_model=VoiceConfigOut)
|
|
async def voice_config(user: User = Depends(get_current_user)):
|
|
"""Return current voice mode so frontend knows whether to use browser or API."""
|
|
settings = get_settings()
|
|
# API STT (Whisper) works with OpenAI-compatible providers
|
|
# Check if we have a dedicated voice key OR a generic LLM key for OpenAI
|
|
has_key = bool(settings.openai_api_key or (settings.llm_api_key and settings.llm_provider == "openai"))
|
|
|
|
api_available = bool(settings.voice_mode == "api" and has_key)
|
|
return VoiceConfigOut(
|
|
voice_mode=settings.voice_mode,
|
|
voice_api_available=api_available,
|
|
)
|
|
|
|
|
|
@router.post("/transcribe")
|
|
async def transcribe_audio(
|
|
audio: UploadFile = File(...),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
"""Transcribe uploaded audio to text (API mode only)."""
|
|
audio_bytes = await audio.read()
|
|
text = await transcribe(audio_bytes, filename=audio.filename or "audio.webm")
|
|
return {"text": text}
|
|
|
|
|
|
@router.post("/synthesize")
|
|
async def synthesize_text(
|
|
text: str,
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
"""Convert text to speech audio (API mode only)."""
|
|
audio_bytes = await synthesize(text)
|
|
return Response(content=audio_bytes, media_type="audio/mpeg")
|
|
|
|
|
|
@router.post("/generate-instruction", dependencies=[Depends(require_admin)])
|
|
async def generate_instruction(body: VoiceInstructionRequest):
|
|
"""Admin: takes raw transcript and returns a structured instruction via LLM."""
|
|
structured = await summarize_instruction(body.raw_text)
|
|
return {"instruction": structured}
|