-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvoice_assistant_example.py
More file actions
357 lines (297 loc) · 13.6 KB
/
voice_assistant_example.py
File metadata and controls
357 lines (297 loc) · 13.6 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
"""
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: voice_assistant_example.py ║
Version: 0.0.34 ║
Last Modified: 2026-03-09 03:52:20 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 356 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• 2a1fb0423 2026-03-03 Merge branch 'develop' into copilot/audit-src-module-docu... ║
• a629043ab 2026-02-22 Audit: document gaps found - benchmarks and stale annotat... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
"""
#!/usr/bin/env python3
"""
ThemisDB Voice Assistant - Example Usage
This example demonstrates how to use the ThemisDB Voice Assistant API
to record and transcribe phone calls, generate meeting protocols, and
interact with the database using voice commands.
Requirements:
pip install requests
Usage:
python voice_assistant_example.py
"""
import requests
import base64
import json
import os
from datetime import datetime
from pathlib import Path
# Configuration
THEMIS_URL = os.getenv("THEMIS_URL", "http://localhost:8080")
THEMIS_TOKEN = os.getenv("THEMIS_TOKEN", "your-jwt-token-here")
class ThemisVoiceAssistant:
"""Client for ThemisDB Voice Assistant API"""
def __init__(self, base_url: str, token: str):
self.base_url = base_url
self.headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
def transcribe_audio(self, audio_file: str, language: str = "auto") -> dict:
"""Transcribe audio file to text"""
with open(audio_file, "rb") as f:
audio_data = f.read()
audio_base64 = base64.b64encode(audio_data).decode()
response = requests.post(
f"{self.base_url}/api/v1/voice/transcribe",
headers=self.headers,
json={
"audio_base64": audio_base64,
"language": language,
"timestamps": True,
"speaker_diarization": False
}
)
response.raise_for_status()
return response.json()
def synthesize_speech(self, text: str, voice: str = "default") -> bytes:
"""Convert text to speech"""
response = requests.post(
f"{self.base_url}/api/v1/voice/synthesize",
headers=self.headers,
json={
"text": text,
"voice": voice,
"speed": 1.0,
"format": "wav",
"return_base64": True
}
)
response.raise_for_status()
result = response.json()
return base64.b64decode(result["audio_base64"])
def process_voice_command(self, text: str, session_id: str = "default") -> dict:
"""Process a voice command"""
response = requests.post(
f"{self.base_url}/api/v1/voice/command",
headers=self.headers,
json={
"text": text,
"session_id": session_id
}
)
response.raise_for_status()
return response.json()
def record_phone_call(self, audio_file: str, caller: str, callee: str, **kwargs) -> dict:
"""Record and transcribe a phone call"""
with open(audio_file, "rb") as f:
audio_data = f.read()
audio_base64 = base64.b64encode(audio_data).decode()
payload = {
"audio_base64": audio_base64,
"call_id": kwargs.get("call_id", f"call-{datetime.now().strftime('%Y%m%d-%H%M%S')}"),
"caller": caller,
"callee": callee,
"start_time": kwargs.get("start_time", int(datetime.now().timestamp() * 1000)),
"end_time": kwargs.get("end_time", int(datetime.now().timestamp() * 1000)),
"call_type": kwargs.get("call_type", "inbound"),
"custom_fields": kwargs.get("custom_fields", {})
}
response = requests.post(
f"{self.base_url}/api/v1/voice/call/record",
headers=self.headers,
json=payload
)
response.raise_for_status()
return response.json()
def generate_meeting_protocol(self, audio_file: str, title: str, participants: list, **kwargs) -> dict:
"""Generate a meeting protocol from audio"""
with open(audio_file, "rb") as f:
audio_data = f.read()
audio_base64 = base64.b64encode(audio_data).decode()
payload = {
"audio_base64": audio_base64,
"meeting_id": kwargs.get("meeting_id", f"meeting-{datetime.now().strftime('%Y%m%d-%H%M%S')}"),
"title": title,
"start_time": kwargs.get("start_time", int(datetime.now().timestamp() * 1000)),
"end_time": kwargs.get("end_time", int(datetime.now().timestamp() * 1000)),
"organizer": kwargs.get("organizer", ""),
"participants": participants,
"custom_fields": kwargs.get("custom_fields", {})
}
response = requests.post(
f"{self.base_url}/api/v1/voice/meeting/protocol",
headers=self.headers,
json=payload
)
response.raise_for_status()
return response.json()
def get_stats(self) -> dict:
"""Get voice assistant statistics"""
response = requests.get(
f"{self.base_url}/api/v1/voice/stats",
headers=self.headers
)
response.raise_for_status()
return response.json()
def example_1_transcribe_audio():
"""Example 1: Transcribe an audio file"""
print("\n" + "="*60)
print("Example 1: Transcribe Audio File")
print("="*60)
assistant = ThemisVoiceAssistant(THEMIS_URL, THEMIS_TOKEN)
# Note: Replace with actual audio file
# result = assistant.transcribe_audio("sample_audio.mp3", language="en")
# print(f"Transcript: {result['text']}")
# print(f"Language: {result['language']}")
# print(f"Confidence: {result['confidence']:.2%}")
print("Note: This example requires an actual audio file.")
print("Usage: result = assistant.transcribe_audio('audio.mp3')")
def example_2_synthesize_speech():
"""Example 2: Convert text to speech"""
print("\n" + "="*60)
print("Example 2: Text-to-Speech Synthesis")
print("="*60)
assistant = ThemisVoiceAssistant(THEMIS_URL, THEMIS_TOKEN)
text = "Hello! Welcome to ThemisDB Voice Assistant."
print(f"Synthesizing: {text}")
# audio_data = assistant.synthesize_speech(text, voice="default")
# with open("output.wav", "wb") as f:
# f.write(audio_data)
# print("Audio saved to output.wav")
print("Note: Uncomment the code above to generate audio.")
def example_3_voice_command():
"""Example 3: Process voice command"""
print("\n" + "="*60)
print("Example 3: Voice Command Processing")
print("="*60)
assistant = ThemisVoiceAssistant(THEMIS_URL, THEMIS_TOKEN)
commands = [
"Show me the total revenue for this month",
"List the top 10 customers by sales",
"What was our best selling product last week?"
]
session_id = "demo-session"
for command in commands:
print(f"\nUser: {command}")
# result = assistant.process_voice_command(command, session_id)
# print(f"Assistant: {result['response']}")
print("\nNote: Uncomment the code above to process commands.")
def example_4_phone_call_recording():
"""Example 4: Record and transcribe phone call"""
print("\n" + "="*60)
print("Example 4: Phone Call Recording")
print("="*60)
assistant = ThemisVoiceAssistant(THEMIS_URL, THEMIS_TOKEN)
# result = assistant.record_phone_call(
# audio_file="call_recording.mp3",
# caller="+1234567890",
# callee="+0987654321",
# call_type="inbound",
# custom_fields={
# "department": "Customer Support",
# "category": "Technical Issue",
# "priority": "high"
# }
# )
#
# print(f"Call ID: {result['call_id']}")
# print(f"Duration: {result['duration_ms'] / 1000 / 60:.1f} minutes")
# print(f"\nTranscript:\n{result['transcript']}")
# print(f"\nSummary:\n{result['summary']}")
# print(f"\nStored as: {result['document_id']}")
print("Note: This example requires an actual call recording.")
print("The recording will be transcribed, summarized, and stored in ThemisDB")
print("with full revision control and audit logging.")
def example_5_meeting_protocol():
"""Example 5: Generate meeting protocol"""
print("\n" + "="*60)
print("Example 5: Meeting Protocol Generation")
print("="*60)
assistant = ThemisVoiceAssistant(THEMIS_URL, THEMIS_TOKEN)
# result = assistant.generate_meeting_protocol(
# audio_file="meeting_recording.wav",
# title="Q4 Planning Meeting",
# participants=[
# "john.doe@company.com",
# "jane.smith@company.com",
# "bob.jones@company.com"
# ],
# organizer="john.doe@company.com",
# custom_fields={
# "project": "Phoenix",
# "location": "Conference Room A"
# }
# )
#
# print(f"Meeting: {result['title']}")
# print(f"Duration: {result['duration_ms'] / 1000 / 60:.1f} minutes")
# print(f"\nSummary:\n{result['summary']}")
# print(f"\nKey Points:")
# for point in result['key_points']:
# print(f" - {point}")
# print(f"\nAction Items:")
# for item in result['action_items']:
# print(f" [ ] {item['description']}")
# print(f"\nStored as: {result['document_id']}")
print("Note: This example requires an actual meeting recording.")
print("The protocol will include:")
print(" - Full transcript with timestamps")
print(" - AI-generated summary")
print(" - Key discussion points")
print(" - Action items with assignments")
def example_6_statistics():
"""Example 6: Get voice assistant statistics"""
print("\n" + "="*60)
print("Example 6: Voice Assistant Statistics")
print("="*60)
assistant = ThemisVoiceAssistant(THEMIS_URL, THEMIS_TOKEN)
# stats = assistant.get_stats()
# print(json.dumps(stats, indent=2))
print("Note: Statistics include:")
print(" - STT: transcriptions completed, processing time, real-time factor")
print(" - TTS: syntheses completed, audio duration")
print(" - LLM: tokens processed, cache hits, latency")
print(" - Sessions: active session count")
def main():
"""Run all examples"""
print("""
╔══════════════════════════════════════════════════════════╗
║ ThemisDB Voice Assistant - Example Usage ║
╚══════════════════════════════════════════════════════════╝
This script demonstrates the Voice Assistant API capabilities:
- Speech-to-Text transcription
- Text-to-Speech synthesis
- Voice command processing
- Phone call recording and transcription
- Meeting protocol generation
Note: Most examples are commented out and require actual audio files.
Uncomment the code blocks to try them with your own data.
""")
# Run examples
example_1_transcribe_audio()
example_2_synthesize_speech()
example_3_voice_command()
example_4_phone_call_recording()
example_5_meeting_protocol()
example_6_statistics()
print("\n" + "="*60)
print("Examples completed!")
print("="*60)
print("\nFor more information, see:")
print(" - docs/en/features/voice_assistant_guide.md")
print(" - docs/de/features/sprachassistent_anleitung.md")
print()
if __name__ == "__main__":
main()