Skip to content

Commit de2a9f3

Browse files
author
Zhengxi Tian
authored
Add files via upload
1 parent 1cab7de commit de2a9f3

File tree

3 files changed

+195
-15
lines changed

3 files changed

+195
-15
lines changed

Lambda/search-photos.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import uuid
6+
import time
67
from botocore.vendored import requests
78

89
ES_HOST = 'https://vpc-photos-rsjxyzqwdjlisyiem3w4iwldya.us-east-1.es.amazonaws.com'
@@ -21,6 +22,55 @@ def lambda_handler(event, context):
2122
lex = boto3.client('lex-runtime')
2223

2324
query = event["queryStringParameters"]["q"]
25+
26+
if query == "voiceSearch":
27+
transcribe = boto3.client('transcribe')
28+
job_name = time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()).replace(":", "-").replace(" ", "")
29+
job_uri = "https://s3.amazonaws.com/cc3-voices/test.mp3"
30+
transcribe.start_transcription_job(
31+
TranscriptionJobName=job_name,
32+
Media={'MediaFileUri': job_uri},
33+
MediaFormat='mp3',
34+
LanguageCode='en-US'
35+
)
36+
while True:
37+
status = transcribe.get_transcription_job(TranscriptionJobName=job_name)
38+
if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']:
39+
break
40+
print("Not ready yet...")
41+
time.sleep(5)
42+
print("Transcript URL: ", status)
43+
transcriptURL = status['TranscriptionJob']['Transcript']['TranscriptFileUri']
44+
trans_text = requests.get(transcriptURL).json()
45+
46+
print("Transcripts: ", trans_text)
47+
print(trans_text["results"]['transcripts'][0]['transcript'])
48+
49+
s3client = boto3.client('s3')
50+
response = s3client.delete_object(
51+
Bucket='cc3-voices',
52+
Key='test.mp3'
53+
)
54+
query = trans_text["results"]['transcripts'][0]['transcript']
55+
s3client.put_object(Body=query, Bucket='cc3-voices', Key='test.txt')
56+
return {
57+
'statusCode': 200,
58+
'headers': {
59+
"Access-Control-Allow-Origin": "*"
60+
},
61+
'body': "transcribe done"
62+
}
63+
64+
if query == "voiceResult":
65+
s3client = boto3.client('s3')
66+
data = s3client.get_object(Bucket='cc3-voices', Key='test.txt')
67+
query = data.get('Body').read().decode('utf-8')
68+
print("Voice query: ", query)
69+
s3client.delete_object(
70+
Bucket='cc3-voices',
71+
Key='test.txt'
72+
)
73+
2474

2575
lex_response = lex.post_text(
2676
botName='PhotoAlbum',

S3/apiGateway-js-sdk/album.html

Lines changed: 139 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,55 @@
8686
padding: 5px 15px;
8787
margin-left: 10px;
8888
}
89+
#record {
90+
background-color: red; /* Green */
91+
border-width: medium;
92+
border-color: black;
93+
color: white;
94+
padding: 20px;
95+
text-align: center;
96+
text-decoration: none;
97+
display: inline-block;
98+
font-size: 16px;
99+
margin: 4px 2px;
100+
cursor: pointer;
101+
max-width: 50%;
102+
max-height: 15%;
103+
border-radius: 50%;
104+
left: 100px;
105+
right: 100px;
106+
position: relative;
107+
}
108+
#stopRecord {
109+
background-color: green; /* Green */
110+
border-width: medium;
111+
border-color: black;
112+
color: white;
113+
padding: 20px;
114+
text-align: center;
115+
text-decoration: none;
116+
display: inline-block;
117+
font-size: 16px;
118+
margin: 4px 2px;
119+
cursor: pointer;
120+
max-width: 50%;
121+
max-height: 15%;
122+
border-radius: 50%;
123+
left: 100px;
124+
right: 100px;
125+
position: relative;
126+
}
127+
#recordedAudio {
128+
left: 100px;
129+
right: 100px;
130+
position: relative;
131+
}
89132
</style>
90133
</head>
91134

