Skip to content

Commit 88a31c6

Browse files
committed
fix: Fixed python server to work on OSX and updated setup documentation to mention all required dependencies
feat: Added support for IAM API tokens to python server
1 parent 0e4f98e commit 88a31c6

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

examples/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Setup - Node.js
2727
Setup - Python
2828
--------------
2929

30-
1. `cd` into the `examples/` directory and run `pip install watson_developer_cloud flask` (or `easy_install...`) to install python dependencies
30+
1. `cd` into the `examples/` directory and run `pip install watson_developer_cloud flask python-dotenv pyopenssl` (or `easy_install...`) to install python dependencies.
3131
2. run `bower install` to install client-side dependencies
3232
3. edit `server.py` to include your service credentials (or create a `.env` file)
3333
4. run `python server.py`

examples/server.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os, json
22
from flask import Flask
33
from watson_developer_cloud import AuthorizationV1 as Authorization
4+
from watson_developer_cloud import IAMTokenManager
45
from watson_developer_cloud import SpeechToTextV1 as SpeechToText
56
from watson_developer_cloud import TextToSpeechV1 as TextToSpeech
67
from dotenv import load_dotenv
@@ -9,21 +10,29 @@
910
load_dotenv('.env')
1011

1112
# Text to Speech
12-
TTS_USERNAME = os.environ.get('TTS_USERNAME'); # '<Text to Speech username>'
13-
TTS_PASSWORD = os.environ.get('TTS_PASSWORD'); # '<Text to Speech password'
13+
TTS_USERNAME = os.environ.get('TEXT_TO_SPEECH_USERNAME') # '<Text to Speech username>'
14+
TTS_PASSWORD = os.environ.get('TEXT_TO_SPEECH_PASSWORD') # '<Text to Speech password'
15+
TTS_APIKEY = os.environ.get('TEXT_TO_SPEECH_IAM_APIKEY') # '<Text to Speech IAM API key'
16+
TTS_URL = os.environ.get('TEXT_TO_SPEECH_URL') # '<Text to Speech URL>'
1417

1518
# Speech to Text
16-
STT_USERNAME = os.environ.get('STT_USERNAME'); # '<Speech to Text username>'
17-
STT_PASSWORD = os.environ.get('STT_PASSWORD'); # '<Speech to Text password>'
19+
STT_USERNAME = os.environ.get('SPEECH_TO_TEXT_USERNAME') # '<Speech to Text username>'
20+
STT_PASSWORD = os.environ.get('SPEECH_TO_TEXT_PASSWORD') # '<Speech to Text password>'
21+
STT_APIKEY = os.environ.get('SPEECH_TO_TEXT_IAM_APIKEY') # '<Speech to Text IAM API key>'
22+
STT_URL = os.environ.get('SPEECH_TO_TEXT_URL') # '<Speech to Text URL>'
1823

1924
# on bluemix, automatically pull credentials from environment
2025
if 'VCAP_SERVICES' in os.environ:
2126
stt = json.loads(os.environ['VCAP_SERVICES'])['speech_to_text'][0]
2227
STT_USERNAME = stt["credentials"]["username"]
2328
STT_PASSWORD = stt["credentials"]["password"]
29+
STT_APIKEY = stt["credentials"]["apikey"]
30+
STT_URL = stt["credentials"]["url"] if stt["credentials"]["url"] else SpeechToText.default_url
2431
tts = json.loads(os.environ['VCAP_SERVICES'])['text_to_speech'][0]
2532
TTS_USERNAME = tts["credentials"]["username"]
2633
TTS_PASSWORD = tts["credentials"]["password"]
34+
TTS_APIKEY = tts["credentials"]["apikey"]
35+
TTS_URL = tts["credentials"]["url"] if tts["credentials"]["url"] else TextToSpeech.default_url
2736

2837
app = Flask(__name__, static_url_path='')
2938

@@ -33,13 +42,24 @@ def root():
3342

3443
@app.route('/api/speech-to-text/token')
3544
def getSttToken():
36-
print(STT_USERNAME)
37-
authorization = Authorization(username=STT_USERNAME, password=STT_PASSWORD)
38-
return authorization.get_token(url=SpeechToText.default_url)
45+
if (STT_APIKEY):
46+
iamTokenManager = IAMTokenManager(iam_apikey=STT_APIKEY)
47+
token = iamTokenManager.get_token()
48+
else:
49+
authorization = Authorization(username=STT_USERNAME, password=STT_PASSWORD)
50+
token = authorization.get_token(url=STT_URL)
51+
return token
3952

4053
@app.route('/api/text-to-speech/token')
4154
def getTtsToken():
42-
authorization = Authorization(username=TTS_USERNAME, password=TTS_PASSWORD)
43-
return authorization.get_token(url=TextToSpeech.default_url)
55+
if (TTS_APIKEY):
56+
iamTokenManager = IAMTokenManager(iam_apikey=TTS_APIKEY)
57+
token = iamTokenManager.get_token()
58+
else:
59+
authorization = Authorization(username=TTS_USERNAME, password=TTS_PASSWORD)
60+
token = authorization.get_token(url=TTS_URL)
61+
return token
4462

45-
app.run(debug=True)
63+
# NOTE: ssl_context='adhoc' fixes response encoding (Flask 400 BAD_REQUEST) errors over SSL
64+
if __name__ == '__main__':
65+
app.run(ssl_context='adhoc', debug=True)

0 commit comments

Comments
 (0)