MeloTTS icon indicating copy to clipboard operation
MeloTTS copied to clipboard

如何通过HTTP接口进行调用

Open taoshihan1991 opened this issue 4 months ago • 4 comments

在docker部署后,不能通过python调用,如何进行http接口调用呢?

taoshihan1991 avatar Feb 28 '24 16:02 taoshihan1991

you need to deploy http service by your own

Zengyi-Qin avatar Feb 29 '24 01:02 Zengyi-Qin

like this

api.py

from flask import Flask, Response, request
import io
from melo.api import TTS

app = Flask(__name__)


# Speed is adjustable
speed = 1.0
device = 'cuda:0' # or cuda:0



@app.before_request
def load_model():
    if not hasattr(app, 'model'):
        app.model = TTS(language='ZH', device=device)


@app.route('/audio', methods=['POST'])
def get_audio():
    bio = io.BytesIO()
    speaker_ids = app.model.hps.data.spk2id
    data = request.json
    text = data.get('text')
    print(speaker_ids, text)
    app.model.tts_to_file(text, speaker_ids['ZH'], bio, speed=speed)
    return bio.getvalue()


if __name__ == '__main__':
    app.run(host='***', port=***, debug=True)

lonngxiang avatar Feb 29 '24 03:02 lonngxiang

我是在本地windows docker部署的,直接python类库没法调用,想调用docker部署好的http服务端的接口

taoshihan1991 avatar Mar 01 '24 06:03 taoshihan1991

Thanks for the tip. I tried your code. It returned the follow error.

api.py trying to write to a file and _io.BytesIO does not contain file format information. Anything else I can try here, thanks.

melotts-1  |   File "/app/melo/flask_server.py", line 28, in get_audio
melotts-1  |     app.model.tts_to_file(text, speaker_ids['ZH'], bio, speed=speed)
melotts-1  |   File "/app/melo/api.py", line 135, in tts_to_file
melotts-1  |     soundfile.write(output_path, audio, self.hps.data.sampling_rate)
melotts-1  |   File "/usr/local/lib/python3.9/site-packages/soundfile.py", line 343, in write
melotts-1  |     with SoundFile(file, 'w', samplerate, channels,
melotts-1  |   File "/usr/local/lib/python3.9/site-packages/soundfile.py", line 656, in __init__
melotts-1  |     self._info = _create_info_struct(file, mode, samplerate, channels,
melotts-1  |   File "/usr/local/lib/python3.9/site-packages/soundfile.py", line 1466, in _create_info_struct
melotts-1  |     format = _get_format_from_filename(file, mode)
melotts-1  |   File "/usr/local/lib/python3.9/site-packages/soundfile.py", line 1507, in _get_format_from_filename
melotts-1  |     raise TypeError("No format specified and unable to get format from "
melotts-1  | TypeError: No format specified and unable to get format from file extension: <_io.BytesIO object at 0x7fe4646ebd60>

like this

api.py

from flask import Flask, Response, request
import io
from melo.api import TTS

app = Flask(__name__)


# Speed is adjustable
speed = 1.0
device = 'cuda:0' # or cuda:0



@app.before_request
def load_model():
    if not hasattr(app, 'model'):
        app.model = TTS(language='ZH', device=device)


@app.route('/audio', methods=['POST'])
def get_audio():
    bio = io.BytesIO()
    speaker_ids = app.model.hps.data.spk2id
    data = request.json
    text = data.get('text')
    print(speaker_ids, text)
    app.model.tts_to_file(text, speaker_ids['ZH'], bio, speed=speed)
    return bio.getvalue()


if __name__ == '__main__':
    app.run(host='***', port=***, debug=True)

ttkrpink avatar Mar 12 '24 15:03 ttkrpink