Commit b65bd56f authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+7 −0
Changes for Introduction to Cognitive Computing with IBM Watson Services - Working Files/Chapter 1/No Work Files.rtf: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 No work files for this lesson...}
 No newline at end of file
+7 −0
Changes for Introduction to Cognitive Computing with IBM Watson Services - Working Files/Chapter 2/No Work Files.rtf: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 No work files for this lesson...}
 No newline at end of file
+58 −0
Changes for Introduction to Cognitive Computing with IBM Watson Services - Working Files/Chapter 3/0301_speech_to_text.py: 58 added lines, 0 removed lines.
Original line number Diff line number Diff line
'''
    Example Python script for calling the IBM Watson Speech to Text API

    You will need to provide your own IBM Watson service credentials.
    
    This script is provided as-is, with no warranties explicit or implied.
    You may use this script for education or professional purposes, and no
    attribution is required.

    Matt Coatney
    O'Reilly Media
    Copyright (c) 2017
'''

import os, subprocess, requests, codecs, json, sys
from requests.auth import HTTPBasicAuth

def speech_to_text(audiofile, username, password):
    f = open(audiofile, 'rb')
    data = f.read()
    f.close()

    auth = HTTPBasicAuth(username, password)
    
    res = requests.post(url='https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true',
                        data=data,
                        auth=auth,
                        headers={'Content-Type': 'audio/ogg; codecs=opus'})
    
    return res.json()
        
if len(sys.argv) < 4:
    print 'Usage: python ' + sys.argv[0] + ' path_to_file api_username api_password'

path = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]

# Get audio track and convert to compressed OGG format - requires ffmpeg to be installed
command = 'ffmpeg -i "' + path + '" -y -loglevel error -ab 160k -ac 2 -ar 16000 -vn "' + path + '.ogg"'
subprocess.call(command, shell=True)        
  
# Call API
result = speech_to_text(path + '.ogg', username, password)

text = ''

for piece in result['results']:
    text += piece['alternatives'][0]['transcript'].replace('%HESITATION', '')
    
# Write results
f = codecs.open(path + '.s2t.json', mode='w', encoding='utf8')
f.write(json.dumps(result, indent=4, separators=(',', ': ')))
f.close()

f = codecs.open(path + '.txt', mode='w', encoding='utf8')
f.write(text)
f.close()
 No newline at end of file
+52 −0
Changes for Introduction to Cognitive Computing with IBM Watson Services - Working Files/Chapter 3/0303_text_to_speech.py: 52 added lines, 0 removed lines.
Original line number Diff line number Diff line
'''
    Example Python script for calling the IBM Watson Text to Speech API

    You will need to provide your own IBM Watson service credentials.
    
    This script is provided as-is, with no warranties explicit or implied.
    You may use this script for education or professional purposes, and no
    attribution is required.

    Matt Coatney
    O'Reilly Media
    Copyright (c) 2017
'''

import json, requests, codecs, sys, time
from requests.auth import HTTPBasicAuth

def text_to_speech(text, username, password):
    time.sleep(1) # Throttle multiple calls
    
    url_base = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_MichaelVoice'
    data = json.dumps( { 'text': text.encode('UTF-8') } )
    
    auth = HTTPBasicAuth(username, password)
    
    headers = {
      'Content-Type': 'application/json;charset=utf-8',
      'Accept': 'audio/wav'
    }
    
    res = requests.post(url_base, data=data, auth=auth, headers=headers)
    return res.content

if len(sys.argv) < 4:
    print 'Usage: python ' + sys.argv[0] + ' path_to_file api_username api_password'

path = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
    
# Get text from file
f = codecs.open(path, mode='r', encoding='utf8')
text = f.read()
f.close()
    
# Call API
result = text_to_speech(text, username, password)

# Write results
f = open(path + '.t2s.wav', 'wb')
f.write(result)
f.close()
 No newline at end of file
+1.04 MiB

File added.

No diff preview for this file type.

Loading