File size: 3,657 Bytes
8d4c60f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os

import logging
import uuid

from flask import Flask, request, send_file, jsonify
from werkzeug.utils import secure_filename
from flask_apscheduler import APScheduler

from utils import clean_folder, merge_model

app = Flask(__name__)
app.config.from_pyfile("config.py")

scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()

logging.getLogger('numba').setLevel(logging.WARNING)

voice_obj, voice_speakers = merge_model(app.config["MODEL_LIST"])

if not os.path.exists(app.config['UPLOAD_FOLDER']):
    try:
        os.mkdir(app.config['UPLOAD_FOLDER'])
    except:
        pass


@app.route('/')
@app.route('/voice/')
def index():
    return "usage:https://github.com/Artrajz/MoeGoe-Simple-API#readme"


@app.route('/voice/speakers', methods=["GET", "POST"])
def voice_speakers_api():
    speakers_list = voice_speakers
    return jsonify(speakers_list)


@app.route('/voice', methods=["GET", "POST"])
def voice_api():
    if request.method == "GET":
        text = request.args.get("text")
        speaker_id = int(request.args.get("id", 0))
        format = request.args.get("format", "wav")
        lang = request.args.get("lang", "mix")
        speed = float(request.args.get("speed", 1.0))
    elif request.method == "POST":
        json_data = request.json
        text = json_data["text"]
        speaker_id = int(json_data["id"])
        format = json_data["format"]
        lang = json_data["lang"]
        speed = float(json_data["speed"])

    if lang.upper() == "ZH":
        text = f"[ZH]{text}[ZH]"
    elif lang.upper() == "JA":
        text = f"[JA]{text}[JA]"

    real_id = voice_obj[speaker_id][0]
    real_obj = voice_obj[speaker_id][1]

    output, file_type, fname = real_obj.generate(text, real_id, format, speed)

    return send_file(path_or_file=output, mimetype=file_type, download_name=fname)


@app.route('/voice/conversion', methods=["GET", "POST"])
def voice_conversion_api():
    if request.method == "GET":
        return jsonify("method should be POST")
    if request.method == "POST":
        # json_data = request.json
        voice = request.files['upload']
        original_id = int(request.form["original_id"])
        target_id = int(request.form["target_id"])

        form = {}

        format = voice.filename.split(".")[1]

        fname = secure_filename(str(uuid.uuid1()) + "." + voice.filename.split(".")[1])
        voice.save(os.path.join(app.config['UPLOAD_FOLDER'], fname))

        real_original_id = int(voice_obj[original_id][0])
        real_target_id = int(voice_obj[target_id][0])
        real_obj = voice_obj[original_id][1]
        real_target_obj = voice_obj[target_id][1]

        if voice_obj[original_id][2] != voice_obj[target_id][2]:
            form["status"] = "error"
            form["message"] = "speaker IDs are in diffrent Model!"
            return form

        output = real_obj.voice_conversion(os.path.join(app.config['UPLOAD_FOLDER'], fname),
                                           real_original_id, real_target_id, format)
        file_type = f"audio/{format}"

        return send_file(path_or_file=output, mimetype=file_type, download_name=fname)
        # return output


# 定时清理临时文件,每小时清一次
@scheduler.task('interval', id='随便写', seconds=3600, misfire_grace_time=900)
def clean_task():
    clean_folder(app.config["UPLOAD_FOLDER"])
    clean_folder(app.config["SILK_OUT_PATH"])


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=app.config["PORT"])  # 如果对外开放用这个,docker部署也用这个
    # app.run(host='127.0.0.1', port=app.config["PORT"], debug=True)  # 本地运行、调试