This time, it is a record of doing what I did at my own AWS hackathon half a year ago again. Last time, I was listening to what I didn't understand because there was an expert nearby, but this time I remembered everything from registering the free frame to converting text to voice with Polly, so it settled in my memory. Ec2's Linux is running uselessly, but of course it is still within the free tier. By the way, the one that consumes the most is 30 GB of Amazon Elastic Block Storage, which is 12.5%.
・ Establish a development style ・ Last code ・ Slight improvement ・ Listen to the sound
Many of the registrations are kind, so I think it can be created by imitating them. 【reference】 Beginners try AWS Summary of initial settings that should be done quickly after acquiring an AWS account So, I was able to set up an EC2 server in Ohio safely. As a development style, I used to create a program with vi at first, but as I got used to it, I created a program with Jupyter Notebook on my PC and sent it with Teraterm. This is definitely easier. In addition, Billing and security are above, but most important. Initially, I used the following command. However, as I get used to each page and service of AWS a little, I can do file operations on S3 from the CLI, but I have come to think that it is faster to play with files directly on S3. List of commands for operating S3 with AWS CLI
I referred to the reference because I need a key to use S3. The screen is a little different, but you can see what you are doing. 【reference】 Get access key and secret key to operate s3 of aws (IAM)
After installing various Python Lib on ubuntu instance of EC2, when transferring the program with Teraterm, [user name is ubuntu](https://aws.amazon.com/jp/premiumsupport/knowledge-center/ec2- It was password-login /). Also, host will copy and enter the address IPv4 public IP address. By the way, Teraterm is connected all day without disconnection. However, if the location of the instance is Ohio but it has changed to Tokyo, the instance is displayed as 0 and I am in a hurry at first. When transferring things from your own PC or transferring to your own PC with Teraterm, use the command SSHSCR. So, the following previous code worked. This code uses boto3 to convert the text shown in Text into Joanna's voice and save it in speech.mp3.
import boto3
polly_client = boto3.Session(
aws_access_key_id=,
aws_secret_access_key=,
region_name='us-west-2').client('polly')
response = polly_client.synthesize_speech(VoiceId='Joanna',
OutputFormat='mp3',
Text = 'This is a sample text to be synthesized.')
file = open('speech.mp3', 'wb')
file.write(response['AudioStream'].read())
file.close()
Next, the code to transfer this to the bucket created in S3 in advance is as follows.
# -*- coding: utf-8 -*-
import boto3
s3 = boto3.resource('s3') #Get S3 object
bucket = s3.Bucket('s3 bucket-name')
bucket.upload_file('Specify ec2 mp3 file', 'dir specification of bucket of s3')
But
s3_client = boto3.client('s3')
# Upload the file to S3
s3_client.upload_file('Specify ec2 mp3 file', 's3 bucket-name', 'mp3 filename in s3')
But it works.
However, in this case, the files in S3 are private in both cases (such security settings are the default). In this case, you cannot refer to (play) from the outside unless you publish each file on the S3 site. If you do it as a reference, it seems that you can set ACL = Public while sending it. However, this time I couldn't do it this way, but as a result of various trials (** I believed that I could do it ), I was able to achieve it with the following code. 【reference】 [How to upload and publish files to S3 using boto3? ](Https://www.it-swarm.dev/ja/python/boto3%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6% E3% 83% 95% E3% 82% A1% E3% 82% A4% E3% 83% AB% E3% 82% 92s3% E3% 81% AB% E3% 82% A2% E3% 83% 83% E3% 83% 97% E3% 83% AD% E3% 83% BC% E3% 83% 89% E3% 81% 97% E3% 81% A6% E5% 85% AC% E9% 96% 8B% E3% 81% 99% E3% 82% 8B% E6% 96% B9% E6% B3% 95% E3% 81% AF% EF% BC% 9F / 830216134 /) In the code below, there are two improvements compared to the above. ① session = Session (profile_name = "***") eliminates the need to write the key directly (2) Since it is published (referenced) as it is transferred by bucket.upload_file ('ec2 file name','s3 file name', ExtraArgs = {'ACL':'public-read'}), it is continuous. Audio can be published continuously
import boto3
from boto3 import Session
session = Session(profile_name="*****")
polly = session.client("polly")
response = polly.synthesize_speech(Text="Hello, Tokyo and Yokohama are also a little cloudy. The voice is Mizuki", OutputFormat="mp3", VoiceId="Mizuki")
file = open('Saved file name on ec2', 'wb')
file.write(response['AudioStream'].read())
file.close()
s3 = boto3.resource('s3') #Get S3 object
bucket = s3.Bucket('Bucket Dir name for s3')
bucket.upload_file('ec2 file name', 'File name in s3', ExtraArgs={'ACL':'public-read'})
Now you have a one-sided mechanism for conversations and other apps.
You can listen to it in the following html.
https://Bucket name.s3.amazonaws.com/index.html
$ aws s3 cp --acl public-read index.html s3://Bucket name/index.html
<html>
<body>
<figure>
<figcaption>Listen to the Answer:</figcaption>
<audio
autoplay
controls
src="speech0.mp3">
Your browser does not support the
<code>audio</code> element.
</audio>
</figure>
</body>
</html>
・ I tried to run the AWS hackathon app using the AWS free tier. ・ Japanese Text using Polly-Voice conversion was possible for continuous publication
・ S3-I would like to make an upload by downloading or using Lambda (Amazon Transcribe, Amazon Translate).
Recommended Posts