Spaces:
Running
Running
import os | |
from flask import jsonify, send_file, request | |
from main import * | |
#from main import import tts_model, device | |
def text_to_speech_func(text, output_path="output_tts.wav"): | |
if tts_model is None: | |
return "TTS model not initialized." | |
input_tokens = tts_model.tokenizer(text, return_tensors="pt", padding=True).to(device) | |
with torch.no_grad(): | |
audio_output = tts_model(input_tokens['input_ids']) | |
torchaudio.save(output_path, audio_output.cpu(), 16000) | |
return output_path | |
def tts_api(): | |
data = request.get_json() | |
text = data.get('text') | |
if not text: | |
return jsonify({"error": "Text is required"}), 400 | |
output_file = text_to_speech_func(text) | |
if output_file == "TTS model not initialized.": | |
return jsonify({"error": "TTS generation failed"}), 500 | |
return send_file(output_file, mimetype="audio/wav", as_attachment=True, download_name="output.wav") | |