config file, executable, tray icon

This commit is contained in:
Dobromir Popov 2024-09-10 11:45:17 +03:00
parent 400bccfeec
commit 4974af0678
6 changed files with 51 additions and 12 deletions

8
agent-mAId/config.json Normal file
View File

@ -0,0 +1,8 @@
{
"api_key": "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",
"kb_key": "ctrl",
"mouse_btn": "left",
"model": "distil-whisper-large-v3-en",
"language":"en" // whisper-large-v3 or distil-whisper-large-v3-en
}

View File

@ -12,25 +12,39 @@ from pystray import MenuItem as item
from PIL import Image from PIL import Image
import ctypes import ctypes
import time import time
import json5
# 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 config
config = load_config()
# Extract API key and button from the config file
API_KEY = config['api_key']
KB_KEY = config['kb_key']
MOUSE_BTN = config['mouse_btn']
MODEL = config['model']
# Constants # Constants
API_KEY = "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" # Make sure to use your actual API key
BUTTON = 'ctrl' # The keyboard button to listen for
AUTO_START_PATH = os.path.expanduser(r"~\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup") # For autostart AUTO_START_PATH = os.path.expanduser(r"~\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup") # For autostart
# Initialize the Groq client # Initialize the Groq client
client = Groq(api_key=API_KEY) client = Groq(api_key=API_KEY)
def record_audio(filename): def record_audio(filename):
"""Records audio when BUTTON or mouse button is pressed.""" """Records audio when key and mouse button is pressed."""
audio = pyaudio.PyAudio() audio = pyaudio.PyAudio()
stream = audio.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024) stream = audio.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
frames = [] frames = []
print("Recording...") print("Recording...")
# Record while button or mouse is pressed # Record while button or mouse is pressed
while keyboard.is_pressed(BUTTON) and mouse.is_pressed(button='left'): while keyboard.is_pressed(KB_KEY) and mouse.is_pressed(button=MOUSE_BTN):
data = stream.read(1024) data = stream.read(1024)
frames.append(data) frames.append(data)
@ -52,8 +66,9 @@ def transcribe_audio(filename):
with open(filename, "rb") as file: with open(filename, "rb") as file:
transcription = client.audio.transcriptions.create( transcription = client.audio.transcriptions.create(
file=(filename, file.read()), file=(filename, file.read()),
model="distil-whisper-large-v3-en", model=MODEL, #"distil-whisper-large-v3-en",
prompt="Specify context or spelling", prompt="Specify context or spelling",
language=config['language'],
response_format="json", response_format="json",
temperature=0.0 temperature=0.0
) )
@ -67,7 +82,7 @@ def simulate_keypress(text):
def add_to_autostart(): def add_to_autostart():
"""Registers the app to autostart on login.""" """Registers the app to autostart on login."""
script_path = os.path.abspath(__file__) script_path = os.path.abspath(__file__)
shortcut_path = os.path.join(AUTO_START_PATH, "MyApp.lnk") shortcut_path = os.path.join(AUTO_START_PATH, "mAId.lnk")
# Use ctypes to create the shortcut (this is Windows specific) # Use ctypes to create the shortcut (this is Windows specific)
shell = ctypes.windll.shell32 shell = ctypes.windll.shell32
@ -79,27 +94,31 @@ def quit_app(icon):
icon.stop() icon.stop()
sys.exit() sys.exit()
def setup_tray_icon(): def setup_tray_icon():
"""Setup system tray icon and menu.""" """Setup system tray icon and menu."""
icon_image = Image.new('RGB', (64, 64), color=(255, 0, 0)) # Red icon as an example #icon_image = Image.new('RGB', (64, 64), color=(255, 0, 0)) # Red icon as an example
icon_image = Image.open('mic.webp')
menu = ( menu = (
item('Register to Autostart', add_to_autostart), item('Register to Autostart', add_to_autostart),
item('Exit', lambda: quit_app(icon)) item('Exit', lambda: quit_app(icon))
) )
icon = pystray.Icon("my_app", icon_image, menu=pystray.Menu(*menu)) icon = pystray.Icon("mAId", icon_image, menu=pystray.Menu(*menu))
icon.run() icon.run()
def main_loop(): def main_loop():
"""Continuously listen for key or mouse press and transcribe audio.""" """Continuously listen for key or mouse press and transcribe audio."""
filename = "output.wav" filename = "output.wav"
while True: while True:
print("Waiting for button or mouse press...") print("Waiting for key and mouse press...")
# Wait for button or mouse press # Wait for KB_KEY or mouse press
while not (keyboard.is_pressed(BUTTON) or mouse.is_pressed(button='left')): while not (keyboard.is_pressed(KB_KEY) and mouse.is_pressed(button=MOUSE_BTN)):
time.sleep(0.1) # Small sleep to avoid busy-waiting time.sleep(0.1) # Small sleep to avoid busy-waiting
# Record audio # Record audio

BIN
agent-mAId/mic.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

3
agent-mAId/readme.md Normal file
View File

@ -0,0 +1,3 @@
# to install as an app:
pip install pyinstaller
pyinstaller --onefile main.py

View File

@ -1,4 +1,12 @@
//C:\Users\popov\.continue\config.json
{ {
"models": [ {
"title": "local ollama> yi-coder",
"provider": "ollama",
"model": "yi-coder:9b",
"apiBase": "http://localhost:11434"
}
],
"tabAutocompleteModel": { "tabAutocompleteModel": {
"title": "Tab Autocomplete Model", "title": "Tab Autocomplete Model",
"provider": "ollama", "provider": "ollama",
@ -8,6 +16,7 @@
} }
// original: "tabAutocompleteModel": { // original: "tabAutocompleteModel": {
// "title": "Starcoder 3b", // "title": "Starcoder 3b",
// "provider": "ollama", // "provider": "ollama",