Untitled
unknown
plain_text
a year ago
1.5 kB
7
Indexable
import os
from flask import Flask, render_template, request, redirect, url_for
from melo.api import TTS
app = Flask(__name__)
# Đường dẫn tới thư mục static chứa file audio
static_folder = os.path.join(os.path.dirname(__file__), 'static')
@app.route('/')
def index():
# Liệt kê tất cả các file audio đã tạo
audio_files = sorted([f for f in os.listdir(static_folder) if f.endswith('.wav')])
return render_template('index.html', audio_files=audio_files)
@app.route('/generate_audio', methods=['POST'])
def generate_audio():
text = request.form['text']
speed = float(request.form['speed'])
device = request.form['device']
model = TTS(language='JP', device=device)
speaker_ids = model.hps.data.spk2id
# Tìm số tiếp theo để đặt tên file output
next_index = 1
while True:
output_path = os.path.join(static_folder, f'{next_index}.wav')
if not os.path.exists(output_path):
break
next_index += 1
model.tts_to_file(text, speaker_ids['JP'], output_path, speed=speed)
return redirect(url_for('index'))
@app.route('/delete_audio/<filename>', methods=['POST'])
def delete_audio(filename):
file_path = os.path.join(static_folder, filename)
if os.path.exists(file_path):
os.remove(file_path)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host="10.81.18.200")Editor is loading...
Leave a Comment