myshell-ai / MeloTTS

High-quality multi-lingual text-to-speech library by MyShell.ai. Support English, Spanish, French, Chinese, Japanese and Korean.
MIT License
4.75k stars 615 forks source link

如何通过HTTP接口进行调用 #22

Open taoshihan1991 opened 8 months ago

taoshihan1991 commented 8 months ago

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

Zengyi-Qin commented 8 months ago

you need to deploy http service by your own

lonngxiang commented 8 months ago

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)
taoshihan1991 commented 8 months ago

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

ttkrpink commented 8 months ago

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)
dhc45010 commented 3 months ago

bio = model.tts_to_file(text, speaker_id=speaker_ids['ZH'], speed=speed)

litterGuy commented 2 months ago

set format='wav'

app.model.tts_to_file(text, speaker_ids['ZH'], bio, speed=speed, format='wav')