Loginauth / tts.py
Gregniuki's picture
Update tts.py
4d9e392
raw
history blame
1.04 kB
# app/routes/tts.py
from fastapi import APIRouter, Depends
from auth import UserCreate # Import a model for the user response
from main import get_current_user
import onnxruntime
import numpy as np
def tts_synthesis(text, onnx_model_path):
# Load the ONNX model
session = onnxruntime.InferenceSession(onnx_model_path)
# Prepare input data
input_name = session.get_inputs()[0].name
input_data = np.array([text], dtype=np.str).reshape(1, 1)
# Run inference
output_name = session.get_outputs()[0].name
output = session.run([output_name], {input_name: input_data})
# Extract audio data
audio_data = output[0]
# Return the audio data
return audio_data
router = APIRouter()
@router.get("/synthesize", response_model=UserCreate)
def synthesize_text(text: str, current_user: str = Depends(get_current_user)):
# Use the get_current_user dependency to ensure authentication
audio_data = tts_synthesis(text, current_user)
return {"user": current_user, "audio_data": audio_data}