Files
javis_bot/src/voice_worker/__main__.py
javis-bot 282f114e60 feat(weather): KMA current weather inside Korea with the app's stored key
The 기능 screen's 기상청 날씨 card stores the KMA API hub key encrypted
(kmaApiKey, masked in logs). On start the app passes it to the worker in
JARVIS_KMA_API_KEY; the worker takes it out of its environment before any
child process starts. Inside Korea getWeather reads 초단기실황 and the
coming hour's sky from 초단기예보, and falls back to open-meteo, then
met.no, on any KMA failure. The key is in KMA's URLs, so failures are
logged by their kind only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:05:31 +09:00

115 lines
4.4 KiB
Python

"""``JARVIS_VOICE_TOKEN=... python -m voice_worker`` (see voice_worker.spec.md)."""
from __future__ import annotations
import argparse
import asyncio
import os
import shutil
import sys
from .log import debug_log
def main() -> int:
ap = argparse.ArgumentParser(prog="voice_worker")
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--port", type=int, default=8771)
ap.add_argument("--stt-model", default="large-v3-turbo")
ap.add_argument("--stt-device", default="cuda")
ap.add_argument("--stt-compute", default="int8_float16")
ap.add_argument("--melo-python", default=None, help="python of the MeloTTS venv; no TTS without it")
ap.add_argument("--melo-device", default="cuda")
ap.add_argument("--melo-speed", type=float, default=1.5)
ap.add_argument("--melo-language", default="KR")
ap.add_argument("--history", default=None, help="conversation history SQLite file; no history without it")
ap.add_argument("--brain", action="store_true",
help="answer reply requests with the jarvis reply engine (needs --history)")
ap.add_argument("--reply-language", default="Korean",
help="answers come in this language unless the asker asks for another; '' for the asker's")
ap.add_argument("--tool-language", default="ko", help="ISO code tools get (e.g. the Wikipedia host)")
args = ap.parse_args()
if args.brain and not args.history:
print("--brain needs --history", file=sys.stderr)
return 2
token = os.environ.get("JARVIS_VOICE_TOKEN", "")
if not token:
print("JARVIS_VOICE_TOKEN is not set", file=sys.stderr)
return 2
# The MCP servers' secrets and the KMA key leave the environment before any child process (MeloTTS) starts;
# only the brain gets them (voice_worker.spec.md, "Tools").
from .brain import take_mcp_env
mcp_env = take_mcp_env()
from jarvis.tools.builtin.weather import take_kma_key
take_kma_key()
from .server import VoiceWorker, is_loopback, nvidia_card
from .stt_whisper import WhisperTranscriber
from .vad import SileroVad
if not is_loopback(args.host):
print("the voice worker only listens on a loopback address", file=sys.stderr)
return 2
transcriber = WhisperTranscriber(args.stt_model, args.stt_device, args.stt_compute)
transcriber.warm_up()
vad = SileroVad()
tts = None
if args.melo_python:
from .tts_melo import MeloEngine
tts = MeloEngine(args.melo_python, args.melo_device, args.melo_speed, args.melo_language)
history = brain = None
if args.history:
from jarvis.memory.history import HistoryStore
history = HistoryStore(args.history)
if args.brain:
from .brain import JarvisBrain
brain = JarvisBrain(language=args.tool_language or None, reply_language=args.reply_language or None,
mcp_env=mcp_env)
# Background collection runs apart from the voice and the brain (voice_worker.spec.md).
collector = None
if os.environ.get("JARVIS_COLLECT_PATH"):
from .collect import Collector, load_config
collector = Collector(load_config(os.environ["JARVIS_COLLECT_PATH"]))
collector.start()
# Voice recordings for checking what was heard and said, only when the app turns them on.
recorder = None
from .recordings import RECORDINGS_ENV, Recorder
if os.environ.get(RECORDINGS_ENV):
recorder = Recorder(os.environ[RECORDINGS_ENV])
async def run() -> None:
worker = VoiceWorker(token, transcriber, vad.stream, tts, history=history, brain=brain, recorder=recorder,
card=nvidia_card if shutil.which("nvidia-smi") else None)
server = await worker.serve(args.host, args.port)
debug_log(f"voice worker listening on {args.host}:{args.port} tts={'on' if tts else 'off'} "
f"history={'on' if history else 'off'} brain={'on' if brain else 'off'} "
f"recordings={'on' if recorder else 'off'}",
"voice_worker")
print(f"READY {args.host}:{args.port}", flush=True)
await server.serve_forever()
try:
asyncio.run(run())
except KeyboardInterrupt:
pass
finally:
if collector:
collector.stop()
if tts:
tts.close()
if history:
history.close()
if recorder:
recorder.close()
return 0
if __name__ == "__main__":
sys.exit(main())