AFKode API

Provides voice note taking functionality with Whisper and ChatGPT

afkode.action

This module provides a Command class for recognizing and executing voice commands.

Each command corresponds to a Python file in the ‘command’ directory with an ‘execute’ function.

class afkode.action.Command(transcript)

Allows for selection of commands from extra folder.

collate_and_choose_command(command_candidate)

Check the ‘command’ directory for a file matching the command_candidate.

Parameters:

command_candidate (str) – The potential command name to look for.

Return type:

None

execute()

Execute the recognized command by importing its module and calling its ‘execute’ function.

Returns:

The result of the ‘execute’ function if the command was recognized, otherwise None.

Return type:

str

Raises:

ImportError – If the command was not recognized.

afkode.api

This file contains functions related to interacting with external API systems.

afkode.api.chatgpt(prompt, model='gpt-3.5-turbo')

Sends a prompt to the ChatGPT API for chat-based language generation.

Parameters:
  • prompt (str) – The prompt text.

  • model (str) – The name of the model to use. Default is “gpt-3.5-turbo”.

Return type:

str

Returns:

The generated text response.

afkode.api.get_credentials()

Loads credentials for API calls.

Return type:

Dict[str, str]

Returns:

Credentials from several services used in API calls

afkode.api.google_tts(output_path, text_input=None, ssml_input=None)

Converts text to speech audio file and saves.

Parameters:
  • output_path (Path) – Location to write completed google TTS file

  • text_input (Optional[str]) – Text input for text to speech

  • ssml_input (Optional[str]) – Alternatively pass in SSML input for text to speech

Return type:

None

afkode.api.whisper(path)

Transcribes audio from the specified file path using the Whisper API.

Parameters:

path (str) – The path to the audio file.

Return type:

str

Returns:

The transcribed text.

afkode.meta

meta-programming scripts to receive help from chatgpt on program improvements.

afkode.meta.clean_py(python_text)

Loads first portion of the project readme.

Parameters:

python_text (str) – input text of loaded python file

Return type:

str

Returns:

Removes top text from file

afkode.meta.get_formatted_readme()

Loads first portion of the project readme.

Return type:

str

Returns:

Cleaned up readme string only taking first part.

afkode.meta.start(prompt=None, input_files=None, execute=False)

Conduct an adhoc analysis on the codebase.

Parameters:
  • prompt (Optional[str]) – text of the LLM prompt

  • input_files (Optional[List[Path]]) – list of paths to include in the prompt

  • execute (bool) – whether to run on LLM or not

Return type:

None

afkode.run

This file is the main entry point of the AFKode program.

It handles the execution of the program and orchestrates the different components.

afkode.run.start()

Begins the AFKode program and handles the user interaction and file operations.

The program records voice input, transcribes it, and performs various operations based on the user’s commands.

It includes the main program loop, which only exits when specifically commanded by the user.

Return type:

None

afkode.set_env

This script helps imports on Pythonista or MacOS.

afkode.utils

Contains utility functions used throughout the AFKode project.

afkode.utils.extract_number(filename)

Extracts a number from a filename using regular expressions.

Parameters:

filename (str) – The name of the file.

Return type:

int

Returns:

The extracted number, or None if no number is found.

afkode.utils.get_agent_response_directory()

Retrieves the directory path for user input.

Return type:

Path

Returns:

The path to the user input directory.

afkode.utils.get_base_path()

Helps to determine where to run file activities depending on env.

Return type:

Path

Returns:

Resolved base path of program

afkode.utils.get_formatted_command_list()

Parses the command folder and produces a simple list of commands.

Return type:

str

Returns:

Cleaned up string with ‘filename - description’

afkode.utils.get_prompt_path()

Helper to find prompt directory.

Return type:

Path

Returns:

Resolved base path of prompt directory.

afkode.utils.get_spoken_command_list()

Get cleaned up simple list of actions.

Return type:

List[str]

Returns:

List of cleaned up actions

afkode.utils.get_user_prompt_directory()

Retrieves the directory path for user input.

Return type:

Path

Returns:

The path to the user input directory.

afkode.utils.get_user_prompt_files()

Retrieves a string of all the filenames in the user input directory.

Return type:

str

Returns:

The string of filenames.

afkode.utils.load_config()

Load the config file for basic behaviour change.

Return type:

Dict[str, Any]

Returns:

Basic dictionary with user facing options.

afkode.utils.resolve_input_paths(input_files, exclude=None)

Given a list of file paths, test and resolve them.

Parameters:
  • input_files (List[str]) – The list of input file paths.

  • exclude (Optional[List[str]]) – The list of files to exclude. Default is None.

Return type:

List[Path]

Returns:

