Skip to content

Commit

Permalink
chore: fix readme (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ran-isenberg committed May 5, 2023
1 parent 9f4061a commit 00a3f02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This is a sample code as part of a blog post I'm working on.

### Who gave me this Idea?

I was inspired to design a solution for myself after seeing [this](https://www.youtube.com/watch?v=k-U_YJiuLGs) excellent YouTube video by Johannes Koch and Jimmy Dahlqvist.
I was inspired to design a solution for myself after seeing [this](https://www.youtube.com/watch?v=k-U_YJiuLGs) excellent YouTube video by Johannes Koch and Jimmy Dahlqvist.

### Architecture

Expand Down Expand Up @@ -89,7 +89,15 @@ File contents are created out of the Pipfile.lock.

``make deploy`` ``make deps`` commands generate it automatically.

### ** How to turn a text file to turn into speech?

Put a text file (.txt) into the /text folder. Deploy the CDK stack with 'make deploy' command.

It will be uploaded into an S3 bucket and turned into an mp3 file sent to you via email.

The email address is hardcoded and can be found at service/logic/email.py file.

When you add a new file: you can either upload to directly to the bucket or add them to the /text folder and run 'make deploy'.

## Code Contributions
Code contributions are welcomed. Read this [guide.](https://github.com/ran-isenberg/aws-lambda-handler-cookbook/blob/main/CONTRIBUTING.md)
Expand Down
8 changes: 1 addition & 7 deletions service/handlers/consumer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any, Dict, List

import boto3
from aws_lambda_powertools.utilities.parser import parse
from aws_lambda_powertools.utilities.parser.models import S3Model, S3RecordModel
from aws_lambda_powertools.utilities.typing import LambdaContext
Expand All @@ -21,13 +20,8 @@ def start(event: Dict[str, Any], context: LambdaContext) -> None:
logger.debug('environment variables', extra=env_vars.dict())
parsed_input: S3Model = parse(event=event, model=S3Model)
records: List[S3RecordModel] = parsed_input.Records
s3 = boto3.client('s3')
for record in records:
bucket_name = record.s3.bucket.name
object_key = record.s3.object.key
# read text file
obj = s3.get_object(Bucket=bucket_name, Key=object_key)
text: str = obj['Body'].read().decode('utf-8').replace('\n', '')
logger.info(f'working on object {object_key} in the bucket {bucket_name}')
consume_text_async(text, bucket_name)
consume_text_async(bucket_name, object_key)
logger.info('finished handling text processor event')
12 changes: 10 additions & 2 deletions service/logic/consume_text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from pathlib import Path

import boto3

from service.dal.polly_handler import PollyWrapper
from service.handlers.utils.observability import logger
from service.logic.email import send_binary_file


Expand All @@ -14,13 +17,18 @@ def consume_text() -> None:
f.write(audio_stream.read())


def consume_text_async(text: str, output_bucket_name: str) -> None:
def consume_text_async(bucket_name: str, object_key: str) -> None:
# read text file
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket_name, Key=object_key)
text: str = obj['Body'].read().decode('utf-8').replace('\n', '')
logger.info(f'working on object {object_key} in the bucket {bucket_name}')
polly_wrapper = PollyWrapper()
audio_stream, _, = polly_wrapper.do_synthesis_task(
text=text,
engine='neural',
voice='Ruth',
audio_format='mp3',
s3_bucket=output_bucket_name,
s3_bucket=bucket_name,
)
send_binary_file(audio_stream.read())

0 comments on commit 00a3f02

Please sign in to comment.