Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could you give a python example? #5

Open
Chr0my1702 opened this issue Dec 26, 2021 · 2 comments
Open

Could you give a python example? #5

Chr0my1702 opened this issue Dec 26, 2021 · 2 comments

Comments

@Chr0my1702
Copy link

Could you give a python example as I would like to know how to do it. I'm not fully understanding the requests.

@tgwaste
Copy link

tgwaste commented Feb 3, 2022

Here is a python example:

Example Usage: ./playht-example.py -t 'hello world' -o test.wav

There is probably a better way to download than to issue a system call to curl but I wrote this quick and havnt looked into that yet.

#!/usr/bin/env python3

import json, optparse, os, requests, sys, time

api = 'https://play.ht/api/v1/convert'
status = 'https://play.ht/api/v1/articleStatus'
userid = 'YOUR_USER_ID'
secret = 'YOUR_SECRET'

parser = optparse.OptionParser(usage='usage: %prog [options]')
parser.add_option('-n', '--narration', dest='narration', type='string', default='customerservice', help='narration style')
parser.add_option('-o', '--outputfile', dest='outputfile', type='string', default='', help='output wav file')
parser.add_option('-s', '--silence', dest='silence', action='store_true', default=False, help='trim silence from end')
parser.add_option('-t', '--text', dest='text', type='string', default='', help='text to say')
parser.add_option('-v', '--voice', dest='voice', type='string', default='en-US-JennyNeural', help='voice to use')
(options, args) = parser.parse_args()

if len(sys.argv[1:]) == 0:
  parser.print_help()
  sys.exit(1)

headers = {
  'Authorization' : secret,
  'X-User-ID' : userid,
  'Content-Type' : 'application/json'
}

payload = {
  'voice': options.voice, # en-US-JennyNeural en-US-AriaNeural
  'content': [options.text],
  'narrationStyle': options.narration, # newscast-formal, newscast-casual, customerservice, chat, cheerful, empathetic
  'trimSilence': options.silence
}

result = requests.post(api, data=json.dumps(payload), headers=headers)

if 'transcriptionId' not in result.json():
  print('did not receive a transcriptionId')
  sys.exit(1)

tid = result.json()['transcriptionId']

print(json.dumps(result.json(), sort_keys = False, indent = 4))

tries = 0

while True:
  result = requests.get(status + '?transcriptionId=' + tid, headers=headers)

  if 'audioUrl' not in result.json():
    tries += 1
    if tries >= 5:
      print('max attempts reached, aborting')
      sys.exit(1)
    time.sleep(2)
    continue

  print(json.dumps(result.json(), sort_keys = False, indent = 4))

  audiourl = result.json()['audioUrl']

  curl = 'curl -s --output "%s" "%s"' % (options.outputfile, audiourl)

  os.system(curl)

  break

@Josode
Copy link

Josode commented Jun 3, 2022

Could you ELI5 how to use this to get python to output speech? I'm not understanding it fully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants