main.py (view raw)
1import io
2import json
3import subprocess
4import wave
5from datetime import date
6from time import sleep
7
8import pyaudio
9import requests
10from playsound3 import playsound
11from pysystemd import ServiceManager, ServiceStatus
12
13SIAM_DIR = "/home/sika/siam"
14VOICE_PATH = SIAM_DIR + "/voices/ella.safetensors"
15SESSION_ID = "ses_siam_" + date.today().isoformat()
16AGENT = "siam-tts"
17
18services = ["ollama", "pocket-tts"]
19
20
21def ensure_running_services():
22 for service in services:
23 status = ServiceStatus(service)
24 if not status.is_running():
25 print(service + " is not running")
26 manager = ServiceManager(service)
27 manager.start()
28 else:
29 print(service + " already running")
30
31
32def sync_waybar():
33 sleep(0.1)
34 subprocess.run(["pkill", "-RTMIN+8", "waybar"], check=True)
35
36
37def get_hex_status():
38 return json.loads(
39 subprocess.run(
40 ["hex", "status", "--json"], check=True, capture_output=True
41 ).stdout
42 )["capture"]["state"]
43
44
45def tts(text):
46 response = requests.post("http://localhost:8000/tts", files={"text": (None, text)})
47 response.raise_for_status()
48
49 with wave.open(io.BytesIO(response.content), "rb") as wf:
50 p = pyaudio.PyAudio()
51 stream = p.open(
52 format=p.get_format_from_width(wf.getsampwidth()),
53 channels=wf.getnchannels(),
54 rate=wf.getframerate(),
55 output=True,
56 )
57 stream.write(wf.readframes(wf.getnframes()))
58 stream.stop_stream()
59 stream.close()
60 p.terminate()
61
62
63if get_hex_status() == "idle":
64 ensure_running_services()
65 playsound(SIAM_DIR + "/sounds/trigger/oui.wav")
66 subprocess.run(["hex", "dictation", "start"], check=True)
67 sync_waybar()
68else:
69 stt_output = subprocess.run(
70 ["hex", "dictation", "stop", "--print"], check=True, capture_output=True
71 ).stdout.decode()
72 sync_waybar()
73 print("> " + stt_output)
74
75 oc_output = subprocess.run(
76 "opencode run -s " + SESSION_ID + " --agent " + AGENT + ' "' + stt_output + '"',
77 shell=True,
78 check=True,
79 capture_output=True,
80 ).stdout.decode()
81
82 print("< " + oc_output)
83 tts(oc_output)