92135
<body>
93136
<div class="container">
94137
<form method="post" action="" enctype="multipart/form-data" id="myform">
95-
<div class='preview'>
96-
<img src="" id="img" width="100" height="100">
97-
</div>
98138
<div >
99139
<input type="file" id="file" name="file"/>
100140
<input type="button" class="button" value="Upload" id="but_upload">
@@ -107,14 +147,17 @@
107147
<div id="container1">
108148
<input type="text" id="searchin" placeholder="Search..."/>
109149
<button id="btngo" class="btn btn-primary" type="button">Search</button>
110-
</div>
111-
<!-- <div id="container2">
112-
<ul id="list" >
113-
<li href="http://www.google.it">Google</li>
114-
<li href="http://www.yahoo.it">Yahoo</li>
115-
<li href="http://www.amazon.it">Amazon</li>
116-
</ul>
117-
</div> -->
150+
</div>
151+
<div>
152+
<p>
153+
<button id=record>Voice search</button>
154+
<button id=stopRecord disabled>Stop</button>
155+
</p>
156+
<p>
157+
<audio id=recordedAudio></audio>
158+
159+
</p>
160+
</div>
118161
</div>
119162

120163
</body>
@@ -128,6 +171,7 @@
128171
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
129172

130173
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
174+
<script src="recorder.mp3.min.js"></script>
131175
<script type="text/javascript" src="lib/axios/dist/axios.standalone.js"></script>
132176
<script type="text/javascript" src="lib/CryptoJS/rollups/hmac-sha256.js"></script>
133177
<script type="text/javascript" src="lib/CryptoJS/rollups/sha256.js"></script>
@@ -141,6 +185,8 @@
141185
<script type="text/javascript" src="apigClient.js"></script>
142186

143187
<script type="text/javascript">
188+
/*** Connect with API Gateway ***/
189+
var apigClient = apigClientFactory.newClient();
144190

145191
function showImage(src, width, height, alt) {
146192
var img = document.createElement("img");
@@ -173,14 +219,92 @@
173219
// console.log(response.data)
174220
alert("Upload successful!!");
175221
})
176-
177-
178222
});
179223
});
180224

225+
var rec=Recorder({
226+
bitRate:32,
227+
sampleRate:24000
228+
});
181229

182-
/*** Connect with API Gateway ***/
183-
var apigClient = apigClientFactory.newClient();
230+
function sendData(data) {
231+
let config = {
232+
headers:{'Content-Type': data.type}
233+
};
234+
235+
url = 'https://tg0swa682e.execute-api.us-east-1.amazonaws.com/test1/upload/cc3-voices/test.mp3'
236+
axios.put(url,data,config).then(response=>{
237+
// console.log(response.data)
238+
// alert("Upload successful!!");
239+
query = "voiceSearch";
240+
params = {q: query};
241+
apigClient.searchGet(params, {}, {})
242+
.then(function(result){
243+
//This is where you would put a success callback
244+
console.log(result);
245+
}).catch(function(result){
246+
//This is where you would put an error callback
247+
console.log(result);
248+
});
249+
setTimeout(function(){
250+
params = {q: "voiceResult"};
251+
apigClient.searchGet(params, {}, {})
252+
.then(function(result){
253+
//This is where you would put a success callback
254+
console.log(result);
255+
// 这里写showImage的函数
256+
let img_list = result.data
257+
if(img_list === "No such photos.") {
258+
alert("No such photos!!")
259+
return
260+
}
261+
for (var i = 0; i < img_list.length; i++) {
262+
img_url = img_list[i];
263+
new_img = document.createElement('img');
264+
new_img.src = img_url;
265+
document.body.appendChild(new_img);
266+
}
267+
268+
}).catch(function(result){
269+
//This is where you would put an error callback
270+
console.log(result);
271+
alert("No such photos!!");
272+
});
273+
}, 120000);
274+
}).catch(err => {
275+
console.log(err);
276+
})
277+
}
278+
279+
record.onclick = e => {
280+
console.log('I was clicked')
281+
record.disabled = true;
282+
record.style.backgroundColor = "blue"
283+
stopRecord.disabled=false;
284+
285+
rec.open(function(){
286+
rec.start();
287+
},function(msg,isUserNotAllow){
288+
console.log((isUserNotAllow?"UserNotAllow,":"")+"can't record:"+msg);
289+
});
290+
}
291+
stopRecord.onclick = e => {
292+
console.log("I was clicked")
293+
record.disabled = false;
294+
stop.disabled=true;
295+
record.style.backgroundColor = "red"
296+
rec.stop(function(blob,duration){
297+
console.log(URL.createObjectURL(blob),"Duration:"+duration+"ms");
298+
console.log(blob);
299+
rec.close();
300+
var audio=document.createElement("audio");
301+
audio.controls=true;
302+
document.body.appendChild(audio);
303+
audio.src=URL.createObjectURL(blob);
304+
// audio.play();
305+
sendData(blob);
306+
});
307+
}
184308

185309

186310
// after clcik the search button, show the search result

S3/apiGateway-js-sdk/recorder.mp3.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)