Skip to content

Commit

Permalink
fix: Fixed python server to work on OSX and updated setup documentati…
Browse files Browse the repository at this point in the history
…on to mention all required dependencies

feat: Added support for IAM API tokens to python server
  • Loading branch information
w0o committed Mar 29, 2019
1 parent 0e4f98e commit 88a31c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Setup - Node.js
Setup - Python
--------------

1. `cd` into the `examples/` directory and run `pip install watson_developer_cloud flask` (or `easy_install...`) to install python dependencies
1. `cd` into the `examples/` directory and run `pip install watson_developer_cloud flask python-dotenv pyopenssl` (or `easy_install...`) to install python dependencies.
2. run `bower install` to install client-side dependencies
3. edit `server.py` to include your service credentials (or create a `.env` file)
4. run `python server.py`
Expand Down
40 changes: 30 additions & 10 deletions examples/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os, json
from flask import Flask
from watson_developer_cloud import AuthorizationV1 as Authorization
from watson_developer_cloud import IAMTokenManager
from watson_developer_cloud import SpeechToTextV1 as SpeechToText
from watson_developer_cloud import TextToSpeechV1 as TextToSpeech
from dotenv import load_dotenv
Expand All @@ -9,21 +10,29 @@
load_dotenv('.env')

# Text to Speech
TTS_USERNAME = os.environ.get('TTS_USERNAME'); # '<Text to Speech username>'
TTS_PASSWORD = os.environ.get('TTS_PASSWORD'); # '<Text to Speech password'
TTS_USERNAME = os.environ.get('TEXT_TO_SPEECH_USERNAME') # '<Text to Speech username>'
TTS_PASSWORD = os.environ.get('TEXT_TO_SPEECH_PASSWORD') # '<Text to Speech password'
TTS_APIKEY = os.environ.get('TEXT_TO_SPEECH_IAM_APIKEY') # '<Text to Speech IAM API key'
TTS_URL = os.environ.get('TEXT_TO_SPEECH_URL') # '<Text to Speech URL>'

# Speech to Text
STT_USERNAME = os.environ.get('STT_USERNAME'); # '<Speech to Text username>'
STT_PASSWORD = os.environ.get('STT_PASSWORD'); # '<Speech to Text password>'
STT_USERNAME = os.environ.get('SPEECH_TO_TEXT_USERNAME') # '<Speech to Text username>'
STT_PASSWORD = os.environ.get('SPEECH_TO_TEXT_PASSWORD') # '<Speech to Text password>'
STT_APIKEY = os.environ.get('SPEECH_TO_TEXT_IAM_APIKEY') # '<Speech to Text IAM API key>'
STT_URL = os.environ.get('SPEECH_TO_TEXT_URL') # '<Speech to Text URL>'

# on bluemix, automatically pull credentials from environment
if 'VCAP_SERVICES' in os.environ:
stt = json.loads(os.environ['VCAP_SERVICES'])['speech_to_text'][0]
STT_USERNAME = stt["credentials"]["username"]
STT_PASSWORD = stt["credentials"]["password"]
STT_APIKEY = stt["credentials"]["apikey"]
STT_URL = stt["credentials"]["url"] if stt["credentials"]["url"] else SpeechToText.default_url
tts = json.loads(os.environ['VCAP_SERVICES'])['text_to_speech'][0]
TTS_USERNAME = tts["credentials"]["username"]
TTS_PASSWORD = tts["credentials"]["password"]
TTS_APIKEY = tts["credentials"]["apikey"]
TTS_URL = tts["credentials"]["url"] if tts["credentials"]["url"] else TextToSpeech.default_url

app = Flask(__name__, static_url_path='')

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

@app.route('/api/speech-to-text/token')
def getSttToken():
print(STT_USERNAME)
authorization = Authorization(username=STT_USERNAME, password=STT_PASSWORD)
return authorization.get_token(url=SpeechToText.default_url)
if (STT_APIKEY):
iamTokenManager = IAMTokenManager(iam_apikey=STT_APIKEY)
token = iamTokenManager.get_token()
else:
authorization = Authorization(username=STT_USERNAME, password=STT_PASSWORD)
token = authorization.get_token(url=STT_URL)
return token

@app.route('/api/text-to-speech/token')
def getTtsToken():
authorization = Authorization(username=TTS_USERNAME, password=TTS_PASSWORD)
return authorization.get_token(url=TextToSpeech.default_url)
if (TTS_APIKEY):
iamTokenManager = IAMTokenManager(iam_apikey=TTS_APIKEY)
token = iamTokenManager.get_token()
else:
authorization = Authorization(username=TTS_USERNAME, password=TTS_PASSWORD)
token = authorization.get_token(url=TTS_URL)
return token

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

0 comments on commit 88a31c6

Please sign in to comment.