-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.py
More file actions
315 lines (260 loc) · 9.38 KB
/
app.py
File metadata and controls
315 lines (260 loc) · 9.38 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from flask import Flask, render_template, request, redirect, url_for, jsonify
import pandas as pd
import numpy as np
import json
import os
import requests
from word2number import w2n
import re
from werkzeug import secure_filename
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_watson import SpeechToTextV1
from ibm_watson.natural_language_understanding_v1 import Features, EntitiesOptions
try:
import ibm_db
except:
pass
import ibm_boto3
import speech_recognition as sr
from googletrans import Translator
app = Flask(__name__)
app.config["UPLOAD_DIR"] = 'static/raw/'
apikey = ''
url = ''
assistantid = ''
sessionid = ''
# Initialize WKS Model Credentials
apikey = 'YOUR-NLU-API-KEY-HERE'
nlu_url = 'YOUR-URL-HERE'
wks_model_id = 'YOUR-WKS-MODEL-ID-HERE'
with open('credentials1.json', 'r') as credentialsFile:
credentials1 = json.loads(credentialsFile.read())
dsn_driver = "IBM DB2 ODBC DRIVER"
dsn_database = credentials1['db']
dsn_hostname = credentials1['host']
dsn_port = "50000"
dsn_uid = credentials1['username']
dsn_pwd = credentials1['password']
dsn = (
"DRIVER={{IBM DB2 ODBC DRIVER}};"
"DATABASE={0};"
"HOSTNAME={1};"
"PORT={2};"
"PROTOCOL=TCPIP;"
"UID={3};"
"PWD={4};").format(dsn_database, dsn_hostname, dsn_port, dsn_uid, dsn_pwd)
try:
conn = ibm_db.connect(dsn, "", "")
except:
pass
#########################
# Create Orders Table
#########################
table = ' CREATE TABLE ORDERS( \
ID int,NAME varchar(255), \
PHONE varchar(255), \
ORDERS varchar(255), \
ADDRESS varchar(255) ); '
try:
ibm_db.exec_immediate(conn, table)
except:
pass
# Constants for Speech-To-Text values
STT_API_KEY_ID = ""
STT_URL = ""
language_customization_id = ""
acoustic_customization_id = ""
''' Methods for IBM Watson Speech-To-Text '''
with open('credentials.json', 'r') as credentialsFile:
credentials = json.loads(credentialsFile.read())
STT_API_KEY_ID = credentials.get('apikey')
STT_URL = credentials.get('url')
authenticator = IAMAuthenticator(STT_API_KEY_ID)
speech_to_text = SpeechToTextV1(
authenticator=authenticator
)
speech_to_text.set_service_url(STT_URL)
''' Method to handle POST upload '''
@app.route('/uploader', methods=['GET', 'POST'])
def uploader():
try:
if request.method == 'POST':
l = request.form
lang = json.loads(l['SttLang'])
f = request.files["audio"]
filename_converted = f.filename.replace(
" ", "-").replace("'", "").lower()
# cmd = 'rm -r static/raw/*'
# os.system(cmd)
f.save(os.path.join(
app.config["UPLOAD_DIR"], secure_filename(filename_converted)))
print("\n##DEBUG##\n")
print('Input: '+ filename_converted, 'Language: '+lang["language"])
return transcribeAudio(filename_converted, lang["language"])
except Exception as e:
print("Unable {0}".format(e))
myResponse = {"message": str(e)}
except OSError as err:
print("Unable {0}".format(e))
myResponse = {"message": str(e)}
return json.dumps(myResponse, indent=2)
''' Method to handle Transcription '''
def transcribeAudio(filename, lang):
if lang == 'Hi':
r = sr.Recognizer()
with sr.AudioFile(app.config["UPLOAD_DIR"]+filename) as source:
audio = r.record(source)
x = r.recognize_google(audio, language = "hi-IN")
translator = Translator()
y = translator.translate(x)
print('Transcript: '+y.text)
respo = { "transcript": filename.split('.')[0]+': '+y.text,
"filepath" : app.config["UPLOAD_DIR"]+filename }
return extractEntities(respo)
elif lang == 'En':
with open(app.config["UPLOAD_DIR"]+filename, 'rb') as audioSource:
speech_recognition_results = speech_to_text.recognize(
audio=audioSource,
content_type='audio/wav',
timestamps=True,
model='en-US_BroadbandModel',
word_alternatives_threshold=0.9
).get_result()
transcript = ''
for chunks in speech_recognition_results['results']:
if 'alternatives' in chunks.keys():
alternatives = chunks['alternatives'][0]
if 'transcript' in alternatives:
transcript = transcript + alternatives['transcript']
text = transcript.replace('%HESITATION', '')
res = ""
words = text.split()
for word in words:
try:
res = res + str(w2n.word_to_num(word)) + " "
except:
res = res + word + " "
val = 0
text = ""
res = res.split()
count = 0
for r in res:
temp = re.findall(r'\d+', r)
if len(temp) == 0:
count = 0
if val != 0:
x = str(val) + " " + r + " "
text = text + x
val = 0
else:
text = text + r +" "
else:
count = count + 1
if count == 1:
if int(r) >= 100:
val = val + int(r)
elif int(r) < 100:
val = val + int(r)
if count == 2:
if val < 10 and int(r)<100:
val = (val*100) + int(r)
elif int(r) >= 100:
val = val*int(r)
elif int(r) < 100:
val = val + int(r)
print('Transcript: '+text)
respo = { "transcript": filename.split('.')[0]+': '+text,
"filepath" : app.config["UPLOAD_DIR"]+filename }
return extractEntities(respo)
def extractEntities(transcriptText):
authenticator = IAMAuthenticator(apikey)
natural_language_understanding = NaturalLanguageUnderstandingV1(
version = '2019-07-12',
authenticator = authenticator
)
natural_language_understanding.set_service_url(nlu_url)
response = natural_language_understanding.analyze(
text=transcriptText.get("transcript"),
features=Features(entities=EntitiesOptions(model=wks_model_id))).get_result()
name = ''
address = ''
phone = ''
orders = ''
for entity in response['entities']:
if entity['type'] == "ADDRESS":
address = entity['text']
if entity['type'] == "CUSTOMER_NAME":
name = entity['text']
if entity['type'] == "ORDER_ITEMS":
orders = entity['text']
if entity['type'] == "CUSTOMER_PHONE":
phone = entity['text']
response.update({'filepath': transcriptText.get('filepath')})
print('WKS ENTITIES DETECTED: ')
print('(NAME): '+name, ('(PHONE): ')+phone, ('(ORDERS): ')+orders, ('(ADDRESS): ')+address, sep="\n")
try:
ids = getIDs() + 1
except:
pass
a="\'"
n = a+name+a
o = a+orders+a
p = a+phone+a
add = a+address+a
insert = 'INSERT INTO RVB49192.ORDERS VALUES(%d, %s, %s, %s, %s)' %(ids, n, p, o, add)
try:
ibm_db.exec_immediate(conn, insert)
except:
pass
return jsonify(response)
@app.route('/deleteRecord/<int:ID>')
def deleteRecord(ID):
delete_statement= 'DELETE FROM RVB49192.ORDERS WHERE "ID" = {0};'.format(ID)
try:
ibm_db.exec_immediate(conn, delete_statement)
return {'flag': 'success'}
except:
return {'flag': 'failed'}
@app.route('/getDatabaseContents')
def getDatabaseContentsJson():
select_statement = 'SELECT * FROM RVB49192.ORDERS ORDER BY ID desc;'
try:
res = ibm_db.exec_immediate(conn, select_statement)
result = ibm_db.fetch_both(res)
resultDict = []
while(result):
returnDictBuffer = {'ID': result['ID'],
'NAME': result['NAME'],
'PHONE': result['PHONE'],
'ORDERS': result['ORDERS'],
'ADDRESS': result['ADDRESS']}
resultDict.append(returnDictBuffer)
result = ibm_db.fetch_both(res)
return jsonify(resultDict)
except:
resultDict = []
returnDictBuffer = {'ID': 0,
'NAME': 'db2 not connected!',
'PHONE': '',
'ORDERS': '',
'ADDRESS': ''}
resultDict.append(returnDictBuffer)
return jsonify(resultDict)
def getIDs():
select_statement = 'SELECT ID FROM RVB49192.ORDERS ORDER BY ID desc;'
try:
stmt = ibm_db.exec_immediate(conn, select_statement)
finalID = 0
result = ibm_db.fetch_both(stmt)
finalID = int(result['ID'])
return finalID
except:
return 0
@app.route('/')
def index():
return render_template('index.html')
port = os.getenv('VCAP_APP_PORT', '8080')
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run(debug=True, host='0.0.0.0', port=port)