From 74f26fdf7369648b26846704a80458d04fd7ffa9 Mon Sep 17 00:00:00 2001 From: "Patrick Loeber (Python Engineer)" Date: Tue, 2 Nov 2021 07:06:46 +0100 Subject: [PATCH] add note taking with speech recognition project --- notetaking-speech-rec/README.md | 27 ++++++++++++++ notetaking-speech-rec/main.py | 65 +++++++++++++++++++++++++++++++++ notetaking-speech-rec/notion.py | 52 ++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 notetaking-speech-rec/README.md create mode 100644 notetaking-speech-rec/main.py create mode 100644 notetaking-speech-rec/notion.py diff --git a/notetaking-speech-rec/README.md b/notetaking-speech-rec/README.md new file mode 100644 index 0000000..817c76b --- /dev/null +++ b/notetaking-speech-rec/README.md @@ -0,0 +1,27 @@ +# Create a Notetaking App with Speech Recognition + +## Installation + +On Mac you also need: +``` +$ brew install portaudio +$ pip install pyobjc + +``` + +Then use: +``` +# pip install pyaudio +# pip install speechrecognition +# pip install requests gtts playsound +``` + +Note: On a M1 Mac you may need to use this command to install pyaudio +``` +# python -m pip install --global-option='build_ext' --global-option='-I/opt/homebrew/Cellar/portaudio/19.7.0/include' --global-option='-L/opt/homebrew/Cellar/portaudio/19.7.0/lib' pyaudio +``` + +For more setup instructions also have a look here: +- [Pyaudio Installation](http://people.csail.mit.edu/hubert/pyaudio/) +- [Speech Recognition](https://github.com/Uberi/speech_recognition) +- [Notion API setup](https://developers.notion.com/docs/getting-started) \ No newline at end of file diff --git a/notetaking-speech-rec/main.py b/notetaking-speech-rec/main.py new file mode 100644 index 0000000..9c0bbaa --- /dev/null +++ b/notetaking-speech-rec/main.py @@ -0,0 +1,65 @@ +import speech_recognition as sr +import gtts +from playsound import playsound +import os +from datetime import datetime +from notion import NotionClient + +r = sr.Recognizer() + +token = "YOUR NOTION TOKEN HERE" +database_id = "YOUR NOTION DATABASE_ID HERE" + +client = NotionClient(token, database_id) + +ACTIVATION_COMMAND = "hey sam" + +def get_audio(): + with sr.Microphone() as source: + print("Say something") + audio = r.listen(source) + return audio + +def audio_to_text(audio): + text = "" + try: + text = r.recognize_google(audio) + except sr.UnknownValueError: + print("Speech recognition could not understand audio") + except sr.RequestError: + print("could not request results from API") + return text + +def play_sound(text): + try: + tts = gtts.gTTS(text) + tempfile = "./temp.mp3" + tts.save(tempfile) + playsound(tempfile) + os.remove(tempfile) + except AssertionError: + print("could not play sound") + + + +if __name__ == "__main__": + + while True: + a = get_audio() + command = audio_to_text(a) + + if ACTIVATION_COMMAND in command.lower(): + print("activate") + play_sound("What can I do for you?") + + note = get_audio() + note = audio_to_text(note) + + if note: + play_sound(note) + + now = datetime.now().astimezone().isoformat() + res = client.create_page(note, now, status="Active") + if res.status_code == 200: + play_sound("Stored new item") + diff --git a/notetaking-speech-rec/notion.py b/notetaking-speech-rec/notion.py new file mode 100644 index 0000000..31b9031 --- /dev/null +++ b/notetaking-speech-rec/notion.py @@ -0,0 +1,52 @@ +import json +import requests + + +class NotionClient: + + def __init__(self, token, database_id) -> None: + self.database_id = database_id + + self.headers = { + "Authorization": "Bearer " + token, + "Content-Type": "application/json", + "Notion-Version": "2021-08-16" + } + + # read, update + def create_page(self, description, date, status): + create_url = 'https://api.notion.com/v1/pages' + + data = { + "parent": { "database_id": self.database_id }, + "properties": { + "Description": { + "title": [ + { + "text": { + "content": description + } + } + ] + }, + "Date": { + "date": { + "start": date, + "end": None + } + }, + "Status": { + "rich_text": [ + { + "text": { + "content": status + } + } + ] + } + }} + + data = json.dumps(data) + res = requests.post(create_url, headers=self.headers, data=data) + print(res.status_code) + return res