Last active
September 25, 2022 14:51
-
-
Save krishashok/ed2d3b417bec31e542a1b9096a32e407 to your computer and use it in GitHub Desktop.
Script to transcribe iPhone Voice memos on a daily basis and email the transcriptions to yourself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The Daily Transcriber will take voice notes created on the day the script is run | |
# from iPhone's voice memo app (via iCloud) and transcribe them using Whisper and email | |
# the transcriptions to a destination email of choice. | |
# The best way to set this script up is as a daily cron job | |
# Requirements | |
# You will need to enable "Less Secure App Access" on your Gmail account security settings to be able to send emails | |
# You will also need to install Whisper (follow instructions here - https://github.com/openai/whisper) | |
# Imports | |
import datetime as dt | |
import os | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
import whisper | |
# Email Credentials and Server Details | |
MY_EMAIL = 'YOUR SENDER EMAIL HERE' # Try and avoid using your actual email ID. Create a dummy one for this | |
MY_PASSWORD = 'YOUR PASSWORD HERE' | |
EMAIL_SERVER = 'YOUR EMAIL SERVER' | |
EMAIL_PORT = 587 # YOUR SERVER PORT HERE | |
# Load the whisper model (Change to 'small' or 'base' if the script takes too long) | |
model = whisper.load_model('medium') | |
# Voice memo path (Change as appropriate) | |
VOICE_MEMOS_PATH = "PATH TO VOICE MEMOS ON YOUR MACHINE" | |
# Get today's recordings and process them with Whisper | |
today = dt.datetime.now().date() | |
transcripts = [] | |
for file in os.listdir(VOICE_MEMOS_PATH): | |
file_created_time = dt.datetime.fromtimestamp(os.path.getctime(VOICE_MEMOS_PATH + file)) | |
if (file[-3:] == 'm4a') and (file_created_time.date() == today): | |
transcript = {} | |
print(f"processing {file}") | |
result = model.transcribe(VOICE_MEMOS_PATH + file, fp16=False) | |
transcript["timestamp"] = file_created_time.strftime('%-d %B,%Y %H:%M') | |
transcript["text"] = result["text"] | |
transcripts.append(transcript) | |
# Construct email body content | |
msg_content = "<html></body>" | |
for item in transcripts: | |
msg_content += "<p>" | |
msg_content += item["timestamp"] | |
msg_content += "<br>" | |
msg_content += item["text"] | |
msg_content += "</p>" | |
msg_content += "</html></body>" | |
# Construct email message | |
msg = MIMEMultipart('alternative') | |
msg['From'] = MY_EMAIL | |
msg['To'] = 'RECIPIENT EMAIL' | |
msg['Subject'] = f'Transcribed Voice Memos for {today.strftime("%d-%B-%Y")}' | |
txt_part = MIMEText(msg_content, 'plain', 'utf-8') | |
html_part = MIMEText(msg_content, 'html', 'utf-8') | |
msg.attach(txt_part) | |
msg.attach(html_part) | |
msg_str = msg.as_string() | |
# Send email | |
with smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT) as connection: | |
connection.starttls() | |
connection.login(user=MY_EMAIL, password=MY_PASSWORD) | |
connection.sendmail( | |
from_addr=MY_EMAIL, | |
to_addrs='YOUR RECIPIENT EMAIL', | |
msg=msg_str | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment