-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatson-agi.py
64 lines (53 loc) · 1.6 KB
/
watson-agi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
# Overall import
import sys,os
import requests
import json
## Watson Parameters
# Credentials
username = "XXXXX"
password = "XXXXX"
# Model language
model_name = "es-ES_NarrowbandModel"
# API Url Endpoint
url = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true&model=" + model_name
# Timeout API
timeout = 5
## Audio Parameters
path_audio = "/var/lib/asterisk/sounds"
ext_audio = "wav"
## Init VariablesA
# Default event
event = "SOPORTE"
# Obtain audio file
data = open(path_audio + "/" + sys.argv[1] + "." + ext_audio)
# Send Data
def send(data):
sys.stdout.write(str(data))
sys.stdout.flush()
# Call Watson API
def call_watson(url, data, username, password, timeout):
try:
headers = {'content-type': 'audio/' + ext_audio}
r = requests.post(url=url,auth=(username, password),data=data,headers=headers,timeout=timeout)
except (requests.exceptions.Timeout, requests.exceptions.TooManyRedirects, requests.exceptions.RequestException) as e:
# Handler Exception
return "ERROR"
# Return Error
return json.dumps(r.json())
# Call API Watson
res = call_watson(url, data, username, password, timeout)
if res != "ERROR":
a = json.loads(res)
# Return transcript
text = a["results"][0]["alternatives"][0]["transcript"]
msg = text.encode('ascii', 'ignore').decode('ascii')
## AQUI EMPIEZA NLP
## AQUI TERMINA NLP
# Send Event (VENTAS|FACTURACION|SOPORTE
event = "XXXX"
else:
event = "ERROR"
## Asterisk AGI
# Send Event (VENTAS|FACTURACION|SOPORTE|ERROR)
send("SET VARIABLE SPEECH_TO_TEXT " + event)