Untitled

 avatar
unknown
plain_text
a year ago
2.2 kB
21
Indexable
import os
from flask import Flask, render_template, request, redirect, url_for, jsonify
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('/generate_audio_api', methods=['POST'])
def generate_audio():
    data = request.json
    text = data['text']
    speed = float(data['speed'])
    device = data['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 jsonify({"url": f"{next_index}.wav"}), 201

@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