The list of resolved paths.

afkode.utils.setup_logging(log_level=20)

Configures the logging settings for the application.

Parameters:

log_level (int) – The log level to set. Default is DEBUG.

Return type:

None

afkode.utils.split_transcription_on(transcription, words, strategy='after')

Uses regex matching based on case-insensitive, matched word boundaries to split text before or after.

  • It matches the exact word you’re looking for, bounded by non-word characters or the beginning/end of the string.

  • It splits the transcription at these word boundaries.

  • If strategy is “after”, it takes everything after the last occurrence of the word.

  • If strategy is “before”, it takes everything before the first occurrence of the word.

  • If strategy is “detect”, it will remove the word; this can be tested as a length difference

  • If the word is part of a larger word it doesn’t match.

  • Case insensitive

Parameters:
  • transcription (str) – raw transcription text

  • words (Union[str, List[str]]) – single or list of keywords to search and split on

  • strategy (str) – whether to return text before or after the word

Raises:

ValueError – if an invalid strategy selected

Return type:

str

Returns:

Cleaned transcription text that focuses on info between keywords

afkode.voice_interface

Main controller for voice recording and stop detection.

class afkode.voice_interface.VoiceRecorder

Main controller for voice recording and stop deteection.

clear_data()

Clear input and output folders.

Return type:

None

combine_wav_files()

Combines several .wav files into a single one for transcription.

Return type:

None

load_config()

Load relevant configuration options.

Return type:

None

short_recording(q)

We use shorter recordings for stop word detection every few seconds.

Parameters:

q – Queue to hold completed audio paths.

Return type:

None

simple_record()

Used for confirmations.

Return type:

str

Returns:

Transcription of recorded text

start_detection()

Start the voice detection process.

Return type:

None

transcribe_and_detect_stop(q)

Transcribe short recordings and detect stop words.

Parameters:

q – Queue to hold completed audio paths.

Return type:

None

transcribe_whole()

Perform final transcribe, removing text after stopword.

Return type:

str

Returns:

Transcribed text

afkode.ios.listen

Contains functions related to recording speech on iOS with pythonista.

afkode.ios.listen.basic_record(file_name, record_time)

Simple recording by time for testing.

Parameters:
  • file_name (str) – file name to convert to an apple URL

  • record_time (float) – record using a sleep timer

Return type:

None

afkode.ios.listen.bluetooth(file_name)

Instanciate bluetooth audio for recording.

Parameters:

file_name (str) – file name to convert to an apple URL

Raises:

ValueError – if recorder can’t be started

Return type:

Any

Returns:

recording object

afkode.ios.speech

Contains functions related to text-to-speech on iOS with pythonista.

afkode.ios.speech.play_blip()

Play a short sound to help notify of API speed.

Return type:

None

afkode.ios.speech.speak(text)

Say the text using iOS capability.

Parameters:

text (str) – text to say

Return type:

None

afkode.macos.listen

Contains functions related to recording speech on MacOS.

class afkode.macos.listen.Recorder(file_name, chunk=1024, channels=1, rate=44100)

MacOS specific audio recording.

record()

Start recording audio.

Return type:

None

release()

Release recorded audio and save to file.

Return type:

None

stop()

Stop recording audio.

Return type:

None

afkode.macos.listen.basic_record(file_name, record_time)

Record audio for a specified duration.

Parameters:
  • file_name (str) – Name of the output file.

  • record_time (int) – Duration of the recording in seconds.

Return type:

None

afkode.macos.listen.bluetooth(file_name)

Example usage: Obtain a Recorder object using a Bluetooth connection.

Parameters:

file_name (str) – Name of the output file.

Return type:

Recorder

Returns:

Recorder object.

afkode.macos.speech

Contains functions related to text-to-speech on MacOS.

afkode.macos.speech.make_dir()

Clear and setup the output directories.

Return type:

None

afkode.macos.speech.play_audio(q)

Play audio files in the queue.

Parameters:

q (Queue) – Queue for getting through speech.

Return type:

None

afkode.macos.speech.play_blip()

Play a short sound to help notify of API speed.

Return type:

None

afkode.macos.speech.speak(text)

Run text-to-speech on non-ios platform.

Parameters:

text (str) – full text to be spoken.

Return type:

None

afkode.macos.speech.split_text(text)

Split the given text into sentences.

Parameters:

text (str) – text to be split into different spoken sections.

Return type:

List[str]

Returns:

List to split text into sections for speech engine.

afkode.macos.speech.text_to_speech(sentence, idx)

Create a gTTS object.

Parameters:
  • sentence (str) – input text to be spoken

  • idx (int) – index, to increment filename

Return type:

str

Returns:

Saves a filename for further playing