This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
160 lines (122 loc) · 4.08 KB
/
main.py
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
import argparse
import os
from sys import argv
from time import sleep
from typing import Optional
import boto3
def list_existing(bucket, prefix) -> list:
keys = []
for obj in bucket.objects.filter(Delimiter="/", Prefix=f"{prefix}/"):
keys.append(obj.key)
return keys
def delete_existing(bucket, prefix):
keys_to_delete = list_existing(bucket, prefix)
if len(keys_to_delete) > 0:
bucket.delete_objects(Delete={"Objects": [{"Key": k} for k in keys_to_delete]})
def upload_pages(client, local_path, bucket, prefix):
s3_waiter = client.get_waiter("object_exists")
with os.scandir(local_path) as page_files:
page_file: os.DirEntry
for page_file in page_files:
if not page_file.is_file() or page_file.name == ".gitignore":
continue
print(f"uploading {page_file.name}")
data = open(page_file.path, "rb")
key = f"{prefix}/{page_file.name}"
bucket.put_object(Key=key, Body=data)
s3_waiter.wait(Bucket=bucket.name, Key=key)
def extract_text(session, bucket, prefix) -> Optional[str]:
s3_client = session.client("s3")
textract_client = session.client("textract")
pages_to_read = list_existing(bucket, prefix)
if len(pages_to_read) == 0:
return
all_lines = []
for key in pages_to_read:
lines = []
response = textract_client.detect_document_text(
Document={"S3Object": {"Bucket": bucket.name, "Name": key}}
)
for item in response["Blocks"]:
if item["BlockType"] == "LINE":
lines.append(item["Text"])
store_text_in_s3(
client=s3_client,
bucket=s3_bucket,
key=f"{key}.txt",
text=" ".join(lines),
)
all_lines.extend(lines)
return " ".join(all_lines)
def store_text_in_s3(client, bucket, key, text: str):
print(f"storing {key} in s3")
bucket.put_object(Key=key, Body=text.encode("utf-8"))
s3_waiter = client.get_waiter("object_exists")
s3_waiter.wait(Bucket=bucket.name, Key=key)
def synthesize_text(client, bucket, prefix, text):
response = client.start_speech_synthesis_task(
Engine="neural",
LanguageCode="en-GB",
OutputFormat="ogg_vorbis",
OutputS3BucketName=bucket.name,
OutputS3KeyPrefix=prefix,
Text=text,
TextType="text",
VoiceId="Amy",
)
task_id = response.get("SynthesisTask", {}).get("TaskId")
status = "scheduled"
while status not in ["completed", "failed"]:
status = (
client.get_speech_synthesis_task(TaskId=task_id)
.get("SynthesisTask", {})
.get("TaskStatus")
)
print(status)
sleep(3)
print(
client.get_speech_synthesis_task(TaskId=task_id)
.get("SynthesisTask", {})
.get("TaskStatusReason")
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("profile")
parser.add_argument("bucket")
parser.add_argument("prefix")
parser.add_argument("directory")
print(parser)
if len(argv) != 5:
parser.print_help()
exit(1)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
AWS_PROFILE = args.profile
AWS_BUCKET = args.bucket
AWS_PREFIX_KEY = args.prefix
PAGES_DIR = args.directory
session = boto3.Session(profile_name=AWS_PROFILE)
s3 = session.resource("s3")
s3_bucket = s3.Bucket(AWS_BUCKET)
print("deleting old images")
delete_existing(s3_bucket, AWS_PREFIX_KEY)
print("uploading new images")
upload_pages(
client=session.client("s3"),
local_path=PAGES_DIR,
bucket=s3_bucket,
prefix=AWS_PREFIX_KEY,
)
print("extracting text")
extracted_text = extract_text(
session=session, bucket=s3_bucket, prefix=AWS_PREFIX_KEY
)
print("creating audio file")
synthesize_text(
client=session.client("polly"),
bucket=s3_bucket,
prefix=AWS_PREFIX_KEY,
text=extracted_text,
)
print("finished")