-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetrics_stt.py
113 lines (90 loc) · 3.84 KB
/
metrics_stt.py
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
import logging
import asyncio
from pathlib import Path
from dotenv import load_dotenv
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.metrics import STTMetrics, EOUMetrics
from livekit.agents.voice import Agent, AgentSession
from livekit.agents.voice.room_io import RoomInputOptions
from livekit.plugins import deepgram, openai, silero
from rich.console import Console
from rich.table import Table
from rich import box
from datetime import datetime
logger = logging.getLogger("metrics-stt")
logger.setLevel(logging.INFO)
console = Console()
load_dotenv(dotenv_path=Path(__file__).parent.parent / '.env')
class STTMetricsAgent(Agent):
def __init__(self) -> None:
super().__init__(
instructions="""
You are a helpful agent.
""",
stt=deepgram.STT(),
llm=openai.LLM(model="gpt-4o"),
tts=openai.TTS(),
vad=silero.VAD.load()
)
def stt_wrapper(metrics: STTMetrics):
asyncio.create_task(self.on_stt_metrics_collected(metrics))
def eou_wrapper(metrics: EOUMetrics):
asyncio.create_task(self.on_eou_metrics_collected(metrics))
self.stt.on("metrics_collected", stt_wrapper)
self.stt.on("eou_metrics_collected", eou_wrapper)
async def on_stt_metrics_collected(self, metrics: STTMetrics) -> None:
table = Table(
title="[bold blue]STT Metrics Report[/bold blue]",
box=box.ROUNDED,
highlight=True,
show_header=True,
header_style="bold cyan"
)
table.add_column("Metric", style="bold green")
table.add_column("Value", style="yellow")
timestamp = datetime.fromtimestamp(metrics.timestamp).strftime('%Y-%m-%d %H:%M:%S')
table.add_row("Type", str(metrics.type))
table.add_row("Label", str(metrics.label))
table.add_row("Request ID", str(metrics.request_id))
table.add_row("Timestamp", timestamp)
table.add_row("Duration", f"[white]{metrics.duration:.4f}[/white]s")
table.add_row("Speech ID", str(metrics.speech_id))
table.add_row("Error", str(metrics.error))
table.add_row("Streamed", "✓" if metrics.streamed else "✗")
table.add_row("Audio Duration", f"[white]{metrics.audio_duration:.4f}[/white]s")
console.print("\n")
console.print(table)
console.print("\n")
async def on_eou_metrics_collected(self, metrics: EOUMetrics) -> None:
table = Table(
title="[bold blue]End of Utterance Metrics Report[/bold blue]",
box=box.ROUNDED,
highlight=True,
show_header=True,
header_style="bold cyan"
)
table.add_column("Metric", style="bold green")
table.add_column("Value", style="yellow")
timestamp = datetime.fromtimestamp(metrics.timestamp).strftime('%Y-%m-%d %H:%M:%S')
table.add_row("Type", str(metrics.type))
table.add_row("Label", str(metrics.label))
table.add_row("Timestamp", timestamp)
table.add_row("End of Utterance Delay", f"[white]{metrics.end_of_utterance_delay:.4f}[/white]s")
table.add_row("Transcription Delay", f"[white]{metrics.transcription_delay:.4f}[/white]s")
table.add_row("Speech ID", str(metrics.speech_id))
table.add_row("Error", str(metrics.error))
console.print("\n")
console.print(table)
console.print("\n")
async def entrypoint(ctx: JobContext):
await ctx.connect()
session = AgentSession()
await session.start(
agent=STTMetricsAgent(),
room=ctx.room,
room_input_options=RoomInputOptions(),
)
if __name__ == "__main__":
cli.run_app(WorkerOptions(
entrypoint_fnc=entrypoint)
)