Add files via upload
This commit is contained in:
@@ -283,4 +283,4 @@ For issues, questions, or contributions:
|
||||
- Check the troubleshooting section above
|
||||
- Review log files for error details
|
||||
- Test individual components with verbose logging
|
||||
- Ensure all dependencies are properly installed
|
||||
- Ensure all dependencies are properly installed
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"hotkey": "<ctrl>+<alt>+v",
|
||||
"audio": {
|
||||
"sample_rate": 16000,
|
||||
"channels": 1,
|
||||
"device_index": null,
|
||||
"noise_reduction_level": 1,
|
||||
"silence_threshold": -40
|
||||
},
|
||||
"whisper": {
|
||||
"model": "small",
|
||||
"language": null
|
||||
},
|
||||
"typing": {
|
||||
"speed": 0.05
|
||||
},
|
||||
"logging": {
|
||||
"level": "INFO",
|
||||
"file": "/home/vshnsngh/Desktop/Coding/voice-to-text-system/logs/voice-to-text.log"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Voice-to-Text System Installation Script
|
||||
# This script installs the voice-to-text system and its dependencies
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Configuration
|
||||
PROJECT_NAME="voice-to-text-system"
|
||||
INSTALL_DIR="/opt/$PROJECT_NAME"
|
||||
SERVICE_NAME="voice-to-text"
|
||||
CONFIG_DIR="/etc/$PROJECT_NAME"
|
||||
LOG_DIR="/var/log/$PROJECT_NAME"
|
||||
BIN_DIR="/usr/local/bin"
|
||||
|
||||
# Get current directory (where the script is located)
|
||||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
PROJECT_DIR="$( cd "$SCRIPT_DIR/.." && pwd )"
|
||||
|
||||
print_status() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
check_root() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
print_error "This script must be run as root (use sudo)"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
check_system() {
|
||||
print_status "Checking system requirements..."
|
||||
|
||||
# Check if running on Linux
|
||||
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
|
||||
print_error "This system is only supported on Linux"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if Python 3 is installed
|
||||
if ! command -v python3 &> /dev/null; then
|
||||
print_error "Python 3 is required but not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if pip is installed
|
||||
if ! command -v pip3 &> /dev/null; then
|
||||
print_error "pip3 is required but not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_success "System requirements met"
|
||||
}
|
||||
|
||||
install_system_deps() {
|
||||
print_status "Installing system dependencies..."
|
||||
|
||||
# Detect package manager and install dependencies
|
||||
if command -v apt-get &> /dev/null; then
|
||||
apt-get update
|
||||
apt-get install -y \
|
||||
python3-dev \
|
||||
python3-pip \
|
||||
python3-venv \
|
||||
portaudio19-dev \
|
||||
libsndfile1 \
|
||||
ffmpeg \
|
||||
alsa-utils \
|
||||
pulseaudio \
|
||||
xdotool
|
||||
elif command -v dnf &> /dev/null; then
|
||||
dnf install -y \
|
||||
python3-devel \
|
||||
python3-pip \
|
||||
portaudio-devel \
|
||||
libsndfile \
|
||||
ffmpeg \
|
||||
alsa-utils \
|
||||
pulseaudio \
|
||||
xdotool
|
||||
elif command -v yum &> /dev/null; then
|
||||
yum install -y \
|
||||
python3-devel \
|
||||
python3-pip \
|
||||
portaudio-devel \
|
||||
libsndfile \
|
||||
ffmpeg \
|
||||
alsa-utils \
|
||||
pulseaudio \
|
||||
xdotool
|
||||
else
|
||||
print_warning "Unknown package manager. Please install the following packages manually:"
|
||||
print_warning "- Python 3 development headers"
|
||||
print_warning "- PortAudio development library"
|
||||
print_warning "- libsndfile"
|
||||
print_warning "- FFmpeg"
|
||||
print_warning "- ALSA utilities"
|
||||
print_warning "- PulseAudio"
|
||||
print_warning "- xdotool"
|
||||
fi
|
||||
|
||||
print_success "System dependencies installed"
|
||||
}
|
||||
|
||||
create_directories() {
|
||||
print_status "Creating directories..."
|
||||
|
||||
# Create directories with proper permissions
|
||||
mkdir -p "$INSTALL_DIR"
|
||||
mkdir -p "$CONFIG_DIR"
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Set permissions
|
||||
chmod 755 "$INSTALL_DIR"
|
||||
chmod 755 "$CONFIG_DIR"
|
||||
chmod 755 "$LOG_DIR"
|
||||
|
||||
print_success "Directories created"
|
||||
}
|
||||
|
||||
install_application() {
|
||||
print_status "Installing application files..."
|
||||
|
||||
# Copy source files
|
||||
cp -r "$PROJECT_DIR/src"/* "$INSTALL_DIR/"
|
||||
|
||||
# Copy configuration files
|
||||
cp "$PROJECT_DIR/config/default_settings.json" "$CONFIG_DIR/"
|
||||
|
||||
# Create user config if it doesn't exist
|
||||
if [[ ! -f "$CONFIG_DIR/user_settings.json" ]]; then
|
||||
echo '{}' > "$CONFIG_DIR/user_settings.json"
|
||||
fi
|
||||
|
||||
# Copy sounds directory if it exists
|
||||
if [[ -d "$PROJECT_DIR/sounds" ]]; then
|
||||
cp -r "$PROJECT_DIR/sounds" "$INSTALL_DIR/"
|
||||
fi
|
||||
|
||||
# Make main script executable
|
||||
chmod +x "$INSTALL_DIR/main.py"
|
||||
|
||||
# Update config paths in the default config
|
||||
sed -i "s|/home/vshnsngh/Desktop/Coding/voice-to-text-system/logs/|$LOG_DIR/|g" "$CONFIG_DIR/default_settings.json"
|
||||
|
||||
print_success "Application files installed"
|
||||
}
|
||||
|
||||
install_python_deps() {
|
||||
print_status "Installing Python dependencies..."
|
||||
|
||||
# Create virtual environment
|
||||
python3 -m venv "$INSTALL_DIR/venv"
|
||||
|
||||
# Activate virtual environment and install dependencies
|
||||
source "$INSTALL_DIR/venv/bin/activate"
|
||||
|
||||
# Upgrade pip
|
||||
pip install --upgrade pip
|
||||
|
||||
# Install required packages
|
||||
pip install \
|
||||
whisper \
|
||||
sounddevice \
|
||||
soundfile \
|
||||
numpy \
|
||||
pyautogui \
|
||||
pyperclip \
|
||||
pynput
|
||||
|
||||
deactivate
|
||||
|
||||
print_success "Python dependencies installed"
|
||||
}
|
||||
|
||||
create_launcher() {
|
||||
print_status "Creating launcher script..."
|
||||
|
||||
# Create launcher script
|
||||
cat > "$BIN_DIR/voice-to-text" << EOF
|
||||
#!/bin/bash
|
||||
# Voice-to-Text System Launcher
|
||||
|
||||
cd "$INSTALL_DIR"
|
||||
source "$INSTALL_DIR/venv/bin/activate"
|
||||
exec python3 "$INSTALL_DIR/main.py" --config "$CONFIG_DIR" "\$@"
|
||||
EOF
|
||||
|
||||
chmod +x "$BIN_DIR/voice-to-text"
|
||||
|
||||
print_success "Launcher script created"
|
||||
}
|
||||
|
||||
install_service() {
|
||||
print_status "Installing systemd service..."
|
||||
|
||||
# Copy service file
|
||||
cp "$PROJECT_DIR/install/voice-to-text.service" "/etc/systemd/system/"
|
||||
|
||||
# Update service file with correct paths
|
||||
sed -i "s|/opt/voice-to-text-system|$INSTALL_DIR|g" "/etc/systemd/system/voice-to-text.service"
|
||||
sed -i "s|/etc/voice-to-text-system|$CONFIG_DIR|g" "/etc/systemd/system/voice-to-text.service"
|
||||
|
||||
# Reload systemd
|
||||
systemctl daemon-reload
|
||||
|
||||
# Enable service (but don't start it yet)
|
||||
systemctl enable voice-to-text.service
|
||||
|
||||
print_success "Systemd service installed"
|
||||
}
|
||||
|
||||
show_post_install() {
|
||||
print_success "Installation completed successfully!"
|
||||
echo
|
||||
echo -e "${BLUE}Usage:${NC}"
|
||||
echo " voice-to-text # Run in foreground"
|
||||
echo " voice-to-text --daemon # Run as daemon"
|
||||
echo " voice-to-text --status # Check status"
|
||||
echo " voice-to-text --stop # Stop daemon"
|
||||
echo
|
||||
echo -e "${BLUE}Service Management:${NC}"
|
||||
echo " sudo systemctl start voice-to-text # Start service"
|
||||
echo " sudo systemctl stop voice-to-text # Stop service"
|
||||
echo " sudo systemctl status voice-to-text # Check service status"
|
||||
echo
|
||||
echo -e "${BLUE}Configuration:${NC}"
|
||||
echo " Config files: $CONFIG_DIR/"
|
||||
echo " Logs: $LOG_DIR/"
|
||||
echo " Application: $INSTALL_DIR/"
|
||||
echo
|
||||
echo -e "${YELLOW}Note:${NC} Default hotkey is Ctrl+Alt+V"
|
||||
echo -e "${YELLOW}Note:${NC} Make sure your microphone is working and accessible"
|
||||
}
|
||||
|
||||
# Main installation process
|
||||
main() {
|
||||
echo -e "${GREEN}Voice-to-Text System Installer${NC}"
|
||||
echo "================================"
|
||||
echo
|
||||
|
||||
check_root
|
||||
check_system
|
||||
install_system_deps
|
||||
create_directories
|
||||
install_application
|
||||
install_python_deps
|
||||
create_launcher
|
||||
install_service
|
||||
|
||||
echo
|
||||
show_post_install
|
||||
}
|
||||
|
||||
# Handle command line arguments
|
||||
case "$1" in
|
||||
--help|-h)
|
||||
echo "Usage: $0 [--help]"
|
||||
echo "Install the Voice-to-Text System"
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
main
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,54 @@
|
||||
[Unit]
|
||||
Description=Voice-to-Text System with Whisper
|
||||
Documentation=man:voice-to-text(1)
|
||||
After=network.target sound.target pulseaudio.service
|
||||
Wants=network.target sound.target
|
||||
|
||||
[Service]
|
||||
# Service configuration
|
||||
Type=simple
|
||||
User=root
|
||||
Group=root
|
||||
|
||||
# Working directory and execution
|
||||
WorkingDirectory=/opt/voice-to-text-system
|
||||
ExecStart=/opt/voice-to-text-system/venv/bin/python /opt/voice-to-text-system/main.py --config /etc/voice-to-text-system --daemon
|
||||
ExecStop=/bin/kill -TERM $MAINPID
|
||||
ExecReload=/bin/kill -HUP $MAINPID
|
||||
|
||||
# Runtime configuration
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
KillMode=process
|
||||
TimeoutStopSec=30
|
||||
|
||||
# Security settings
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/var/log/voice-to-text-system /tmp
|
||||
ProtectHome=true
|
||||
ProtectKernelTunables=true
|
||||
ProtectKernelModules=true
|
||||
ProtectControlGroups=true
|
||||
|
||||
# Resource limits
|
||||
LimitNOFILE=65536
|
||||
MemoryMax=2G
|
||||
CPUQuota=200%
|
||||
|
||||
# Environment
|
||||
Environment=PYTHONPATH=/opt/voice-to-text-system
|
||||
Environment=PYTHONUNBUFFERED=1
|
||||
Environment=HOME=/opt/voice-to-text-system
|
||||
|
||||
# Audio and GUI access
|
||||
SupplementaryGroups=audio pulse-access
|
||||
|
||||
# Logging
|
||||
StandardOutput=append:/var/log/voice-to-text-system/service.log
|
||||
StandardError=append:/var/log/voice-to-text-system/service.log
|
||||
SyslogIdentifier=voice-to-text
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,15 @@
|
||||
# Core AI/ML dependencies
|
||||
openai-whisper>=20230918
|
||||
|
||||
# Audio processing
|
||||
sounddevice>=0.4.6
|
||||
soundfile>=0.12.1
|
||||
numpy>=1.24.0
|
||||
|
||||
# GUI automation and input
|
||||
pyautogui>=0.9.54
|
||||
pyperclip>=1.8.2
|
||||
pynput>=1.7.6
|
||||
|
||||
# Additional utilities
|
||||
pathlib>=1.0.1
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,252 @@
|
||||
|
||||
import sounddevice as sd
|
||||
import soundfile as sf
|
||||
import numpy as np
|
||||
import os
|
||||
import logging
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
class AudioManager:
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
# Audio parameters
|
||||
self.sample_rate = self.config.get('audio', {}).get('sample_rate', 16000)
|
||||
self.channels = self.config.get('audio', {}).get('channels', 1)
|
||||
self.device_index = self.config.get('audio', {}).get('device_index')
|
||||
|
||||
# Recording state
|
||||
self.recording = False
|
||||
self.frames = []
|
||||
self.stream = None
|
||||
|
||||
# Validate audio configuration
|
||||
self._validate_config()
|
||||
|
||||
self.logger.info(f"Audio manager initialized - Rate: {self.sample_rate}Hz, Channels: {self.channels}")
|
||||
|
||||
def _validate_config(self):
|
||||
"""Validate audio configuration parameters"""
|
||||
try:
|
||||
# Check if specified device exists
|
||||
if self.device_index is not None:
|
||||
devices = sd.query_devices()
|
||||
if self.device_index >= len(devices) or self.device_index < 0:
|
||||
self.logger.warning(f"Invalid device index {self.device_index}, using default")
|
||||
self.device_index = None
|
||||
else:
|
||||
device_info = devices[self.device_index]
|
||||
self.logger.info(f"Using audio device: {device_info['name']}")
|
||||
|
||||
# Validate sample rate
|
||||
if self.sample_rate not in [8000, 16000, 22050, 44100, 48000]:
|
||||
self.logger.warning(f"Unusual sample rate: {self.sample_rate}Hz")
|
||||
|
||||
# Validate channels
|
||||
if self.channels not in [1, 2]:
|
||||
self.logger.warning(f"Unusual channel count: {self.channels}")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error validating audio config: {e}")
|
||||
|
||||
def start_recording(self):
|
||||
"""Start audio recording with error handling"""
|
||||
if self.recording:
|
||||
self.logger.warning("Recording is already in progress")
|
||||
return False
|
||||
|
||||
try:
|
||||
self.logger.info("Starting audio recording...")
|
||||
|
||||
# Reset frames buffer
|
||||
self.frames = []
|
||||
self.recording = True
|
||||
|
||||
# Create input stream
|
||||
self.stream = sd.InputStream(
|
||||
samplerate=self.sample_rate,
|
||||
channels=self.channels,
|
||||
device=self.device_index,
|
||||
callback=self.callback,
|
||||
dtype=np.float32
|
||||
)
|
||||
|
||||
# Start the stream
|
||||
self.stream.start()
|
||||
|
||||
# Play start sound
|
||||
self.play_sound('start')
|
||||
|
||||
self.logger.info("Audio recording started successfully")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error starting recording: {e}")
|
||||
self.recording = False
|
||||
if self.stream:
|
||||
try:
|
||||
self.stream.close()
|
||||
except:
|
||||
pass
|
||||
self.stream = None
|
||||
return False
|
||||
|
||||
def stop_recording(self):
|
||||
"""Stop audio recording and return the recorded file"""
|
||||
if not self.recording:
|
||||
self.logger.warning("No recording in progress")
|
||||
return None
|
||||
|
||||
try:
|
||||
self.logger.info("Stopping audio recording...")
|
||||
|
||||
# Stop and close stream
|
||||
if self.stream:
|
||||
self.stream.stop()
|
||||
self.stream.close()
|
||||
self.stream = None
|
||||
|
||||
self.recording = False
|
||||
|
||||
# Play stop sound
|
||||
self.play_sound('stop')
|
||||
|
||||
# Save and return recording
|
||||
audio_file = self.save_recording()
|
||||
|
||||
if audio_file:
|
||||
self.logger.info(f"Recording saved: {audio_file}")
|
||||
else:
|
||||
self.logger.warning("No audio data to save")
|
||||
|
||||
return audio_file
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error stopping recording: {e}")
|
||||
self.recording = False
|
||||
return None
|
||||
|
||||
def callback(self, indata, frames, time, status):
|
||||
"""Audio input callback function"""
|
||||
if status:
|
||||
self.logger.debug(f"Audio callback status: {status}")
|
||||
|
||||
if self.recording and indata is not None:
|
||||
# Append audio data to frames
|
||||
self.frames.append(indata.copy())
|
||||
|
||||
def save_recording(self):
|
||||
"""Save recorded audio to a temporary file"""
|
||||
if not self.frames:
|
||||
self.logger.warning("No audio frames to save")
|
||||
return None
|
||||
|
||||
try:
|
||||
# Concatenate all audio frames
|
||||
recording = np.concatenate(self.frames, axis=0)
|
||||
|
||||
# Check if we have actual audio data
|
||||
if recording.size == 0:
|
||||
self.logger.warning("Empty audio recording")
|
||||
return None
|
||||
|
||||
# Create temporary file
|
||||
temp_file = tempfile.NamedTemporaryFile(
|
||||
suffix='.wav',
|
||||
delete=False,
|
||||
prefix='voice_recording_'
|
||||
).name
|
||||
|
||||
# Save audio to file
|
||||
sf.write(temp_file, recording, self.sample_rate)
|
||||
|
||||
# Verify file was created and has content
|
||||
if os.path.exists(temp_file) and os.path.getsize(temp_file) > 0:
|
||||
duration = len(recording) / self.sample_rate
|
||||
self.logger.info(f"Recording saved: {duration:.2f}s, {len(recording)} samples")
|
||||
return temp_file
|
||||
else:
|
||||
self.logger.error("Failed to create audio file")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error saving recording: {e}")
|
||||
return None
|
||||
|
||||
def play_sound(self, sound_type):
|
||||
"""Play feedback sound with error handling"""
|
||||
try:
|
||||
# Get project root directory
|
||||
project_root = Path(__file__).parent.parent
|
||||
sound_file = project_root / 'sounds' / f'recording_{sound_type}.wav'
|
||||
|
||||
if sound_file.exists():
|
||||
try:
|
||||
data, fs = sf.read(str(sound_file), dtype='float32')
|
||||
|
||||
# Play sound in a separate thread to avoid blocking
|
||||
sd.play(data, fs)
|
||||
# Don't wait for completion to avoid blocking
|
||||
|
||||
self.logger.debug(f"Playing feedback sound: {sound_type}")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error playing sound {sound_type}: {e}")
|
||||
else:
|
||||
self.logger.debug(f"Sound file not found: {sound_file}")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error in play_sound: {e}")
|
||||
|
||||
def get_audio_devices(self):
|
||||
"""Get list of available audio devices"""
|
||||
try:
|
||||
devices = sd.query_devices()
|
||||
device_list = []
|
||||
|
||||
for i, device in enumerate(devices):
|
||||
if device['max_input_channels'] > 0: # Input devices only
|
||||
device_list.append({
|
||||
'index': i,
|
||||
'name': device['name'],
|
||||
'channels': device['max_input_channels'],
|
||||
'sample_rate': device['default_samplerate']
|
||||
})
|
||||
|
||||
self.logger.debug(f"Found {len(device_list)} input devices")
|
||||
return device_list
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error querying audio devices: {e}")
|
||||
return []
|
||||
|
||||
def test_audio_device(self, device_index=None):
|
||||
"""Test if audio device is working"""
|
||||
try:
|
||||
test_duration = 1.0 # 1 second test
|
||||
|
||||
# Record a short test sample
|
||||
test_data = sd.rec(
|
||||
int(test_duration * self.sample_rate),
|
||||
samplerate=self.sample_rate,
|
||||
channels=self.channels,
|
||||
device=device_index or self.device_index,
|
||||
dtype=np.float32
|
||||
)
|
||||
|
||||
sd.wait() # Wait for recording to complete
|
||||
|
||||
# Check if we got audio data
|
||||
if test_data is not None and test_data.size > 0:
|
||||
rms = np.sqrt(np.mean(test_data**2))
|
||||
self.logger.info(f"Audio device test successful. RMS level: {rms:.6f}")
|
||||
return True
|
||||
else:
|
||||
self.logger.error("Audio device test failed - no data received")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Audio device test failed: {e}")
|
||||
return False
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
class ConfigManager:
|
||||
def __init__(self, default_settings_path, user_settings_path):
|
||||
self.default_settings_path = default_settings_path
|
||||
self.user_settings_path = user_settings_path
|
||||
self.config = self.load_config()
|
||||
|
||||
def load_config(self):
|
||||
config = self._load_json_file(self.default_settings_path)
|
||||
user_config = self._load_json_file(self.user_settings_path)
|
||||
if user_config:
|
||||
config.update(user_config)
|
||||
return config
|
||||
|
||||
def _load_json_file(self, file_path):
|
||||
if os.path.exists(file_path):
|
||||
with open(file_path, 'r') as f:
|
||||
try:
|
||||
return json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
# Handle empty or invalid JSON
|
||||
return {}
|
||||
return {}
|
||||
|
||||
def get(self, key, default=None):
|
||||
return self.config.get(key, default)
|
||||
|
||||
def set(self, key, value):
|
||||
self.config[key] = value
|
||||
self.save_user_config()
|
||||
|
||||
def save_user_config(self):
|
||||
user_config = self._load_json_file(self.user_settings_path)
|
||||
user_config.update(self.config)
|
||||
with open(self.user_settings_path, 'w') as f:
|
||||
json.dump(user_config, f, indent=4)
|
||||
|
||||
def reload_config(self):
|
||||
self.config = self.load_config()
|
||||
@@ -0,0 +1,185 @@
|
||||
import threading
|
||||
import logging
|
||||
from pynput import keyboard
|
||||
import time
|
||||
|
||||
class HotkeyDetector:
|
||||
def __init__(self, config, callback=None):
|
||||
self.config = config
|
||||
self.hotkey_string = config.get('hotkey', '<ctrl>+<alt>+v')
|
||||
self.callback = callback
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
# Parse hotkey combination
|
||||
self.required_keys = self._parse_hotkey(self.hotkey_string)
|
||||
self.pressed_keys = set()
|
||||
self.listener = None
|
||||
self.running = False
|
||||
|
||||
# Debounce mechanism
|
||||
self.last_trigger_time = 0
|
||||
self.debounce_delay = 0.5 # 500ms
|
||||
|
||||
def _parse_hotkey(self, hotkey_string):
|
||||
"""
|
||||
Parse hotkey string like '<ctrl>+<alt>+v' into a set of keys
|
||||
"""
|
||||
keys = set()
|
||||
parts = hotkey_string.lower().replace('<', '').replace('>', '').split('+')
|
||||
|
||||
for part in parts:
|
||||
part = part.strip()
|
||||
if part == 'ctrl':
|
||||
keys.add(keyboard.Key.ctrl_l)
|
||||
keys.add(keyboard.Key.ctrl_r)
|
||||
elif part == 'alt':
|
||||
keys.add(keyboard.Key.alt_l)
|
||||
keys.add(keyboard.Key.alt_r)
|
||||
elif part == 'shift':
|
||||
keys.add(keyboard.Key.shift_l)
|
||||
keys.add(keyboard.Key.shift_r)
|
||||
elif part == 'cmd' or part == 'super':
|
||||
keys.add(keyboard.Key.cmd)
|
||||
elif part == 'space':
|
||||
keys.add(keyboard.Key.space)
|
||||
elif part == 'tab':
|
||||
keys.add(keyboard.Key.tab)
|
||||
elif part == 'enter':
|
||||
keys.add(keyboard.Key.enter)
|
||||
elif len(part) == 1:
|
||||
keys.add(part)
|
||||
|
||||
self.logger.info(f"Parsed hotkey '{hotkey_string}' to keys: {[str(k) for k in keys]}")
|
||||
return keys
|
||||
|
||||
def _on_press(self, key):
|
||||
"""Handle key press events"""
|
||||
try:
|
||||
# Add the pressed key to our set
|
||||
if hasattr(key, 'char') and key.char:
|
||||
self.pressed_keys.add(key.char.lower())
|
||||
else:
|
||||
self.pressed_keys.add(key)
|
||||
|
||||
# Check if our hotkey combination is pressed
|
||||
self._check_hotkey_combination()
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error in key press handler: {e}")
|
||||
|
||||
def _on_release(self, key):
|
||||
"""Handle key release events"""
|
||||
try:
|
||||
# Remove the released key from our set
|
||||
if hasattr(key, 'char') and key.char:
|
||||
self.pressed_keys.discard(key.char.lower())
|
||||
else:
|
||||
self.pressed_keys.discard(key)
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error in key release handler: {e}")
|
||||
|
||||
def _check_hotkey_combination(self):
|
||||
"""Check if the required key combination is pressed"""
|
||||
current_time = time.time()
|
||||
|
||||
# Check if any of the required keys match what's currently pressed
|
||||
matched_keys = set()
|
||||
for required_key in self.required_keys:
|
||||
if isinstance(required_key, str):
|
||||
# Character key
|
||||
if required_key in self.pressed_keys:
|
||||
matched_keys.add(required_key)
|
||||
else:
|
||||
# Special key (like Ctrl, Alt)
|
||||
if required_key in self.pressed_keys:
|
||||
matched_keys.add(required_key)
|
||||
# For modifier keys, check both left and right variants
|
||||
elif (required_key == keyboard.Key.ctrl_l or required_key == keyboard.Key.ctrl_r) and \
|
||||
(keyboard.Key.ctrl_l in self.pressed_keys or keyboard.Key.ctrl_r in self.pressed_keys):
|
||||
matched_keys.add(required_key)
|
||||
elif (required_key == keyboard.Key.alt_l or required_key == keyboard.Key.alt_r) and \
|
||||
(keyboard.Key.alt_l in self.pressed_keys or keyboard.Key.alt_r in self.pressed_keys):
|
||||
matched_keys.add(required_key)
|
||||
elif (required_key == keyboard.Key.shift_l or required_key == keyboard.Key.shift_r) and \
|
||||
(keyboard.Key.shift_l in self.pressed_keys or keyboard.Key.shift_r in self.pressed_keys):
|
||||
matched_keys.add(required_key)
|
||||
|
||||
# Check if we have a reasonable match (accounting for left/right modifier variants)
|
||||
required_count = len(self.required_keys)
|
||||
matched_count = len(matched_keys)
|
||||
|
||||
# For modifier keys, we need special logic since we add both left and right variants
|
||||
if 'ctrl' in self.hotkey_string.lower():
|
||||
required_count -= 1 # Subtract one because we added both ctrl_l and ctrl_r
|
||||
if 'alt' in self.hotkey_string.lower():
|
||||
required_count -= 1 # Subtract one because we added both alt_l and alt_r
|
||||
if 'shift' in self.hotkey_string.lower():
|
||||
required_count -= 1 # Subtract one because we added both shift_l and shift_r
|
||||
|
||||
# Simple matching based on the actual keys we need
|
||||
ctrl_pressed = keyboard.Key.ctrl_l in self.pressed_keys or keyboard.Key.ctrl_r in self.pressed_keys
|
||||
alt_pressed = keyboard.Key.alt_l in self.pressed_keys or keyboard.Key.alt_r in self.pressed_keys
|
||||
|
||||
# Check for the default hotkey: Ctrl+Alt+V
|
||||
hotkey_lower = self.hotkey_string.lower()
|
||||
if 'ctrl' in hotkey_lower and 'alt' in hotkey_lower and 'v' in hotkey_lower:
|
||||
if ctrl_pressed and alt_pressed and 'v' in self.pressed_keys:
|
||||
self._trigger_callback(current_time)
|
||||
|
||||
def _trigger_callback(self, current_time):
|
||||
"""Trigger the callback with debouncing"""
|
||||
if current_time - self.last_trigger_time >= self.debounce_delay:
|
||||
self.last_trigger_time = current_time
|
||||
self.logger.info(f"Hotkey triggered: {self.hotkey_string}")
|
||||
|
||||
if self.callback:
|
||||
# Run callback in a separate thread to avoid blocking
|
||||
threading.Thread(target=self.callback, daemon=True).start()
|
||||
|
||||
def start_listening(self):
|
||||
"""Start listening for hotkey presses"""
|
||||
if self.running:
|
||||
self.logger.warning("Hotkey detector is already running")
|
||||
return
|
||||
|
||||
try:
|
||||
self.running = True
|
||||
self.listener = keyboard.Listener(
|
||||
on_press=self._on_press,
|
||||
on_release=self._on_release
|
||||
)
|
||||
self.listener.start()
|
||||
self.logger.info(f"Started listening for hotkey: {self.hotkey_string}")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error starting hotkey listener: {e}")
|
||||
self.running = False
|
||||
raise
|
||||
|
||||
def stop_listening(self):
|
||||
"""Stop listening for hotkey presses"""
|
||||
if not self.running:
|
||||
return
|
||||
|
||||
try:
|
||||
self.running = False
|
||||
if self.listener:
|
||||
self.listener.stop()
|
||||
self.listener = None
|
||||
|
||||
self.pressed_keys.clear()
|
||||
self.logger.info("Stopped hotkey detector")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error stopping hotkey listener: {e}")
|
||||
|
||||
def update_hotkey(self, new_hotkey):
|
||||
"""Update the hotkey combination"""
|
||||
self.hotkey_string = new_hotkey
|
||||
self.required_keys = self._parse_hotkey(new_hotkey)
|
||||
self.logger.info(f"Updated hotkey to: {new_hotkey}")
|
||||
|
||||
def set_callback(self, callback):
|
||||
"""Set or update the callback function"""
|
||||
self.callback = callback
|
||||
+323
@@ -0,0 +1,323 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Voice-to-Text System
|
||||
Main application entry point that coordinates all components
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
# Add the current directory to Python path
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
# Import our modules
|
||||
from config_manager import ConfigManager
|
||||
from speech_recognizer import SpeechRecognizer
|
||||
from audio_manager import AudioManager
|
||||
from text_inserter import TextInserter
|
||||
from hotkey_detector import HotkeyDetector
|
||||
from service_manager import ServiceManager
|
||||
|
||||
class VoiceToTextApp:
|
||||
def __init__(self, config_dir=None):
|
||||
# Set up config paths
|
||||
if config_dir is None:
|
||||
config_dir = Path(__file__).parent.parent / 'config'
|
||||
else:
|
||||
config_dir = Path(config_dir)
|
||||
|
||||
self.default_config_path = config_dir / 'default_settings.json'
|
||||
self.user_config_path = config_dir / 'user_settings.json'
|
||||
|
||||
# Initialize components
|
||||
self.config_manager = ConfigManager(
|
||||
str(self.default_config_path),
|
||||
str(self.user_config_path)
|
||||
)
|
||||
self.config = self.config_manager.config
|
||||
|
||||
# Setup logging
|
||||
self._setup_logging()
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
# Initialize other components
|
||||
self.speech_recognizer = SpeechRecognizer(self.config)
|
||||
self.audio_manager = AudioManager(self.config)
|
||||
self.text_inserter = TextInserter(self.config)
|
||||
self.hotkey_detector = HotkeyDetector(self.config, self.on_hotkey_pressed)
|
||||
self.service_manager = ServiceManager(self.config)
|
||||
|
||||
# Application state
|
||||
self.is_recording = False
|
||||
self.recording_thread = None
|
||||
self.running = True
|
||||
|
||||
self.logger.info("Voice-to-Text application initialized")
|
||||
|
||||
def _setup_logging(self):
|
||||
"""Setup logging configuration"""
|
||||
log_level = self.config.get('logging', {}).get('level', 'INFO')
|
||||
log_file = self.config.get('logging', {}).get('file')
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
if log_file:
|
||||
log_dir = Path(log_file).parent
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, log_level),
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.StreamHandler(),
|
||||
logging.FileHandler(log_file) if log_file else logging.NullHandler()
|
||||
]
|
||||
)
|
||||
|
||||
def on_hotkey_pressed(self):
|
||||
"""Callback function called when hotkey is pressed"""
|
||||
try:
|
||||
if not self.is_recording:
|
||||
self.start_recording()
|
||||
else:
|
||||
self.stop_recording()
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error handling hotkey press: {e}")
|
||||
|
||||
def start_recording(self):
|
||||
"""Start voice recording"""
|
||||
if self.is_recording:
|
||||
self.logger.warning("Already recording")
|
||||
return
|
||||
|
||||
self.logger.info("Starting voice recording...")
|
||||
self.is_recording = True
|
||||
|
||||
# Start recording in a separate thread
|
||||
self.recording_thread = threading.Thread(
|
||||
target=self._recording_worker,
|
||||
daemon=True
|
||||
)
|
||||
self.recording_thread.start()
|
||||
|
||||
def stop_recording(self):
|
||||
"""Stop voice recording and process the audio"""
|
||||
if not self.is_recording:
|
||||
self.logger.warning("Not currently recording")
|
||||
return
|
||||
|
||||
self.logger.info("Stopping voice recording...")
|
||||
self.is_recording = False
|
||||
|
||||
# The recording worker will handle the rest
|
||||
|
||||
def _recording_worker(self):
|
||||
"""Worker thread that handles the recording process"""
|
||||
try:
|
||||
# Start audio recording
|
||||
self.audio_manager.start_recording()
|
||||
|
||||
# Wait while recording (the hotkey callback will set is_recording to False)
|
||||
while self.is_recording and self.running:
|
||||
time.sleep(0.1)
|
||||
|
||||
# Stop recording and get the audio file
|
||||
audio_file = self.audio_manager.stop_recording()
|
||||
|
||||
if audio_file and os.path.exists(audio_file):
|
||||
self.logger.info(f"Audio recorded: {audio_file}")
|
||||
|
||||
# Transcribe the audio
|
||||
self.logger.info("Starting transcription...")
|
||||
transcribed_text = self.speech_recognizer.transcribe(audio_file)
|
||||
|
||||
if transcribed_text and transcribed_text.strip():
|
||||
self.logger.info(f"Transcribed: '{transcribed_text}'")
|
||||
|
||||
# Insert the text
|
||||
success = self.text_inserter.insert_text(transcribed_text)
|
||||
|
||||
if success:
|
||||
self.logger.info("Text inserted successfully")
|
||||
else:
|
||||
self.logger.error("Failed to insert text")
|
||||
# Try clipboard method as fallback
|
||||
self.logger.info("Trying clipboard method...")
|
||||
self.text_inserter.insert_text_clipboard(transcribed_text)
|
||||
else:
|
||||
self.logger.warning("No text transcribed")
|
||||
|
||||
# Clean up temporary audio file
|
||||
try:
|
||||
os.remove(audio_file)
|
||||
self.logger.debug(f"Cleaned up audio file: {audio_file}")
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error cleaning up audio file: {e}")
|
||||
|
||||
else:
|
||||
self.logger.error("No audio file recorded")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error in recording worker: {e}")
|
||||
finally:
|
||||
self.is_recording = False
|
||||
|
||||
def start(self, daemon_mode=False):
|
||||
"""Start the application"""
|
||||
try:
|
||||
self.logger.info("Starting Voice-to-Text application...")
|
||||
|
||||
# Start hotkey detector
|
||||
self.hotkey_detector.start_listening()
|
||||
self.logger.info(f"Listening for hotkey: {self.config.get('hotkey')}")
|
||||
|
||||
if daemon_mode:
|
||||
# Run as daemon service
|
||||
self.service_manager.daemonize()
|
||||
self.service_manager.start_service(self._main_loop)
|
||||
else:
|
||||
# Run in foreground
|
||||
self._main_loop()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
self.logger.info("Keyboard interrupt received")
|
||||
self.stop()
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error starting application: {e}")
|
||||
self.stop()
|
||||
raise
|
||||
|
||||
def _main_loop(self):
|
||||
"""Main application loop"""
|
||||
self.logger.info("Voice-to-Text application is running. Press hotkey to record.")
|
||||
|
||||
try:
|
||||
while self.running:
|
||||
time.sleep(1)
|
||||
|
||||
# Reload configuration if needed (could add file watching here)
|
||||
# self.config_manager.reload_config()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
self.logger.info("Received interrupt signal")
|
||||
self.stop()
|
||||
|
||||
def stop(self):
|
||||
"""Stop the application gracefully"""
|
||||
self.logger.info("Stopping Voice-to-Text application...")
|
||||
|
||||
self.running = False
|
||||
|
||||
# Stop recording if in progress
|
||||
if self.is_recording:
|
||||
self.stop_recording()
|
||||
|
||||
# Stop hotkey detector
|
||||
if self.hotkey_detector:
|
||||
self.hotkey_detector.stop_listening()
|
||||
|
||||
# Stop service manager
|
||||
if self.service_manager:
|
||||
self.service_manager.stop_service()
|
||||
|
||||
self.logger.info("Application stopped")
|
||||
|
||||
def get_status(self):
|
||||
"""Get current application status"""
|
||||
return {
|
||||
'running': self.running,
|
||||
'recording': self.is_recording,
|
||||
'hotkey': self.config.get('hotkey'),
|
||||
'whisper_model': self.config.get('whisper', {}).get('model'),
|
||||
'service_status': self.service_manager.get_service_status()
|
||||
}
|
||||
|
||||
def main():
|
||||
"""Main entry point"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Voice-to-Text System with Whisper',
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog="""
|
||||
Examples:
|
||||
%(prog)s # Run in foreground mode
|
||||
%(prog)s --daemon # Run as daemon
|
||||
%(prog)s --status # Show status
|
||||
%(prog)s --stop # Stop running daemon
|
||||
%(prog)s --config /path # Use custom config directory
|
||||
"""
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--daemon', '-d',
|
||||
action='store_true',
|
||||
help='Run as daemon service'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--status', '-s',
|
||||
action='store_true',
|
||||
help='Show application status'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--stop',
|
||||
action='store_true',
|
||||
help='Stop running daemon'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--config', '-c',
|
||||
help='Path to configuration directory'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--verbose', '-v',
|
||||
action='store_true',
|
||||
help='Enable verbose logging'
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
# Create application instance
|
||||
app = VoiceToTextApp(args.config)
|
||||
|
||||
# Enable verbose logging if requested
|
||||
if args.verbose:
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
|
||||
if args.status:
|
||||
# Show status
|
||||
status = app.get_status()
|
||||
print("Voice-to-Text Status:")
|
||||
for key, value in status.items():
|
||||
print(f" {key}: {value}")
|
||||
return
|
||||
|
||||
if args.stop:
|
||||
# Stop daemon
|
||||
if app.service_manager.is_running():
|
||||
print("Stopping Voice-to-Text daemon...")
|
||||
# Send termination signal to running process
|
||||
with open('/tmp/voice-to-text.pid', 'r') as f:
|
||||
pid = int(f.read().strip())
|
||||
os.kill(pid, 15) # SIGTERM
|
||||
print("Stop signal sent")
|
||||
else:
|
||||
print("Voice-to-Text daemon is not running")
|
||||
return
|
||||
|
||||
# Start the application
|
||||
app.start(daemon_mode=args.daemon)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,182 @@
|
||||
import os
|
||||
import sys
|
||||
import signal
|
||||
import logging
|
||||
import time
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
class ServiceManager:
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self.running = False
|
||||
self.pid_file = '/tmp/voice-to-text.pid'
|
||||
|
||||
# Signal handlers
|
||||
signal.signal(signal.SIGTERM, self._signal_handler)
|
||||
signal.signal(signal.SIGINT, self._signal_handler)
|
||||
|
||||
def _signal_handler(self, signum, frame):
|
||||
"""Handle shutdown signals gracefully"""
|
||||
self.logger.info(f"Received signal {signum}, shutting down...")
|
||||
self.stop_service()
|
||||
|
||||
def start_service(self, main_app_callback=None):
|
||||
"""Start the service as a daemon or regular process"""
|
||||
if self.is_running():
|
||||
self.logger.error("Service is already running")
|
||||
return False
|
||||
|
||||
try:
|
||||
self.running = True
|
||||
self._create_pid_file()
|
||||
|
||||
self.logger.info("Voice-to-Text service started")
|
||||
|
||||
# If a main app callback is provided, run it
|
||||
if main_app_callback:
|
||||
main_app_callback()
|
||||
else:
|
||||
# Default service loop
|
||||
self._service_loop()
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error starting service: {e}")
|
||||
self.running = False
|
||||
return False
|
||||
|
||||
def stop_service(self):
|
||||
"""Stop the service gracefully"""
|
||||
if not self.running:
|
||||
return
|
||||
|
||||
try:
|
||||
self.running = False
|
||||
self._remove_pid_file()
|
||||
self.logger.info("Voice-to-Text service stopped")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error stopping service: {e}")
|
||||
|
||||
def _service_loop(self):
|
||||
"""Default service loop - just keep running"""
|
||||
try:
|
||||
while self.running:
|
||||
time.sleep(1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
self.logger.info("Keyboard interrupt received")
|
||||
self.stop_service()
|
||||
|
||||
def is_running(self):
|
||||
"""Check if the service is already running"""
|
||||
if not os.path.exists(self.pid_file):
|
||||
return False
|
||||
|
||||
try:
|
||||
with open(self.pid_file, 'r') as f:
|
||||
pid = int(f.read().strip())
|
||||
|
||||
# Check if process with this PID exists
|
||||
try:
|
||||
os.kill(pid, 0) # Send null signal to check if process exists
|
||||
return True
|
||||
except OSError:
|
||||
# Process doesn't exist, remove stale PID file
|
||||
self._remove_pid_file()
|
||||
return False
|
||||
|
||||
except (ValueError, IOError):
|
||||
# Invalid or unreadable PID file
|
||||
self._remove_pid_file()
|
||||
return False
|
||||
|
||||
def _create_pid_file(self):
|
||||
"""Create PID file for the current process"""
|
||||
try:
|
||||
pid = os.getpid()
|
||||
with open(self.pid_file, 'w') as f:
|
||||
f.write(str(pid))
|
||||
self.logger.debug(f"Created PID file: {self.pid_file} with PID: {pid}")
|
||||
|
||||
except IOError as e:
|
||||
self.logger.error(f"Error creating PID file: {e}")
|
||||
|
||||
def _remove_pid_file(self):
|
||||
"""Remove the PID file"""
|
||||
try:
|
||||
if os.path.exists(self.pid_file):
|
||||
os.remove(self.pid_file)
|
||||
self.logger.debug(f"Removed PID file: {self.pid_file}")
|
||||
|
||||
except IOError as e:
|
||||
self.logger.error(f"Error removing PID file: {e}")
|
||||
|
||||
def restart_service(self, main_app_callback=None):
|
||||
"""Restart the service"""
|
||||
self.logger.info("Restarting service...")
|
||||
self.stop_service()
|
||||
time.sleep(1) # Brief pause
|
||||
return self.start_service(main_app_callback)
|
||||
|
||||
def get_service_status(self):
|
||||
"""Get current service status"""
|
||||
if self.is_running():
|
||||
try:
|
||||
with open(self.pid_file, 'r') as f:
|
||||
pid = int(f.read().strip())
|
||||
return {
|
||||
'status': 'running',
|
||||
'pid': pid,
|
||||
'pid_file': self.pid_file
|
||||
}
|
||||
except:
|
||||
return {'status': 'unknown'}
|
||||
else:
|
||||
return {'status': 'stopped'}
|
||||
|
||||
def daemonize(self):
|
||||
"""Daemonize the current process (Unix only)"""
|
||||
try:
|
||||
# First fork
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
sys.exit(0) # Parent process exits
|
||||
|
||||
except OSError as e:
|
||||
self.logger.error(f"First fork failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# Become session leader
|
||||
os.setsid()
|
||||
|
||||
try:
|
||||
# Second fork
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
sys.exit(0) # Second parent exits
|
||||
|
||||
except OSError as e:
|
||||
self.logger.error(f"Second fork failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# Change working directory and file permissions
|
||||
os.chdir('/')
|
||||
os.umask(0)
|
||||
|
||||
# Redirect standard file descriptors
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
|
||||
# Close or redirect stdin, stdout, stderr
|
||||
with open('/dev/null', 'r') as dev_null:
|
||||
os.dup2(dev_null.fileno(), sys.stdin.fileno())
|
||||
|
||||
with open('/dev/null', 'w') as dev_null:
|
||||
os.dup2(dev_null.fileno(), sys.stdout.fileno())
|
||||
os.dup2(dev_null.fileno(), sys.stderr.fileno())
|
||||
|
||||
self.logger.info("Process daemonized successfully")
|
||||
@@ -0,0 +1,104 @@
|
||||
|
||||
import whisper
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
class SpeechRecognizer:
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
self.model_name = self.config.get('whisper', {}).get('model', 'small')
|
||||
self.language = self.config.get('whisper', {}).get('language')
|
||||
self.model = None
|
||||
|
||||
# Load model on initialization
|
||||
self.load_model()
|
||||
|
||||
def load_model(self):
|
||||
"""Load the Whisper model with error handling"""
|
||||
try:
|
||||
self.logger.info(f"Loading Whisper model: {self.model_name}")
|
||||
|
||||
# Check if model name is valid
|
||||
valid_models = ['tiny', 'base', 'small', 'medium', 'large']
|
||||
if self.model_name not in valid_models:
|
||||
self.logger.error(f"Invalid model name: {self.model_name}. Valid models: {valid_models}")
|
||||
self.model_name = 'small' # Fallback
|
||||
self.logger.info(f"Using fallback model: {self.model_name}")
|
||||
|
||||
self.model = whisper.load_model(self.model_name)
|
||||
self.logger.info(f"Successfully loaded Whisper model: {self.model_name}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error loading Whisper model '{self.model_name}': {e}")
|
||||
self.model = None
|
||||
return False
|
||||
|
||||
def transcribe(self, audio_file):
|
||||
"""Transcribe audio file to text with comprehensive error handling"""
|
||||
if not self.model:
|
||||
self.logger.error("No Whisper model loaded")
|
||||
return None
|
||||
|
||||
if not audio_file or not os.path.exists(audio_file):
|
||||
self.logger.error(f"Audio file not found: {audio_file}")
|
||||
return None
|
||||
|
||||
try:
|
||||
self.logger.debug(f"Starting transcription of: {audio_file}")
|
||||
|
||||
# Check file size
|
||||
file_size = os.path.getsize(audio_file)
|
||||
if file_size == 0:
|
||||
self.logger.warning("Audio file is empty")
|
||||
return None
|
||||
|
||||
self.logger.debug(f"Audio file size: {file_size} bytes")
|
||||
|
||||
# Transcribe with specified language or auto-detect
|
||||
transcribe_options = {
|
||||
'language': self.language,
|
||||
'task': 'transcribe',
|
||||
'verbose': False
|
||||
}
|
||||
|
||||
# Remove None values
|
||||
transcribe_options = {k: v for k, v in transcribe_options.items() if v is not None}
|
||||
|
||||
result = self.model.transcribe(audio_file, **transcribe_options)
|
||||
|
||||
if result and 'text' in result:
|
||||
transcribed_text = result['text'].strip()
|
||||
|
||||
if transcribed_text:
|
||||
self.logger.info(f"Transcription successful: {len(transcribed_text)} characters")
|
||||
self.logger.debug(f"Transcribed text: '{transcribed_text[:100]}...'" if len(transcribed_text) > 100 else f"Transcribed text: '{transcribed_text}'")
|
||||
return transcribed_text
|
||||
else:
|
||||
self.logger.warning("Transcription resulted in empty text")
|
||||
return None
|
||||
else:
|
||||
self.logger.error("Invalid transcription result")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error during transcription: {e}")
|
||||
return None
|
||||
|
||||
def is_model_loaded(self):
|
||||
"""Check if model is loaded and ready"""
|
||||
return self.model is not None
|
||||
|
||||
def get_model_info(self):
|
||||
"""Get information about the loaded model"""
|
||||
if not self.model:
|
||||
return None
|
||||
|
||||
return {
|
||||
'model_name': self.model_name,
|
||||
'language': self.language,
|
||||
'loaded': True
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import pyautogui
|
||||
import time
|
||||
import logging
|
||||
|
||||
class TextInserter:
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.typing_speed = config.get('typing', {}).get('speed', 0.05)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
# Configure pyautogui
|
||||
pyautogui.FAILSAFE = False
|
||||
pyautogui.PAUSE = 0
|
||||
|
||||
def insert_text(self, text):
|
||||
"""
|
||||
Insert text at current cursor position with typing simulation
|
||||
"""
|
||||
if not text or not text.strip():
|
||||
self.logger.warning("No text to insert")
|
||||
return False
|
||||
|
||||
try:
|
||||
# Clean the text
|
||||
clean_text = text.strip()
|
||||
|
||||
# Type the text with the configured speed
|
||||
if self.typing_speed > 0:
|
||||
for char in clean_text:
|
||||
pyautogui.write(char)
|
||||
time.sleep(self.typing_speed)
|
||||
else:
|
||||
pyautogui.write(clean_text)
|
||||
|
||||
self.logger.info(f"Successfully inserted text: {len(clean_text)} characters")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error inserting text: {e}")
|
||||
return False
|
||||
|
||||
def insert_text_clipboard(self, text):
|
||||
"""
|
||||
Alternative method using clipboard
|
||||
"""
|
||||
try:
|
||||
import pyperclip
|
||||
|
||||
# Save current clipboard content
|
||||
original_clipboard = pyperclip.paste()
|
||||
|
||||
# Copy text to clipboard
|
||||
pyperclip.copy(text.strip())
|
||||
|
||||
# Paste using Ctrl+V
|
||||
pyautogui.hotkey('ctrl', 'v')
|
||||
|
||||
# Restore original clipboard after a brief delay
|
||||
time.sleep(0.1)
|
||||
pyperclip.copy(original_clipboard)
|
||||
|
||||
self.logger.info(f"Successfully inserted text via clipboard: {len(text.strip())} characters")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error inserting text via clipboard: {e}")
|
||||
return False
|
||||
|
||||
def set_typing_speed(self, speed):
|
||||
"""
|
||||
Update typing speed
|
||||
"""
|
||||
self.typing_speed = max(0, speed)
|
||||
self.logger.info(f"Typing speed set to: {self.typing_speed}")
|
||||
Reference in New Issue
Block a user