refactor: move systemd service to different file, delete default config
This commit is contained in:
parent
33e6f229d3
commit
499c155c83
4 changed files with 40 additions and 31 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[project]
|
[project]
|
||||||
name = "speechd"
|
name = "speechd"
|
||||||
version = "1.2.7"
|
version = "1.2.8"
|
||||||
description = "Speech-to-Text daemon with Groq Whisper API"
|
description = "Speech-to-Text daemon with Groq Whisper API"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
|
|
@ -23,6 +23,7 @@ build-backend = "hatchling.build"
|
||||||
|
|
||||||
[tool.hatch.build.targets.wheel]
|
[tool.hatch.build.targets.wheel]
|
||||||
packages = ["src/speechd"]
|
packages = ["src/speechd"]
|
||||||
|
include = ["src/speechd/*.service"]
|
||||||
|
|
||||||
[tool.hatch.build.targets.wheel.shared-scripts]
|
[tool.hatch.build.targets.wheel.shared-scripts]
|
||||||
"scripts/speechd-toggle" = "speechd-toggle"
|
"scripts/speechd-toggle" = "speechd-toggle"
|
||||||
|
|
|
||||||
|
|
@ -2,23 +2,14 @@ from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
from importlib import resources
|
||||||
|
|
||||||
from speechd.config import Config
|
from speechd.config import Config
|
||||||
from speechd.daemon import SpeechDaemon
|
from speechd.daemon import SpeechDaemon
|
||||||
|
|
||||||
SERVICE_UNIT = """[Unit]
|
|
||||||
Description=Speech-to-Text daemon
|
|
||||||
After=graphical-session.target
|
|
||||||
|
|
||||||
[Service]
|
def get_service_unit() -> str:
|
||||||
Type=simple
|
return resources.files("speechd").joinpath("speechd.service").read_text()
|
||||||
ExecStart=%h/.local/bin/speechd
|
|
||||||
Restart=on-failure
|
|
||||||
RestartSec=3
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=default.target
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def install_service():
|
def install_service():
|
||||||
|
|
@ -28,14 +19,13 @@ def install_service():
|
||||||
service_file = service_dir / "speechd.service"
|
service_file = service_dir / "speechd.service"
|
||||||
|
|
||||||
service_dir.mkdir(parents=True, exist_ok=True)
|
service_dir.mkdir(parents=True, exist_ok=True)
|
||||||
service_file.write_text(SERVICE_UNIT)
|
service_file.write_text(get_service_unit())
|
||||||
|
|
||||||
print(f"Installed: {service_file}")
|
print(f"Installed: {service_file}")
|
||||||
print("\nNext steps:")
|
print("\nNext steps:")
|
||||||
print(" 1. Run 'speechd' to create config file")
|
print(" 1. Create ~/.config/speechd/config.toml with your API key")
|
||||||
print(" 2. Edit ~/.config/speechd/config.toml and add your API key")
|
print(" 2. Run: systemctl --user enable --now speechd")
|
||||||
print(" 3. Run: systemctl --user enable --now speechd")
|
print(" 3. Toggle recording: speechd-toggle")
|
||||||
print(" 4. Toggle recording: speechd-toggle")
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,15 @@
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import stat
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
DEFAULT_CONFIG = """api_key = "your-grok-api-key"
|
|
||||||
model = "whisper-large-v3-turbo"
|
def get_config_path() -> Path:
|
||||||
language = "ru"
|
config_home = os.environ.get("XDG_CONFIG_HOME")
|
||||||
sample_rate = 16000
|
if config_home:
|
||||||
timeout = 300
|
return Path(config_home) / "speechd" / "config.toml"
|
||||||
audio_quality = 0.8
|
return Path.home() / ".config" / "speechd" / "config.toml"
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
|
|
@ -25,14 +26,16 @@ class Config:
|
||||||
def load(cls) -> "Config":
|
def load(cls) -> "Config":
|
||||||
import tomllib
|
import tomllib
|
||||||
|
|
||||||
config_path = Path.home() / ".config" / "speechd" / "config.toml"
|
config_path = get_config_path()
|
||||||
|
|
||||||
if not config_path.exists():
|
if not config_path.exists():
|
||||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
raise RuntimeError(f"Config not found at {config_path}")
|
||||||
config_path.write_text(DEFAULT_CONFIG)
|
|
||||||
config_path.chmod(0o600)
|
mode = config_path.stat().st_mode
|
||||||
raise RuntimeError(
|
if mode & (stat.S_IRGRP | stat.S_IROTH):
|
||||||
f"Config created at {config_path}\nPlease edit and add your Groq API key"
|
logging.warning(
|
||||||
|
"WARNING: %s is world accessible. Consider limiting its permissions (600).",
|
||||||
|
config_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(config_path, "rb") as f:
|
with open(config_path, "rb") as f:
|
||||||
|
|
|
||||||
15
src/speechd/speechd.service
Normal file
15
src/speechd/speechd.service
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
[Unit]
|
||||||
|
Description=Speech-to-Text daemon
|
||||||
|
After=graphical-session.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=%h/.local/bin/speechd
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=3
|
||||||
|
|
||||||
|
ProtectSystem=full
|
||||||
|
NoNewPrivileges=yes
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
Loading…
Add table
Add a link
Reference in a new issue