File size: 1,030 Bytes
4811ba6 eca9834 74fe0c7 fdd123c eca9834 72f77dc eca9834 f46aecc eca9834 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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}
|