forked from ShaerWare/AI_Secretary_System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
executable file
·360 lines (287 loc) · 12.5 KB
/
admin.py
File metadata and controls
executable file
·360 lines (287 loc) · 12.5 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
#!/usr/bin/env python3
"""
CLI Админка для AI Secretary System
Использование:
./admin.py status # Статус системы
./admin.py tts presets # Список пресетов TTS
./admin.py tts preset warm # Установить пресет
./admin.py tts test "Привет" # Тестовый синтез
./admin.py llm prompt # Показать промпт
./admin.py llm model # Текущая модель
"""
import sys
import time
import click
import requests
from rich import box
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
console = Console()
# URL оркестратора
ORCHESTRATOR_URL = "http://localhost:8002"
def api_get(endpoint: str):
"""GET запрос к API"""
try:
resp = requests.get(f"{ORCHESTRATOR_URL}{endpoint}", timeout=10)
resp.raise_for_status()
return resp.json()
except requests.exceptions.ConnectionError:
console.print("[red]Ошибка: Оркестратор недоступен[/red]")
console.print(f"[dim]Проверьте, запущен ли: curl {ORCHESTRATOR_URL}/health[/dim]")
sys.exit(1)
except Exception as e:
console.print(f"[red]Ошибка API: {e}[/red]")
sys.exit(1)
def api_post(endpoint: str, data: dict):
"""POST запрос к API"""
try:
resp = requests.post(f"{ORCHESTRATOR_URL}{endpoint}", json=data, timeout=60)
resp.raise_for_status()
return resp.json()
except requests.exceptions.ConnectionError:
console.print("[red]Ошибка: Оркестратор недоступен[/red]")
sys.exit(1)
except requests.exceptions.HTTPError as e:
try:
detail = e.response.json().get("detail", str(e))
except Exception:
detail = str(e)
console.print(f"[red]Ошибка: {detail}[/red]")
sys.exit(1)
except Exception as e:
console.print(f"[red]Ошибка API: {e}[/red]")
sys.exit(1)
def api_delete(endpoint: str):
"""DELETE запрос к API"""
try:
resp = requests.delete(f"{ORCHESTRATOR_URL}{endpoint}", timeout=10)
resp.raise_for_status()
return resp.json()
except requests.exceptions.ConnectionError:
console.print("[red]Ошибка: Оркестратор недоступен[/red]")
sys.exit(1)
except Exception as e:
console.print(f"[red]Ошибка API: {e}[/red]")
sys.exit(1)
@click.group()
def cli():
"""AI Secretary Admin CLI"""
pass
# ============== STATUS ==============
@cli.command()
@click.option("--watch", "-w", is_flag=True, help="Мониторинг в реальном времени")
def status(watch):
"""Показать статус системы"""
def show_status():
data = api_get("/admin/status")
# Заголовок
console.print()
console.print(
Panel.fit("[bold cyan]AI Secretary System[/bold cyan]", subtitle="Status Dashboard")
)
# Таблица сервисов
table = Table(title="Сервисы", box=box.ROUNDED)
table.add_column("Сервис", style="cyan")
table.add_column("Статус", justify="center")
services = data.get("services", {})
status_icons = {True: "[green]✓ Running[/green]", False: "[red]✗ Stopped[/red]"}
table.add_row("Voice Clone (XTTS v2)", status_icons[services.get("voice_clone", False)])
table.add_row("LLM (Gemini)", status_icons[services.get("llm", False)])
table.add_row("STT (Whisper)", status_icons[services.get("stt", False)])
table.add_row("Streaming TTS", status_icons[services.get("streaming_tts", False)])
console.print(table)
# GPU информация
gpu_info = data.get("gpu", [])
if gpu_info:
gpu_table = Table(title="GPU", box=box.ROUNDED)
gpu_table.add_column("ID", style="dim")
gpu_table.add_column("Название", style="cyan")
gpu_table.add_column("Память", justify="right")
for gpu in gpu_info:
mem_str = f"{gpu['used_gb']:.1f} / {gpu['total_gb']:.1f} GB"
gpu_table.add_row(str(gpu["id"]), gpu["name"], mem_str)
console.print(gpu_table)
# TTS конфигурация
tts_config = data.get("tts_config")
if tts_config:
tts_table = Table(title="TTS Configuration", box=box.ROUNDED)
tts_table.add_column("Параметр", style="cyan")
tts_table.add_column("Значение")
tts_table.add_row("Device", tts_config.get("device", "N/A"))
tts_table.add_row(
"Default Preset", f"[yellow]{tts_config.get('default_preset', 'N/A')}[/yellow]"
)
tts_table.add_row("Voice Samples", str(tts_config.get("samples_count", 0)))
console.print(tts_table)
# LLM конфигурация
llm_config = data.get("llm_config")
if llm_config:
llm_table = Table(title="LLM Configuration", box=box.ROUNDED)
llm_table.add_column("Параметр", style="cyan")
llm_table.add_column("Значение")
llm_table.add_row("Model", f"[yellow]{llm_config.get('model_name', 'N/A')}[/yellow]")
llm_table.add_row("History Length", str(llm_config.get("history_length", 0)))
prompt = llm_config.get("system_prompt", "")
if len(prompt) > 50:
prompt = prompt[:50] + "..."
llm_table.add_row("System Prompt", f"[dim]{prompt}[/dim]")
console.print(llm_table)
# Streaming TTS статистика
streaming_stats = data.get("streaming_tts_stats")
if streaming_stats:
console.print(
f"\n[dim]Streaming TTS Cache: {streaming_stats.get('cache_size', 0)} items[/dim]"
)
if watch:
console.print("[dim]Нажмите Ctrl+C для выхода[/dim]")
try:
while True:
console.clear()
show_status()
time.sleep(2)
except KeyboardInterrupt:
console.print("\n[dim]Выход[/dim]")
else:
show_status()
# ============== TTS ==============
@cli.group()
def tts():
"""Управление TTS (голосовым синтезом)"""
pass
@tts.command("presets")
def tts_presets():
"""Список доступных пресетов TTS"""
data = api_get("/admin/tts/presets")
table = Table(title="TTS Presets", box=box.ROUNDED)
table.add_column("Пресет", style="cyan")
table.add_column("Название")
table.add_column("Temp", justify="right")
table.add_column("Speed", justify="right")
table.add_column("", justify="center")
current = data.get("current", "")
for name, preset in data.get("presets", {}).items():
marker = "[green]◀ current[/green]" if name == current else ""
table.add_row(
name,
preset.get("display_name", ""),
str(preset.get("temperature", "")),
str(preset.get("speed", "")),
marker,
)
console.print(table)
@tts.command("preset")
@click.argument("name")
def tts_set_preset(name):
"""Установить пресет TTS"""
data = api_post("/admin/tts/preset", {"preset": name})
console.print(
f"[green]✓[/green] Пресет изменён на: [yellow]{data['preset']}[/yellow] ({data.get('display_name', '')})"
)
settings = data.get("settings", {})
console.print(
f" temperature: {settings.get('temperature', 'N/A')}, speed: {settings.get('speed', 'N/A')}"
)
@tts.command("test")
@click.argument("text")
@click.option("--preset", "-p", default="natural", help="Пресет для синтеза")
def tts_test(text, preset):
"""Тестовый синтез речи"""
console.print(f"[dim]Синтезирую: '{text}'...[/dim]")
with console.status("[bold green]Синтез...[/bold green]"):
data = api_post("/admin/tts/test", {"text": text, "preset": preset})
console.print("[green]✓[/green] Готово!")
console.print(f" Файл: [cyan]{data.get('file', 'N/A')}[/cyan]")
console.print(f" Длительность: {data.get('duration_sec', 0):.2f} сек")
console.print(f" Время синтеза: {data.get('synthesis_time_sec', 0):.2f} сек")
console.print(f" RTF: {data.get('rtf', 0):.2f}x")
@tts.command("cache")
@click.option("--clear", is_flag=True, help="Очистить кэш")
def tts_cache(clear):
"""Статистика/очистка кэша streaming TTS"""
if clear:
data = api_delete("/admin/tts/cache")
console.print(f"[green]✓[/green] Кэш очищен: {data.get('cleared_items', 0)} элементов")
else:
data = api_get("/admin/tts/cache")
console.print(f"Cache size: [cyan]{data.get('cache_size', 0)}[/cyan] items")
console.print(f"Active sessions: [cyan]{data.get('active_sessions', 0)}[/cyan]")
# ============== LLM ==============
@cli.group()
def llm():
"""Управление LLM (языковой моделью)"""
pass
@llm.command("prompt")
@click.argument("new_prompt", required=False)
def llm_prompt(new_prompt):
"""Показать/изменить системный промпт"""
if new_prompt:
data = api_post("/admin/llm/prompt", {"prompt": new_prompt})
console.print("[green]✓[/green] Промпт обновлён")
console.print(f"[dim]{data.get('prompt', '')}[/dim]")
else:
data = api_get("/admin/llm/prompt")
console.print(
Panel(
data.get("prompt", "N/A"),
title="System Prompt",
subtitle=f"Model: {data.get('model', 'N/A')}",
)
)
@llm.command("model")
@click.argument("new_model", required=False)
def llm_model(new_model):
"""Показать/изменить модель LLM"""
if new_model:
data = api_post("/admin/llm/model", {"model": new_model})
console.print(
f"[green]✓[/green] Модель изменена на: [yellow]{data.get('model', '')}[/yellow]"
)
else:
data = api_get("/admin/llm/model")
console.print(f"Текущая модель: [yellow]{data.get('model', 'N/A')}[/yellow]")
console.print("[dim]Доступные: gemini-2.5-flash, gemini-2.5-pro, gemini-2.0-flash[/dim]")
@llm.command("history")
@click.option("--clear", is_flag=True, help="Очистить историю")
def llm_history(clear):
"""Показать/очистить историю диалога"""
if clear:
data = api_delete("/admin/llm/history")
console.print(
f"[green]✓[/green] История очищена: {data.get('cleared_messages', 0)} сообщений"
)
else:
data = api_get("/admin/llm/history")
count = data.get("count", 0)
if count == 0:
console.print("[dim]История диалога пуста[/dim]")
else:
console.print(f"[cyan]История диалога ({count} сообщений):[/cyan]\n")
for msg in data.get("history", []):
role = msg.get("role", "")
content = msg.get("content", "")
if role == "user":
console.print(
f"[blue]User:[/blue] {content[:100]}{'...' if len(content) > 100 else ''}"
)
else:
console.print(
f"[green]Assistant:[/green] {content[:100]}{'...' if len(content) > 100 else ''}"
)
@llm.command("test")
@click.argument("question")
def llm_test(question):
"""Тестовый запрос к LLM"""
console.print(f"[dim]Запрос: '{question}'[/dim]")
with console.status("[bold green]Генерация ответа...[/bold green]"):
try:
resp = requests.post(f"{ORCHESTRATOR_URL}/chat", json={"text": question}, timeout=30)
resp.raise_for_status()
data = resp.json()
except Exception as e:
console.print(f"[red]Ошибка: {e}[/red]")
return
console.print(Panel(data.get("response", "N/A"), title="Ответ Лидии", border_style="green"))
# ============== MAIN ==============
if __name__ == "__main__":
cli()