-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
246 lines (194 loc) · 8.16 KB
/
app.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from ElevateAI import ElevateAI
import json
import time
import email
import imaplib
import os
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import tempfile
import sys
import traceback
def get_newest_email_attachment(config):
# Create temporary directory to save attachment
tmp_folder = tempfile.mkdtemp()
file_name = ''
# Log in to the IMAP server
imap_server = config['imap_server']
imap_username = config['imap_username']
imap_password = config['imap_password']
imap = imaplib.IMAP4_SSL(imap_server)
imap.login(imap_username, imap_password)
# Select the INBOX folder
imap.select("INBOX")
# Search for the newest email message with an attachment
search_criteria = 'DATE'
result, data = imap.sort(search_criteria, 'UTF-8', 'SUBJECT "Transcribe"')
latest_email_id = data[0].split()[-1]
# Fetch the email message and extract the attachment
result, data = imap.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
email_message = email.message_from_bytes(raw_email)
attachment_path = None
sender_address = None
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
if not filename:
continue
# Save the attachment to a temporary file
file_name = filename
attachment_path = os.path.join(tmp_folder, filename)
with open(attachment_path, 'wb') as f:
f.write(part.get_payload(decode=True))
# Get the sender email address
sender_address = email.utils.parseaddr(email_message['From'])[1]
# Log out of the IMAP server
imap.close()
imap.logout()
return attachment_path, file_name, sender_address
def send_email_with_attachment(attachment_path, recipient_address, config):
smtp_server = config["smtp_server"]
smtp_username = config["smtp_username"]
smtp_password = config["smtp_password"]
# Log in to the SMTP server
smtp = smtplib.SMTP_SSL(smtp_server)
smtp.ehlo()
smtp.login(smtp_username, smtp_password)
print("SMTP logged in.")
# Create a message object
message = MIMEMultipart()
message['From'] = smtp_username
message['To'] = recipient_address
message['Subject'] = "Completed Transcription"
# Add the attachment to the message
with open(attachment_path, 'r') as f:
attachment = MIMEApplication(f.read(), _subtype='txt')
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachment_path))
message.attach(attachment)
# Send the message
smtp.send_message(message)
# Log out of the SMTP server
smtp.quit()
def print_conversation(json_str):
data = json.loads(json_str)
filename = 'transcript.txt'
# Initialize variables to store the accumulated phrases for each participant
participantOne_phrases = ""
participantTwo_phrases = ""
tmp_folder = tempfile.mkdtemp()
attachment_path = os.path.join(tmp_folder, filename)
print("=== Begin Transcription Output ===\n\n")
with open(attachment_path, 'w') as f:
# Loop through the sentenceSegments list and accumulate phrases for each participant
for segment in data['sentenceSegments']:
if segment['participant'] == 'participantOne':
participantOne_phrases += segment['phrase'] + " "
elif segment['participant'] == 'participantTwo':
participantTwo_phrases += segment['phrase'] + " "
# If the next segment has a different participant, print the accumulated phrases and reset the variables
if (data['sentenceSegments'].index(segment) != len(data['sentenceSegments'])-1) and (segment['participant'] != data['sentenceSegments'][data['sentenceSegments'].index(segment)+1]['participant']):
p1 = participantOne_phrases.strip()
p2 = participantTwo_phrases.strip()
if p1:
print("participantOne:\n" + p1 + "\n")
f.write("participantOne:\n" + p1 + "\n\n")
if p2:
print("participantTwo:\n" + p2 + "\n")
f.write("participantTwo:\n" + p2 + "\n\n")
participantOne_phrases = ""
participantTwo_phrases = ""
# Print the accumulated phrases for the last participant
p1 = participantOne_phrases.strip()
p2 = participantTwo_phrases.strip()
if p1:
print("participantOne:\n" + p1 + "\n")
f.write("participantOne:\n" + p1 + "\n\n")
if p2:
print("participantTwo:\n" + p2 + "\n")
f.write("participantTwo:\n" + p2 + "\n\n")
print("=== End Transcription Output ===\n\n")
f.close()
return attachment_path
def process_attachment(attach_path, file_name, config):
#Prereq - make sure you create a free account @ https://app.elevateai.com - this will let you generate a token
token = config["api_token"]
langaugeTag = "en-us"
vert = "default"
transcriptionMode = "highAccuracy"
localFilePath = attach_path
fileName = file_name
declareResp = ElevateAI.DeclareAudioInteraction(langaugeTag, vert, None, token, transcriptionMode, True)
declareJson = declareResp.json()
interactionId = declareJson["interactionIdentifier"]
if (localFilePath is None):
raise Exception('Something wrong with attachment')
uploadInteractionResponse = ElevateAI.UploadInteraction(interactionId, token, localFilePath, fileName)
#Loop over status until processed
while True:
getInteractionStatusResponse = ElevateAI.GetInteractionStatus(interactionId,token)
getInteractionStatusResponseJson = getInteractionStatusResponse.json()
if getInteractionStatusResponseJson["status"] == "processed" or getInteractionStatusResponseJson["status"] == "fileUploadFailed" or getInteractionStatusResponseJson["status"] == "fileDownloadFailed" or getInteractionStatusResponseJson["status"] == "processingFailed" :
break
time.sleep(15)
getPuncutatedTranscriptResponse = ElevateAI.GetPuncutatedTranscript(interactionId, token)
getAIResultsResponse = ElevateAI.GetAIResults(interactionId, token)
json_formatted_AI_str = json.dumps(getAIResultsResponse.json(), indent=4)
print(json_formatted_AI_str)
json_formatted_str = json.dumps(getPuncutatedTranscriptResponse.json(), indent=4)
parsed_transcription = print_conversation(json_formatted_str)
return parsed_transcription
def read_config(filename):
"""
Read and parse the configuration file.
"""
try:
with open(filename, 'r') as f:
config = json.load(f)
required_fields = ['imap_server', 'imap_username', 'imap_password',
'smtp_server', 'smtp_username', 'smtp_password', 'api_token']
for field in required_fields:
if field not in config:
raise ValueError(f"Config file is missing required field: {field}")
return config
except FileNotFoundError:
print(f'Error: Config file "{filename}" not found.')
sys.exit(1)
except json.JSONDecodeError:
print(f'Error: Config file "{filename}" is not valid JSON.')
sys.exit(1)
except ValueError as e:
print(f'Error: {e}')
sys.exit(1)
def main():
config = read_config('config.json')
# Get the newest email attachment
try:
attach_path, filename, sender = get_newest_email_attachment(config)
except imaplib.IMAP4.error:
print('\nError connecting to the IMAP server or retrieving the email\n')
traceback.print_exc()
return
# Process the attachment and generate transcript
try:
transcript = process_attachment(attach_path, filename, config)
except:
print('\nError in transcribing:\n')
traceback.print_exc()
return
if not os.path.exists(transcript):
print('\nError finding the transcription file to send\n')
return
# Send email with transcript attachment
try:
send_email_with_attachment(transcript, sender, config)
except smtplib.SMTPException:
print('\nError sending email through the SMTP server\n')
traceback.print_exc()
return
main()