The steps for this recipe are as follows:
- Import the libraries needed to send web requests and play sounds:
import requests
import json
from playsound import playsound
- Set the variables. The key and project URL can be found in the Getting ready section:
auth = 'EndpointKey '
question = 'how many calories in a cheese burger'
projectURL = ''
- Generate the data in the correct format:
headers = {
'Authorization': auth,
'Content-type': 'application/json',
}
data = '{ "question":"'+question+'"}'
- Send a request to the speech services at the project URL:
response = requests.post(projectURL, headers=headers, data=data)
json_data = json.loads(response.text)
- Extract the audio from the response and play it on the speakers:
for meta in json_data['answers'][0]['metadata']:
if meta['name'] == "file":
audiofile = 'audio/&apos...