From 452561fb5b586ce5541f0f2a4bd7ce6d02e5f71d Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 10 Sep 2024 14:33:10 +0300 Subject: [PATCH] try to fix config; exe build config (spec) --- .gitignore | 1 + agent-mAId/config.json | 3 ++- agent-mAId/main.py | 54 +++++++++++++++++++++++++++++++------ agent-mAId/readme.md | 9 ++++++- agent-mAId/requirements.txt | 6 +++-- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 9de15f3..93b5853 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ agent-pyter/google-chrome-stable_current_amd64.deb web/.node-persist/* agent-mAId/output.wav agent-mAId/build/* +agent-mAId/dist/main.exe diff --git a/agent-mAId/config.json b/agent-mAId/config.json index 8d7d658..108c567 100644 --- a/agent-mAId/config.json +++ b/agent-mAId/config.json @@ -3,6 +3,7 @@ "kb_key": "ctrl", "mouse_btn": "left", "model": "distil-whisper-large-v3-en", - "language":"en" // whisper-large-v3 or distil-whisper-large-v3-en + "language":"en", // whisper-large-v3 or distil-whisper-large-v3-en + "action": "type" //type,copy } \ No newline at end of file diff --git a/agent-mAId/main.py b/agent-mAId/main.py index d6264cd..0420952 100644 --- a/agent-mAId/main.py +++ b/agent-mAId/main.py @@ -15,12 +15,44 @@ import io import time import json5 import wave +import pyperclip + +# # Load configuration from config.json +DEFAULT_CONFIG = { + "kb_key": "ctrl", + "mouse_btn": "left", + "model": "distil-whisper-large-v3-en", + "language": "en", # whisper-large-v3 or distil-whisper-large-v3-en + "action": "type" # type, copy +} -# Load configuration from config.json def load_config(): - config_path = os.path.join(os.path.dirname(__file__), 'config.json') - with open(config_path, 'r') as config_file: - return json5.load(config_file) + """Load the configuration file, adjusting for PyInstaller's temp path when bundled.""" + config = DEFAULT_CONFIG.copy() # Start with default configuration + + try: + # Determine if the script is running as a PyInstaller bundle + if getattr(sys, 'frozen', False): + # If running in a bundle, use the temp path where PyInstaller extracts files + config_path = os.path.join(sys._MEIPASS, 'config.json') + else: + # If running in development (normal execution), use the local directory + config_path = os.path.join(os.path.dirname(__file__), 'config.json') + + print('Trying to load config from:', config_path) + with open(config_path, 'r') as config_file: + loaded_config = json5.load(config_file) + # Update the default config with any values from config.json + config.update(loaded_config) + + except FileNotFoundError as ex: + print("Config file not found, using defaults." + ex.strerror) + except json5.JSONDecodeError as ex: + print("Error decoding config file, using defaults." + ex.msg) + except Exception as e: + print(f"Unexpected error while loading config: {e}, using defaults.") + + return config # Load the config config = load_config() @@ -29,6 +61,7 @@ API_KEY = config['api_key'] KB_KEY = config['kb_key'] MOUSE_BTN = config['mouse_btn'] MODEL = config['model'] +POST_TRANSCRIBE = config['action'] # Constants AUTO_START_PATH = os.path.expanduser(r"~\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup") # For autostart @@ -90,7 +123,7 @@ def transcribe_audio(memory_stream): transcription = client.audio.transcriptions.create( file=('audio.wav', memory_stream), model=MODEL, - prompt="Specify context or spelling", + prompt="Transcribe the following audio", language=config['language'], response_format="json", temperature=0.0 @@ -159,9 +192,14 @@ def main_loop(): print("Transcribing audio...") transcribed_text = transcribe_audio(memory_stream) - # Simulate typing the transcribed text - print("Typing transcribed text...") - simulate_keypress(transcribed_text) + if POST_TRANSCRIBE == "type": + # Simulate typing the transcribed text + print("Typing transcribed text...") + simulate_keypress(transcribed_text) + elif POST_TRANSCRIBE == "copy": + # Copy the transcribed text to clipboard + pyperclip.copy(transcribed_text) + print("Transcribed text copied to clipboard.") if __name__ == "__main__": # Start the tray icon in a separate thread so it doesn't block the main functionality diff --git a/agent-mAId/readme.md b/agent-mAId/readme.md index 0700184..3ac5af8 100644 --- a/agent-mAId/readme.md +++ b/agent-mAId/readme.md @@ -1,3 +1,10 @@ -# to install as an app: + pip install pyinstaller pyinstaller --onefile main.py + pyinstaller main.spec + + + +pipreqs . + +pip freeze > requirements.txt diff --git a/agent-mAId/requirements.txt b/agent-mAId/requirements.txt index 13c1fd3..76eb3cc 100644 --- a/agent-mAId/requirements.txt +++ b/agent-mAId/requirements.txt @@ -1,7 +1,9 @@ -#> pipreqs . - groq==0.11.0 +json5==0.9.25 keyboard==0.13.5 mouse==0.7.1 +Pillow==10.4.0 PyAudio==0.2.14 PyAutoGUI==0.9.54 +pyperclip==1.9.0 +pystray==0.19.5