Skip to content

Commit d751612

Browse files
committed
updating examples to work with both python and node.js
1 parent 8d1fc01 commit d751612

File tree

144 files changed

+71
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+71
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ test/resources/stt-auth.json
77
test/resources/tts-auth.json
88
doc/
99
*-auth.json
10+
.env

examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"start": "node server.js"
77
},
88
"dependencies": {
9+
"dotenv": "^2.0.0",
910
"express": "^4.13.3",
1011
"vcap_services": "^0.1.7",
1112
"watson-developer-cloud": "^1.2.0",

examples/readme.md

Lines changed: 13 additions & 4 deletions

examples/server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
var express = require('express'),
2020
app = express();
2121

22-
app.use(express.static(__dirname + '/public'));
23-
app.use(express.static(__dirname + '/../dist')); // normally these files would also go into public/ but this way the example always has the latest code
22+
// allows environment properties to be set in a file named .env
23+
require('dotenv').load();
24+
25+
app.use(express.static(__dirname + '/static'));
2426

2527
// token endpoints
2628
// **Warning**: these endpoints should be guarded with additional authentication & authorization for production use

examples/server.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os, json
2+
from flask import Flask
3+
from watson_developer_cloud import AuthorizationV1 as Authorization
4+
from watson_developer_cloud import SpeechToTextV1 as SpeechToText
5+
from watson_developer_cloud import TextToSpeechV1 as TextToSpeech
6+
from dotenv import load_dotenv
7+
8+
# Set the username and password below or in a .env file for local development
9+
load_dotenv('.env')
10+
11+
# 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'
14+
15+
# 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>'
18+
19+
# on bluemix, automatically pull credentials from environment
20+
if 'VCAP_SERVICES' in os.environ:
21+
stt = json.loads(os.environ['VCAP_SERVICES'])['speech_to_text'][0]
22+
STT_USERNAME = stt["credentials"]["username"]
23+
STT_PASSWORD = stt["credentials"]["password"]
24+
tts = json.loads(os.environ['VCAP_SERVICES'])['text_to_speech'][0]
25+
TTS_USERNAME = tts["credentials"]["username"]
26+
TTS_PASSWORD = tts["credentials"]["password"]
27+
28+
app = Flask(__name__, static_url_path='')
29+
30+
@app.route('/')
31+
def root():
32+
return app.send_static_file('index.html')
33+
34+
@app.route('/api/speech-to-text/token')
35+
def getSttToken():
36+
print(STT_USERNAME)
37+
authorization = Authorization(username=STT_USERNAME, password=STT_PASSWORD)
38+
return authorization.get_token(url=SpeechToText.default_url)
39+
40+
@app.route('/api/text-to-speech/token')
41+
def getTtsToken():
42+
authorization = Authorization(username=TTS_USERNAME, password=TTS_PASSWORD)
43+
return authorization.get_token(url=TextToSpeech.default_url)
44+
45+
app.run(debug=True)

0 commit comments

Comments
 (0)