[Introduction to AWS] Text-Voice conversion and playing ♪

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%.

What i did

・ Establish a development style ・ Last code ・ Slight improvement ・ Listen to the sound

・ Establish a development style

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

・ Last code

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.

・ Slight improvement

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.

・ Listen to the sound

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>

Summary

・ 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

[Introduction to AWS] Text-Voice conversion and playing ♪
[Introduction to AWS] I tried playing with voice-text conversion ♪
[Introduction to AWS] I tried porting the conversation app and playing with text2speech @ AWS ♪
[Introduction to infectious disease model] I tried fitting and playing ♬
[Introduction to Python3 Day 1] Programming and Python
[AWS SAM] Introduction to Python version
[Introduction to AWS] I played with male and female voices with Polly and Transcribe ♪
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Thano Function definition and automatic differentiation
Introduction to Supervisor
Introduction to Tkinter 1: Introduction
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
[Introduction to AWS] The first Lambda is Transcribe ♪
Introduction to PyQt
Introduction to Scrapy (2)
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Linux] Introduction to Linux
[AWS; Introduction to Lambda] 2nd; Extract sentences from json file and save S3 ♬
Introduction to Scrapy (4)
Introduction to Deep Learning ~ Localization and Loss Function ~
Introduction to discord.py (2)
[Introduction to PID] I tried to control and play ♬
Introduction to discord.py
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[Introduction to Style GAN] Mayu Yu and anime smiled ♬
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
Introduction to Lightning pytorch
Introduction to Nonparametric Bayes
Introduction to EV3 / MicroPython
Hexadecimal string and string conversion
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
Introduction to PyQt4 Part 1
Introduction to Dependency Injection
Introduction to Private Chainer
Introduction to machine learning
Easy introduction to home hack with Raspberry Pi and discord.py
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to Data Scientists] Descriptive Statistics and Simple Regression Analysis ♬
[Introduction to cx_Oracle] (Part 4) Fetch and scroll of result set
[Introduction to pytorch] Preprocessing by audio I / O and torch audio (> <;)
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
[Introduction to cx_Oracle] (10th) Update DML and DDL / DCL statements
[Introduction to simulation] I tried playing by simulating corona infection ♬
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to AWS] A memorandum of building a web server on AWS
[Introduction to Scipy] Calculation of Lorenz curve and Gini coefficient ♬
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Introduction of DataLiner ver.1.3 and how to use Union Append
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
[Introduction to Mac] Convenient Mac apps and settings that I use