简体   繁体   English

在bluemix上使用python调用服务

[英]Calling a service with python on bluemix

I'm using python on the IBM Bluemix platform. 我在IBM Bluemix平台上使用python。 How can I do a call to the text-to-speech Watson service? 如何调用文本到语音转换的Watson服务? I have the string inside my python code and I need to pass this text to be read. 我的python代码中有字符串,我需要传递这个文本才能被读取。

Assuming that you already have a Bluemix account and added text to speech Watson API to your Bluemix workspace, you have the credentials to access the API (restful). 假设您已经拥有一个Bluemix帐户并将语音Watson API添加到您的Bluemix工作区,您就拥有访问API的凭据(restful)。

If you were requesting using the CURL linux app, it would be something like this 如果您要求使用CURL linux应用程序,它将是这样的

curl -u "xxxxx729-b03f-4403-8adf-c5418ee4ea05":"xxxxxiWtmVoG" "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=Hello+World" -H "accept: audio/flac" > sound.flac

Using Python, it can be 使用Python,它可以

import requests

headers = {'accept': 'audio/flac'}

r = requests.get('https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=Hello+World', auth=('xxxxx729-b03f-4403-8adf-c5418ee4ea05', 'xxxxxiWtmVoG'), headers=headers)

with open('/home/leo/sound.flac', 'wb') as fd:
    for chunk in r.iter_content(1024):
        fd.write(chunk)

See http://docs.python-requests.org/en/master/user/quickstart/ for details on the requests package. 有关请求包的详细信息,请参阅http://docs.python-requests.org/en/master/user/quickstart/

See https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/text-to-speech/api/v1/ for text2Speech documentation. 有关text2Speech文档,请参阅https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/text-to-speech/api/v1/

最好的方法是使用Watson Developer Cloud Python SDK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM