FastAPI-Backend-Models / routes /stt_routes.py
malek-messaoudii
Uodate stt tts
27ee35f
raw
history blame
704 Bytes
from fastapi import APIRouter, UploadFile, File
from services.stt_service import speech_to_text
from models.stt import STTResponse
import os
import uuid
router = APIRouter(prefix="/stt", tags=["Speech To Text"])
@router.post("/", response_model=STTResponse)
async def convert_stt(file: UploadFile = File(...)):
# dossier temporaire
os.makedirs("audio/temp", exist_ok=True)
# nom temporaire unique
temp_name = f"audio/temp/{uuid.uuid4()}_{file.filename}"
# save file
with open(temp_name, "wb") as f:
f.write(await file.read())
# STT conversion
text = speech_to_text(temp_name)
# cleanup
os.remove(temp_name)
return STTResponse(text=text)