Repository navigation
Expand file tree
/
Copy pathvoice_web_server.py
More file actions
479 lines (413 loc) · 16.1 KB
/
Copy pathvoice_web_server.py
File metadata and controls
479 lines (413 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import json
import tempfile
import os
import atexit
import traceback
from urllib.parse import quote
from urllib.request import urlopen
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from urllib.parse import urlparse
import cgi
from main import generate_hybrid
try:
from cactus import cactus_init, cactus_transcribe, cactus_destroy
except Exception:
cactus_init = None
cactus_transcribe = None
cactus_destroy = None
WEB_DIR = Path(__file__).resolve().parent / "web"
HOST = "127.0.0.1"
PORT = 8080
def _resolve_whisper_weights():
root = Path(__file__).resolve().parent
candidates = [
root / "weights" / "whisper-small",
root.parent / "weights" / "whisper-small",
root / "cactus" / "weights" / "whisper-small",
root.parent / "cactus" / "weights" / "whisper-small",
]
for candidate in candidates:
if candidate.exists():
return str(candidate)
return "weights/whisper-small"
WHISPER_WEIGHTS = _resolve_whisper_weights()
_WHISPER_MODEL = None
TOOLS = [
{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
},
"required": ["location"],
},
},
{
"name": "set_alarm",
"description": "Set an alarm for a given time",
"parameters": {
"type": "object",
"properties": {
"hour": {"type": "integer", "description": "Hour to set the alarm for"},
"minute": {"type": "integer", "description": "Minute to set the alarm for"},
},
"required": ["hour", "minute"],
},
},
{
"name": "send_message",
"description": "Send a message to a contact",
"parameters": {
"type": "object",
"properties": {
"recipient": {"type": "string", "description": "Name of the person to send the message to"},
"message": {"type": "string", "description": "The message content to send"},
},
"required": ["recipient", "message"],
},
},
{
"name": "create_reminder",
"description": "Create a reminder with a title and time",
"parameters": {
"type": "object",
"properties": {
"title": {"type": "string", "description": "Reminder title"},
"time": {"type": "string", "description": "Time for the reminder (e.g. 3:00 PM)"},
},
"required": ["title", "time"],
},
},
{
"name": "search_contacts",
"description": "Search for a contact by name",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Name to search for"},
},
"required": ["query"],
},
},
{
"name": "play_music",
"description": "Play a song or playlist",
"parameters": {
"type": "object",
"properties": {
"song": {"type": "string", "description": "Song or playlist name"},
},
"required": ["song"],
},
},
{
"name": "set_timer",
"description": "Set a countdown timer",
"parameters": {
"type": "object",
"properties": {
"minutes": {"type": "integer", "description": "Number of minutes"},
},
"required": ["minutes"],
},
},
]
_WEATHER_CODE_MAP = {
0: "clear sky",
1: "mainly clear",
2: "partly cloudy",
3: "overcast",
45: "fog",
48: "depositing rime fog",
51: "light drizzle",
53: "moderate drizzle",
55: "dense drizzle",
61: "slight rain",
63: "moderate rain",
65: "heavy rain",
71: "slight snow",
73: "moderate snow",
75: "heavy snow",
80: "slight rain showers",
81: "moderate rain showers",
82: "violent rain showers",
95: "thunderstorm",
}
def _fetch_live_weather(location):
query = quote(location.strip())
geocode_url = f"https://geocoding-api.open-meteo.com/v1/search?name={query}&count=1&language=en&format=json"
with urlopen(geocode_url, timeout=6) as resp:
geo = json.loads(resp.read().decode("utf-8"))
results = geo.get("results") or []
if not results:
return f"Could not find location '{location}'."
first = results[0]
lat = first.get("latitude")
lon = first.get("longitude")
city = first.get("name", location)
country = first.get("country", "")
weather_url = (
"https://api.open-meteo.com/v1/forecast"
f"?latitude={lat}&longitude={lon}¤t=temperature_2m,apparent_temperature,weather_code,wind_speed_10m"
)
with urlopen(weather_url, timeout=6) as resp:
weather = json.loads(resp.read().decode("utf-8"))
current = weather.get("current") or {}
temp = current.get("temperature_2m")
feels = current.get("apparent_temperature")
wind = current.get("wind_speed_10m")
code = current.get("weather_code")
desc = _WEATHER_CODE_MAP.get(code, "unknown conditions")
place = f"{city}, {country}".strip().strip(",")
return f"Current weather in {place}: {temp}°C, feels like {feels}°C, {desc}, wind {wind} km/h."
def _simulate_action(call):
name = call.get("name", "")
args = call.get("arguments", {})
if name == "set_alarm":
hour = args.get("hour")
minute = int(args.get("minute", 0)) if str(args.get("minute", "")).isdigit() else args.get("minute")
return f"Alarm scheduled for {hour}:{minute:02d}" if isinstance(minute, int) else f"Alarm scheduled for {hour}:{minute}"
if name == "set_timer":
return f"Timer set for {args.get('minutes')} minutes"
if name == "send_message":
return f"Message sent to {args.get('recipient')}: {args.get('message')}"
if name == "create_reminder":
return f"Reminder created: {args.get('title')} at {args.get('time')}"
if name == "search_contacts":
return f"Searching contacts for: {args.get('query')}"
if name == "play_music":
return f"Playing music: {args.get('song')}"
if name == "get_weather":
location = args.get("location")
if not location:
return "Weather request missing location."
try:
return _fetch_live_weather(str(location))
except Exception:
traceback.print_exc()
return f"Unable to fetch live weather for {location} right now."
return f"Executed {name}"
def _build_assistant_response(transcript, calls, actions, warning=None):
if warning:
return warning
if not calls:
return "I heard you, but I could not map that to available actions. Please try rephrasing."
if len(actions) == 1:
return f"Done. {actions[0]}."
joined = "; ".join(actions)
return f"Done. I processed your request: {joined}."
def _build_route_response(transcript, routed):
raw_calls = routed.get("function_calls", [])
def _normalize_args_for_key(arguments):
normalized = {}
for k, v in (arguments or {}).items():
if isinstance(v, str):
normalized[k] = v.strip().lower()
else:
normalized[k] = v
return normalized
calls = []
seen = set()
for call in raw_calls:
key = (call.get("name", ""), json.dumps(_normalize_args_for_key(call.get("arguments", {})), sort_keys=True))
if key in seen:
continue
seen.add(key)
calls.append(call)
actions = []
action_seen = set()
for call in calls:
action = _simulate_action(call)
if action in action_seen:
continue
action_seen.add(action)
actions.append(action)
assistant_response = _build_assistant_response(transcript, calls, actions)
return {
"ok": True,
"transcript": transcript,
"source": routed.get("source", "unknown"),
"confidence": routed.get("confidence", 0),
"total_time_ms": routed.get("total_time_ms", 0),
"function_calls": calls,
"actions": actions,
"assistant_response": assistant_response,
}
def _get_whisper_model():
if not (cactus_init and cactus_transcribe and cactus_destroy):
return None
global _WHISPER_MODEL
if _WHISPER_MODEL is None:
_WHISPER_MODEL = cactus_init(WHISPER_WEIGHTS)
return _WHISPER_MODEL
@atexit.register
def _cleanup_whisper_model():
global _WHISPER_MODEL
if _WHISPER_MODEL is not None and cactus_destroy:
cactus_destroy(_WHISPER_MODEL)
_WHISPER_MODEL = None
def _transcribe_audio_bytes(audio_bytes):
model = _get_whisper_model()
if model is None:
raise RuntimeError("Whisper model unavailable. Ensure Cactus is set up with whisper weights.")
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
tmp.write(audio_bytes)
tmp_path = tmp.name
try:
raw = cactus_transcribe(model, tmp_path)
parsed = json.loads(raw)
transcript = str(parsed.get("response", "")).strip()
return transcript
finally:
try:
os.remove(tmp_path)
except OSError:
pass
class VoiceActionHandler(BaseHTTPRequestHandler):
def _json_response(self, status, payload):
body = json.dumps(payload).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _serve_file(self, file_path):
if not file_path.exists() or not file_path.is_file():
self.send_error(404, "Not found")
return
if file_path.suffix == ".html":
content_type = "text/html; charset=utf-8"
elif file_path.suffix == ".css":
content_type = "text/css; charset=utf-8"
elif file_path.suffix == ".js":
content_type = "application/javascript; charset=utf-8"
else:
content_type = "application/octet-stream"
content = file_path.read_bytes()
self.send_response(200)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content)
def do_GET(self):
parsed = urlparse(self.path)
path = parsed.path
if path == "/api/health":
bindings_ok = bool(cactus_init and cactus_transcribe and cactus_destroy)
weights_path = Path(WHISPER_WEIGHTS)
config_path = weights_path / "config.txt"
return self._json_response(200, {
"ok": True,
"server": "up",
"cactus_bindings": bindings_ok,
"whisper_weights_path": str(weights_path),
"whisper_weights_exists": weights_path.exists(),
"whisper_config_exists": config_path.exists(),
})
if path == "/":
return self._serve_file(WEB_DIR / "index.html")
if path == "/app.js":
return self._serve_file(WEB_DIR / "app.js")
if path == "/styles.css":
return self._serve_file(WEB_DIR / "styles.css")
self.send_error(404, "Not found")
def do_POST(self):
parsed = urlparse(self.path)
if parsed.path == "/api/transcribe_route":
if "multipart/form-data" not in self.headers.get("Content-Type", ""):
return self._json_response(400, {"ok": False, "error": "Expected multipart/form-data"})
try:
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={"REQUEST_METHOD": "POST", "CONTENT_TYPE": self.headers.get("Content-Type", "")},
)
audio_field = form["audio"] if "audio" in form else None
if audio_field is None or not getattr(audio_field, "file", None):
return self._json_response(400, {"ok": False, "error": "Missing audio file field 'audio'"})
audio_bytes = audio_field.file.read()
if not audio_bytes:
return self._json_response(400, {"ok": False, "error": "Uploaded audio is empty"})
try:
transcript = _transcribe_audio_bytes(audio_bytes)
except Exception as exc:
traceback.print_exc()
warning = f"Transcription failed: {exc}"
return self._json_response(200, {
"ok": True,
"transcript": "",
"source": "on-device",
"confidence": 0,
"total_time_ms": 0,
"function_calls": [],
"actions": [],
"warning": warning,
"assistant_response": _build_assistant_response("", [], [], warning=warning),
})
if not transcript:
warning = "No speech detected. Please speak louder or record longer."
return self._json_response(200, {
"ok": True,
"transcript": "",
"source": "on-device",
"confidence": 0,
"total_time_ms": 0,
"function_calls": [],
"actions": [],
"warning": warning,
"assistant_response": _build_assistant_response("", [], [], warning=warning),
})
messages = [{"role": "user", "content": transcript}]
try:
routed = generate_hybrid(messages, TOOLS)
return self._json_response(200, _build_route_response(transcript, routed))
except Exception as exc:
traceback.print_exc()
warning = f"Routing failed after transcription: {exc}"
return self._json_response(200, {
"ok": True,
"transcript": transcript,
"source": "on-device",
"confidence": 0,
"total_time_ms": 0,
"function_calls": [],
"actions": [],
"warning": warning,
"assistant_response": _build_assistant_response(transcript, [], [], warning=warning),
})
except Exception as exc:
traceback.print_exc()
return self._json_response(400, {"ok": False, "error": f"Malformed upload: {exc}"})
if parsed.path != "/api/route":
return self._json_response(404, {"error": "Unknown endpoint"})
try:
content_length = int(self.headers.get("Content-Length", "0"))
raw = self.rfile.read(content_length)
payload = json.loads(raw.decode("utf-8"))
transcript = str(payload.get("transcript", "")).strip()
except Exception:
return self._json_response(400, {"error": "Invalid JSON body"})
if not transcript:
return self._json_response(400, {"error": "Transcript is required"})
messages = [{"role": "user", "content": transcript}]
try:
routed = generate_hybrid(messages, TOOLS)
return self._json_response(200, _build_route_response(transcript, routed))
except Exception as exc:
return self._json_response(500, {"ok": False, "error": str(exc)})
def main():
if not WEB_DIR.exists():
raise FileNotFoundError("Missing web directory. Expected ./web with index.html")
server = ThreadingHTTPServer((HOST, PORT), VoiceActionHandler)
print(f"Voice web app running at http://{HOST}:{PORT}")
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.server_close()
if __name__ == "__main__":
main()