Use in-process Llama cleanup

This commit is contained in:
Thales Maciel 2026-02-24 12:46:11 -03:00
parent 548be49112
commit a83a843e1a
No known key found for this signature in database
GPG key ID: 33112E6833C34679
7 changed files with 235 additions and 116 deletions

View file

@ -15,7 +15,7 @@ from faster_whisper import WhisperModel
from config import Config, load, redacted_dict
from recorder import start_recording, stop_recording
from aiprocess import AIConfig, build_processor
from aiprocess import build_processor
from inject import inject
from x11_hotkey import listen
@ -51,7 +51,7 @@ def _compute_type(device: str) -> str:
class Daemon:
def __init__(self, cfg: Config):
def __init__(self, cfg: Config, *, llama_verbose: bool = False):
self.cfg = cfg
self.lock = threading.Lock()
self.state = State.IDLE
@ -63,6 +63,7 @@ class Daemon:
device=cfg.stt.get("device", "cpu"),
compute_type=_compute_type(cfg.stt.get("device", "cpu")),
)
self.ai_processor = build_processor(verbose=llama_verbose)
self.indicator = None
self.status_icon = None
if AppIndicator3 is not None:
@ -183,25 +184,13 @@ class Daemon:
logging.info("stt: %s", text)
ai_model = (self.cfg.ai_cleanup.get("model") or "").strip()
ai_base_url = (self.cfg.ai_cleanup.get("base_url") or "").strip()
if ai_model and ai_base_url:
self.set_state(State.PROCESSING)
logging.info("ai processing started")
try:
processor = build_processor(
AIConfig(
model=ai_model,
base_url=ai_base_url,
api_key=self.cfg.ai_cleanup.get("api_key", ""),
timeout_sec=25,
language_hint="en",
)
)
ai_input = text
text = processor.process(ai_input) or text
except Exception as exc:
logging.error("ai process failed: %s", exc)
self.set_state(State.PROCESSING)
logging.info("ai processing started")
try:
ai_input = text
text = self.ai_processor.process(ai_input) or text
except Exception as exc:
logging.error("ai process failed: %s", exc)
logging.info("processed: %s", text)
@ -286,6 +275,7 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument("--config", default="", help="path to config.json")
parser.add_argument("--dry-run", action="store_true", help="log hotkey only")
parser.add_argument("-v", "--verbose", action="store_true", help="enable verbose logs")
args = parser.parse_args()
logging.basicConfig(
@ -299,7 +289,13 @@ def main():
logging.info("ready (hotkey: %s)", cfg.daemon.get("hotkey", ""))
logging.info("config (%s):\n%s", args.config or str(Path.home() / ".config" / "lel" / "config.json"), json.dumps(redacted_dict(cfg), indent=2))
daemon = Daemon(cfg)
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
try:
daemon = Daemon(cfg, llama_verbose=args.verbose)
except Exception as exc:
logging.error("startup failed: %s", exc)
raise SystemExit(1)
def handle_signal(_sig, _frame):
logging.info("signal received, shutting down")